blob: a2288f9f277b5305ef2e54a915207868ddcb4a3e [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 Moolenaar9e63f612016-03-17 23:13:28 +01003464 partial_T *partial = NULL;
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
Bram Moolenaar532c7802005-01-27 14:44:31 +00003500 /* Skip white space to allow ":call func ()". Not good, but required for
3501 * backward compatibility. */
3502 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 if (*startarg != '(')
3506 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003507 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 goto end;
3509 }
3510
3511 /*
3512 * When skipping, evaluate the function once, to find the end of the
3513 * arguments.
3514 * When the function takes a range, this is discovered after the first
3515 * call, and the loop is broken.
3516 */
3517 if (eap->skip)
3518 {
3519 ++emsg_skip;
3520 lnum = eap->line2; /* do it once, also with an invalid range */
3521 }
3522 else
3523 lnum = eap->line1;
3524 for ( ; lnum <= eap->line2; ++lnum)
3525 {
3526 if (!eap->skip && eap->addr_count > 0)
3527 {
3528 curwin->w_cursor.lnum = lnum;
3529 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003530#ifdef FEAT_VIRTUALEDIT
3531 curwin->w_cursor.coladd = 0;
3532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003535 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003536 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003537 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
3539 failed = TRUE;
3540 break;
3541 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
3543 /* Handle a function returning a Funcref, Dictionary or List. */
3544 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3545 {
3546 failed = TRUE;
3547 break;
3548 }
3549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (doesrange || eap->skip)
3552 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (aborting())
3559 break;
3560 }
3561 if (eap->skip)
3562 --emsg_skip;
3563
3564 if (!failed)
3565 {
3566 /* Check for trailing illegal characters and a following command. */
3567 if (!ends_excmd(*arg))
3568 {
3569 emsg_severe = TRUE;
3570 EMSG(_(e_trailing));
3571 }
3572 else
3573 eap->nextcmd = check_nextcmd(arg);
3574 }
3575
3576end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
3581/*
3582 * ":unlet[!] var1 ... " command.
3583 */
3584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 ex_unletlock(eap, eap->arg, 0);
3588}
3589
3590/*
3591 * ":lockvar" and ":unlockvar" commands
3592 */
3593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 int deep = 2;
3598
3599 if (eap->forceit)
3600 deep = -1;
3601 else if (vim_isdigit(*arg))
3602 {
3603 deep = getdigits(&arg);
3604 arg = skipwhite(arg);
3605 }
3606
3607 ex_unletlock(eap, arg, deep);
3608}
3609
3610/*
3611 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3612 */
3613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614ex_unletlock(
3615 exarg_T *eap,
3616 char_u *argstart,
3617 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618{
3619 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 do
3625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003627 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003628 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 if (lv.ll_name == NULL)
3630 error = TRUE; /* error but continue parsing */
3631 if (name_end == NULL || (!vim_iswhite(*name_end)
3632 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (name_end != NULL)
3635 {
3636 emsg_severe = TRUE;
3637 EMSG(_(e_trailing));
3638 }
3639 if (!(eap->skip || error))
3640 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 break;
3642 }
3643
3644 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 {
3646 if (eap->cmdidx == CMD_unlet)
3647 {
3648 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3649 error = TRUE;
3650 }
3651 else
3652 {
3653 if (do_lock_var(&lv, name_end, deep,
3654 eap->cmdidx == CMD_lockvar) == FAIL)
3655 error = TRUE;
3656 }
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 if (!eap->skip)
3660 clear_lval(&lv);
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 arg = skipwhite(name_end);
3663 } while (!ends_excmd(*arg));
3664
3665 eap->nextcmd = check_nextcmd(arg);
3666}
3667
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003669do_unlet_var(
3670 lval_T *lp,
3671 char_u *name_end,
3672 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 int ret = OK;
3675 int cc;
3676
3677 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 cc = *name_end;
3680 *name_end = NUL;
3681
3682 /* Normal name or expanded name. */
3683 if (check_changedtick(lp->ll_name))
3684 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003691 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 else if (lp->ll_range)
3695 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003696 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003698 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003699
3700 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3701 {
3702 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003703 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003704 return FAIL;
3705 ll_li = li;
3706 ++ll_n1;
3707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708
3709 /* Delete a range of List items. */
3710 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3711 {
3712 li = lp->ll_li->li_next;
3713 listitem_remove(lp->ll_list, lp->ll_li);
3714 lp->ll_li = li;
3715 ++lp->ll_n1;
3716 }
3717 }
3718 else
3719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 /* unlet a List item. */
3722 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003725 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 }
3727
3728 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729}
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003733 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
3735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003736do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 hashtab_T *ht;
3739 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003741 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003742 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 ht = find_var_ht(name, &varname);
3745 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003747 if (ht == &globvarht)
3748 d = &globvardict;
3749 else if (current_funccal != NULL
3750 && ht == &current_funccal->l_vars.dv_hashtab)
3751 d = &current_funccal->l_vars;
3752 else if (ht == &compat_hashtab)
3753 d = &vimvardict;
3754 else
3755 {
3756 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3757 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3758 }
3759 if (d == NULL)
3760 {
3761 EMSG2(_(e_intern2), "do_unlet()");
3762 return FAIL;
3763 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 hi = hash_find(ht, varname);
3765 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003767 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003768 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003769 || var_check_ro(di->di_flags, name, FALSE)
3770 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003771 return FAIL;
3772
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773 delete_var(ht, hi);
3774 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777 if (forceit)
3778 return OK;
3779 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781}
3782
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783/*
3784 * Lock or unlock variable indicated by "lp".
3785 * "deep" is the levels to go (-1 for unlimited);
3786 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3787 */
3788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789do_lock_var(
3790 lval_T *lp,
3791 char_u *name_end,
3792 int deep,
3793 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003794{
3795 int ret = OK;
3796 int cc;
3797 dictitem_T *di;
3798
3799 if (deep == 0) /* nothing to do */
3800 return OK;
3801
3802 if (lp->ll_tv == NULL)
3803 {
3804 cc = *name_end;
3805 *name_end = NUL;
3806
3807 /* Normal name or expanded name. */
3808 if (check_changedtick(lp->ll_name))
3809 ret = FAIL;
3810 else
3811 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003812 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003813 if (di == NULL)
3814 ret = FAIL;
3815 else
3816 {
3817 if (lock)
3818 di->di_flags |= DI_FLAGS_LOCK;
3819 else
3820 di->di_flags &= ~DI_FLAGS_LOCK;
3821 item_lock(&di->di_tv, deep, lock);
3822 }
3823 }
3824 *name_end = cc;
3825 }
3826 else if (lp->ll_range)
3827 {
3828 listitem_T *li = lp->ll_li;
3829
3830 /* (un)lock a range of List items. */
3831 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3832 {
3833 item_lock(&li->li_tv, deep, lock);
3834 li = li->li_next;
3835 ++lp->ll_n1;
3836 }
3837 }
3838 else if (lp->ll_list != NULL)
3839 /* (un)lock a List item. */
3840 item_lock(&lp->ll_li->li_tv, deep, lock);
3841 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003842 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843 item_lock(&lp->ll_di->di_tv, deep, lock);
3844
3845 return ret;
3846}
3847
3848/*
3849 * Lock or unlock an item. "deep" is nr of levels to go.
3850 */
3851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003853{
3854 static int recurse = 0;
3855 list_T *l;
3856 listitem_T *li;
3857 dict_T *d;
3858 hashitem_T *hi;
3859 int todo;
3860
3861 if (recurse >= DICT_MAXNEST)
3862 {
3863 EMSG(_("E743: variable nested too deep for (un)lock"));
3864 return;
3865 }
3866 if (deep == 0)
3867 return;
3868 ++recurse;
3869
3870 /* lock/unlock the item itself */
3871 if (lock)
3872 tv->v_lock |= VAR_LOCKED;
3873 else
3874 tv->v_lock &= ~VAR_LOCKED;
3875
3876 switch (tv->v_type)
3877 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 case VAR_UNKNOWN:
3879 case VAR_NUMBER:
3880 case VAR_STRING:
3881 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003882 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003883 case VAR_FLOAT:
3884 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003885 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003886 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003887 break;
3888
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003889 case VAR_LIST:
3890 if ((l = tv->vval.v_list) != NULL)
3891 {
3892 if (lock)
3893 l->lv_lock |= VAR_LOCKED;
3894 else
3895 l->lv_lock &= ~VAR_LOCKED;
3896 if (deep < 0 || deep > 1)
3897 /* recursive: lock/unlock the items the List contains */
3898 for (li = l->lv_first; li != NULL; li = li->li_next)
3899 item_lock(&li->li_tv, deep - 1, lock);
3900 }
3901 break;
3902 case VAR_DICT:
3903 if ((d = tv->vval.v_dict) != NULL)
3904 {
3905 if (lock)
3906 d->dv_lock |= VAR_LOCKED;
3907 else
3908 d->dv_lock &= ~VAR_LOCKED;
3909 if (deep < 0 || deep > 1)
3910 {
3911 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003912 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3914 {
3915 if (!HASHITEM_EMPTY(hi))
3916 {
3917 --todo;
3918 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3919 }
3920 }
3921 }
3922 }
3923 }
3924 --recurse;
3925}
3926
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003928 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3929 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930 */
3931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933{
3934 return (tv->v_lock & VAR_LOCKED)
3935 || (tv->v_type == VAR_LIST
3936 && tv->vval.v_list != NULL
3937 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3938 || (tv->v_type == VAR_DICT
3939 && tv->vval.v_dict != NULL
3940 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3941}
3942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3944/*
3945 * Delete all "menutrans_" variables.
3946 */
3947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 {
3957 if (!HASHITEM_EMPTY(hi))
3958 {
3959 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3961 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 }
3963 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003964 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965}
3966#endif
3967
3968#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3969
3970/*
3971 * Local string buffer for the next two functions to store a variable name
3972 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3973 * get_user_var_name().
3974 */
3975
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003976static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977
3978static char_u *varnamebuf = NULL;
3979static int varnamebuflen = 0;
3980
3981/*
3982 * Function to concatenate a prefix and a variable name.
3983 */
3984 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003985cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 int len;
3988
3989 len = (int)STRLEN(name) + 3;
3990 if (len > varnamebuflen)
3991 {
3992 vim_free(varnamebuf);
3993 len += 10; /* some additional space */
3994 varnamebuf = alloc(len);
3995 if (varnamebuf == NULL)
3996 {
3997 varnamebuflen = 0;
3998 return NULL;
3999 }
4000 varnamebuflen = len;
4001 }
4002 *varnamebuf = prefix;
4003 varnamebuf[1] = ':';
4004 STRCPY(varnamebuf + 2, name);
4005 return varnamebuf;
4006}
4007
4008/*
4009 * Function given to ExpandGeneric() to obtain the list of user defined
4010 * (global/buffer/window/built-in) variable names.
4011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004013get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004015 static long_u gdone;
4016 static long_u bdone;
4017 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018#ifdef FEAT_WINDOWS
4019 static long_u tdone;
4020#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 static int vidx;
4022 static hashitem_T *hi;
4023 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028#ifdef FEAT_WINDOWS
4029 tdone = 0;
4030#endif
4031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* Global variables */
4034 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004036 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004038 else
4039 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 while (HASHITEM_EMPTY(hi))
4041 ++hi;
4042 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4043 return cat_prefix_varname('g', hi->hi_key);
4044 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004046
4047 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004048 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 else
4054 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004055 while (HASHITEM_EMPTY(hi))
4056 ++hi;
4057 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return (char_u *)"b:changedtick";
4063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004064
4065 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004069 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 else
4072 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004077
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_WINDOWS
4079 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004080 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004081 if (tdone < ht->ht_used)
4082 {
4083 if (tdone++ == 0)
4084 hi = ht->ht_array;
4085 else
4086 ++hi;
4087 while (HASHITEM_EMPTY(hi))
4088 ++hi;
4089 return cat_prefix_varname('t', hi->hi_key);
4090 }
4091#endif
4092
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 /* v: variables */
4094 if (vidx < VV_LEN)
4095 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097 vim_free(varnamebuf);
4098 varnamebuf = NULL;
4099 varnamebuflen = 0;
4100 return NULL;
4101}
4102
4103#endif /* FEAT_CMDL_COMPL */
4104
4105/*
4106 * types for expressions.
4107 */
4108typedef enum
4109{
4110 TYPE_UNKNOWN = 0
4111 , TYPE_EQUAL /* == */
4112 , TYPE_NEQUAL /* != */
4113 , TYPE_GREATER /* > */
4114 , TYPE_GEQUAL /* >= */
4115 , TYPE_SMALLER /* < */
4116 , TYPE_SEQUAL /* <= */
4117 , TYPE_MATCH /* =~ */
4118 , TYPE_NOMATCH /* !~ */
4119} exptype_T;
4120
4121/*
4122 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4125 */
4126
4127/*
4128 * Handle zero level expression.
4129 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004130 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004131 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 * Return OK or FAIL.
4133 */
4134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004135eval0(
4136 char_u *arg,
4137 typval_T *rettv,
4138 char_u **nextcmd,
4139 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 int ret;
4142 char_u *p;
4143
4144 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 if (ret == FAIL || !ends_excmd(*p))
4147 {
4148 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004149 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 /*
4151 * Report the invalid expression unless the expression evaluation has
4152 * been cancelled due to an aborting error, an interrupt, or an
4153 * exception.
4154 */
4155 if (!aborting())
4156 EMSG2(_(e_invexpr2), arg);
4157 ret = FAIL;
4158 }
4159 if (nextcmd != NULL)
4160 *nextcmd = check_nextcmd(p);
4161
4162 return ret;
4163}
4164
4165/*
4166 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004167 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 *
4169 * "arg" must point to the first non-white of the expression.
4170 * "arg" is advanced to the next non-white after the recognized expression.
4171 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004172 * Note: "rettv.v_lock" is not set.
4173 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * Return OK or FAIL.
4175 */
4176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004180 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 /*
4183 * Get the first variable.
4184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return FAIL;
4187
4188 if ((*arg)[0] == '?')
4189 {
4190 result = FALSE;
4191 if (evaluate)
4192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 int error = FALSE;
4194
4195 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 if (error)
4199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201
4202 /*
4203 * Get the second variable.
4204 */
4205 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 return FAIL;
4208
4209 /*
4210 * Check for the ":".
4211 */
4212 if ((*arg)[0] != ':')
4213 {
4214 EMSG(_("E109: Missing ':' after '?'"));
4215 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219
4220 /*
4221 * Get the third variable.
4222 */
4223 *arg = skipwhite(*arg + 1);
4224 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4225 {
4226 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229 }
4230 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233
4234 return OK;
4235}
4236
4237/*
4238 * Handle first level expression:
4239 * expr2 || expr2 || expr2 logical OR
4240 *
4241 * "arg" must point to the first non-white of the expression.
4242 * "arg" is advanced to the next non-white after the recognized expression.
4243 *
4244 * Return OK or FAIL.
4245 */
4246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
Bram Moolenaar33570922005-01-25 22:26:29 +00004249 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 long result;
4251 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004252 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 /*
4255 * Get the first variable.
4256 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259
4260 /*
4261 * Repeat until there is no following "||".
4262 */
4263 first = TRUE;
4264 result = FALSE;
4265 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4266 {
4267 if (evaluate && first)
4268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (error)
4273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 first = FALSE;
4275 }
4276
4277 /*
4278 * Get the second variable.
4279 */
4280 *arg = skipwhite(*arg + 2);
4281 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4282 return FAIL;
4283
4284 /*
4285 * Compute the result.
4286 */
4287 if (evaluate && !result)
4288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (error)
4293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295 if (evaluate)
4296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 rettv->v_type = VAR_NUMBER;
4298 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300 }
4301
4302 return OK;
4303}
4304
4305/*
4306 * Handle second level expression:
4307 * expr3 && expr3 && expr3 logical AND
4308 *
4309 * "arg" must point to the first non-white of the expression.
4310 * "arg" is advanced to the next non-white after the recognized expression.
4311 *
4312 * Return OK or FAIL.
4313 */
4314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004315eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 long result;
4319 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 /*
4329 * Repeat until there is no following "&&".
4330 */
4331 first = TRUE;
4332 result = TRUE;
4333 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4334 {
4335 if (evaluate && first)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 first = FALSE;
4343 }
4344
4345 /*
4346 * Get the second variable.
4347 */
4348 *arg = skipwhite(*arg + 2);
4349 if (eval4(arg, &var2, evaluate && result) == FAIL)
4350 return FAIL;
4351
4352 /*
4353 * Compute the result.
4354 */
4355 if (evaluate && result)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (error)
4361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 if (evaluate)
4364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 rettv->v_type = VAR_NUMBER;
4366 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 }
4369
4370 return OK;
4371}
4372
4373/*
4374 * Handle third level expression:
4375 * var1 == var2
4376 * var1 =~ var2
4377 * var1 != var2
4378 * var1 !~ var2
4379 * var1 > var2
4380 * var1 >= var2
4381 * var1 < var2
4382 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004383 * var1 is var2
4384 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 *
4386 * "arg" must point to the first non-white of the expression.
4387 * "arg" is advanced to the next non-white after the recognized expression.
4388 *
4389 * Return OK or FAIL.
4390 */
4391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004392eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
Bram Moolenaar33570922005-01-25 22:26:29 +00004394 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 char_u *p;
4396 int i;
4397 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004398 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int len = 2;
4400 long n1, n2;
4401 char_u *s1, *s2;
4402 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4403 regmatch_T regmatch;
4404 int ic;
4405 char_u *save_cpo;
4406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 p = *arg;
4414 switch (p[0])
4415 {
4416 case '=': if (p[1] == '=')
4417 type = TYPE_EQUAL;
4418 else if (p[1] == '~')
4419 type = TYPE_MATCH;
4420 break;
4421 case '!': if (p[1] == '=')
4422 type = TYPE_NEQUAL;
4423 else if (p[1] == '~')
4424 type = TYPE_NOMATCH;
4425 break;
4426 case '>': if (p[1] != '=')
4427 {
4428 type = TYPE_GREATER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_GEQUAL;
4433 break;
4434 case '<': if (p[1] != '=')
4435 {
4436 type = TYPE_SMALLER;
4437 len = 1;
4438 }
4439 else
4440 type = TYPE_SEQUAL;
4441 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 case 'i': if (p[1] == 's')
4443 {
4444 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4445 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004446 i = p[len];
4447 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 {
4449 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4450 type_is = TRUE;
4451 }
4452 }
4453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455
4456 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004457 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 */
4459 if (type != TYPE_UNKNOWN)
4460 {
4461 /* extra question mark appended: ignore case */
4462 if (p[len] == '?')
4463 {
4464 ic = TRUE;
4465 ++len;
4466 }
4467 /* extra '#' appended: match case */
4468 else if (p[len] == '#')
4469 {
4470 ic = FALSE;
4471 ++len;
4472 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 else
4475 ic = p_ic;
4476
4477 /*
4478 * Get the second variable.
4479 */
4480 *arg = skipwhite(p + len);
4481 if (eval5(arg, &var2, evaluate) == FAIL)
4482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004483 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return FAIL;
4485 }
4486
4487 if (evaluate)
4488 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 if (type_is && rettv->v_type != var2.v_type)
4490 {
4491 /* For "is" a different type always means FALSE, for "notis"
4492 * it means TRUE. */
4493 n1 = (type == TYPE_NEQUAL);
4494 }
4495 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4496 {
4497 if (type_is)
4498 {
4499 n1 = (rettv->v_type == var2.v_type
4500 && rettv->vval.v_list == var2.vval.v_list);
4501 if (type == TYPE_NEQUAL)
4502 n1 = !n1;
4503 }
4504 else if (rettv->v_type != var2.v_type
4505 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4506 {
4507 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004508 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004510 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 clear_tv(rettv);
4512 clear_tv(&var2);
4513 return FAIL;
4514 }
4515 else
4516 {
4517 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004518 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4519 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 }
4524
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004525 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4526 {
4527 if (type_is)
4528 {
4529 n1 = (rettv->v_type == var2.v_type
4530 && rettv->vval.v_dict == var2.vval.v_dict);
4531 if (type == TYPE_NEQUAL)
4532 n1 = !n1;
4533 }
4534 else if (rettv->v_type != var2.v_type
4535 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4536 {
4537 if (rettv->v_type != var2.v_type)
4538 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4539 else
4540 EMSG(_("E736: Invalid operation for Dictionary"));
4541 clear_tv(rettv);
4542 clear_tv(&var2);
4543 return FAIL;
4544 }
4545 else
4546 {
4547 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004548 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4549 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004550 if (type == TYPE_NEQUAL)
4551 n1 = !n1;
4552 }
4553 }
4554
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004555 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4556 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 {
4558 if (rettv->v_type != var2.v_type
4559 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4560 {
4561 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004562 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004564 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004565 clear_tv(rettv);
4566 clear_tv(&var2);
4567 return FAIL;
4568 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004569 else if (rettv->v_type == VAR_PARTIAL)
4570 {
4571 /* Partials are only equal when identical. */
4572 n1 = rettv->vval.v_partial != NULL
4573 && rettv->vval.v_partial == var2.vval.v_partial;
4574 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 else
4576 {
4577 /* Compare two Funcrefs for being equal or unequal. */
4578 if (rettv->vval.v_string == NULL
4579 || var2.vval.v_string == NULL)
4580 n1 = FALSE;
4581 else
4582 n1 = STRCMP(rettv->vval.v_string,
4583 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004585 if (type == TYPE_NEQUAL)
4586 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004587 }
4588
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004589#ifdef FEAT_FLOAT
4590 /*
4591 * If one of the two variables is a float, compare as a float.
4592 * When using "=~" or "!~", always compare as string.
4593 */
4594 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4595 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4596 {
4597 float_T f1, f2;
4598
4599 if (rettv->v_type == VAR_FLOAT)
4600 f1 = rettv->vval.v_float;
4601 else
4602 f1 = get_tv_number(rettv);
4603 if (var2.v_type == VAR_FLOAT)
4604 f2 = var2.vval.v_float;
4605 else
4606 f2 = get_tv_number(&var2);
4607 n1 = FALSE;
4608 switch (type)
4609 {
4610 case TYPE_EQUAL: n1 = (f1 == f2); break;
4611 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4612 case TYPE_GREATER: n1 = (f1 > f2); break;
4613 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4614 case TYPE_SMALLER: n1 = (f1 < f2); break;
4615 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4616 case TYPE_UNKNOWN:
4617 case TYPE_MATCH:
4618 case TYPE_NOMATCH: break; /* avoid gcc warning */
4619 }
4620 }
4621#endif
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 /*
4624 * If one of the two variables is a number, compare as a number.
4625 * When using "=~" or "!~", always compare as string.
4626 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004627 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004630 n1 = get_tv_number(rettv);
4631 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 switch (type)
4633 {
4634 case TYPE_EQUAL: n1 = (n1 == n2); break;
4635 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4636 case TYPE_GREATER: n1 = (n1 > n2); break;
4637 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4638 case TYPE_SMALLER: n1 = (n1 < n2); break;
4639 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4640 case TYPE_UNKNOWN:
4641 case TYPE_MATCH:
4642 case TYPE_NOMATCH: break; /* avoid gcc warning */
4643 }
4644 }
4645 else
4646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004647 s1 = get_tv_string_buf(rettv, buf1);
4648 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4650 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4651 else
4652 i = 0;
4653 n1 = FALSE;
4654 switch (type)
4655 {
4656 case TYPE_EQUAL: n1 = (i == 0); break;
4657 case TYPE_NEQUAL: n1 = (i != 0); break;
4658 case TYPE_GREATER: n1 = (i > 0); break;
4659 case TYPE_GEQUAL: n1 = (i >= 0); break;
4660 case TYPE_SMALLER: n1 = (i < 0); break;
4661 case TYPE_SEQUAL: n1 = (i <= 0); break;
4662
4663 case TYPE_MATCH:
4664 case TYPE_NOMATCH:
4665 /* avoid 'l' flag in 'cpoptions' */
4666 save_cpo = p_cpo;
4667 p_cpo = (char_u *)"";
4668 regmatch.regprog = vim_regcomp(s2,
4669 RE_MAGIC + RE_STRING);
4670 regmatch.rm_ic = ic;
4671 if (regmatch.regprog != NULL)
4672 {
4673 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004674 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (type == TYPE_NOMATCH)
4676 n1 = !n1;
4677 }
4678 p_cpo = save_cpo;
4679 break;
4680
4681 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4682 }
4683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004684 clear_tv(rettv);
4685 clear_tv(&var2);
4686 rettv->v_type = VAR_NUMBER;
4687 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
4689 }
4690
4691 return OK;
4692}
4693
4694/*
4695 * Handle fourth level expression:
4696 * + number addition
4697 * - number subtraction
4698 * . string concatenation
4699 *
4700 * "arg" must point to the first non-white of the expression.
4701 * "arg" is advanced to the next non-white after the recognized expression.
4702 *
4703 * Return OK or FAIL.
4704 */
4705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004706eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
Bram Moolenaar33570922005-01-25 22:26:29 +00004708 typval_T var2;
4709 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 int op;
4711 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004712#ifdef FEAT_FLOAT
4713 float_T f1 = 0, f2 = 0;
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 char_u *s1, *s2;
4716 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4717 char_u *p;
4718
4719 /*
4720 * Get the first variable.
4721 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004722 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return FAIL;
4724
4725 /*
4726 * Repeat computing, until no '+', '-' or '.' is following.
4727 */
4728 for (;;)
4729 {
4730 op = **arg;
4731 if (op != '+' && op != '-' && op != '.')
4732 break;
4733
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004734 if ((op != '+' || rettv->v_type != VAR_LIST)
4735#ifdef FEAT_FLOAT
4736 && (op == '.' || rettv->v_type != VAR_FLOAT)
4737#endif
4738 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004739 {
4740 /* For "list + ...", an illegal use of the first operand as
4741 * a number cannot be determined before evaluating the 2nd
4742 * operand: if this is also a list, all is ok.
4743 * For "something . ...", "something - ..." or "non-list + ...",
4744 * we know that the first operand needs to be a string or number
4745 * without evaluating the 2nd operand. So check before to avoid
4746 * side effects after an error. */
4747 if (evaluate && get_tv_string_chk(rettv) == NULL)
4748 {
4749 clear_tv(rettv);
4750 return FAIL;
4751 }
4752 }
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 /*
4755 * Get the second variable.
4756 */
4757 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004758 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return FAIL;
4762 }
4763
4764 if (evaluate)
4765 {
4766 /*
4767 * Compute the result.
4768 */
4769 if (op == '.')
4770 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004771 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4772 s2 = get_tv_string_buf_chk(&var2, buf2);
4773 if (s2 == NULL) /* type error ? */
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004779 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004784 else if (op == '+' && rettv->v_type == VAR_LIST
4785 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004786 {
4787 /* concatenate Lists */
4788 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4789 &var3) == FAIL)
4790 {
4791 clear_tv(rettv);
4792 clear_tv(&var2);
4793 return FAIL;
4794 }
4795 clear_tv(rettv);
4796 *rettv = var3;
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 else
4799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 f1 = rettv->vval.v_float;
4806 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 else
4809#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 n1 = get_tv_number_chk(rettv, &error);
4812 if (error)
4813 {
4814 /* This can only happen for "list + non-list". For
4815 * "non-list + ..." or "something - ...", we returned
4816 * before evaluating the 2nd operand. */
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (var2.v_type == VAR_FLOAT)
4822 f1 = n1;
4823#endif
4824 }
4825#ifdef FEAT_FLOAT
4826 if (var2.v_type == VAR_FLOAT)
4827 {
4828 f2 = var2.vval.v_float;
4829 n2 = 0;
4830 }
4831 else
4832#endif
4833 {
4834 n2 = get_tv_number_chk(&var2, &error);
4835 if (error)
4836 {
4837 clear_tv(rettv);
4838 clear_tv(&var2);
4839 return FAIL;
4840 }
4841#ifdef FEAT_FLOAT
4842 if (rettv->v_type == VAR_FLOAT)
4843 f2 = n2;
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004847
4848#ifdef FEAT_FLOAT
4849 /* If there is a float on either side the result is a float. */
4850 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4851 {
4852 if (op == '+')
4853 f1 = f1 + f2;
4854 else
4855 f1 = f1 - f2;
4856 rettv->v_type = VAR_FLOAT;
4857 rettv->vval.v_float = f1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860#endif
4861 {
4862 if (op == '+')
4863 n1 = n1 + n2;
4864 else
4865 n1 = n1 - n2;
4866 rettv->v_type = VAR_NUMBER;
4867 rettv->vval.v_number = n1;
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 return OK;
4874}
4875
4876/*
4877 * Handle fifth level expression:
4878 * * number multiplication
4879 * / number division
4880 * % number modulo
4881 *
4882 * "arg" must point to the first non-white of the expression.
4883 * "arg" is advanced to the next non-white after the recognized expression.
4884 *
4885 * Return OK or FAIL.
4886 */
4887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004888eval6(
4889 char_u **arg,
4890 typval_T *rettv,
4891 int evaluate,
4892 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
Bram Moolenaar33570922005-01-25 22:26:29 +00004894 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int op;
4896 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897#ifdef FEAT_FLOAT
4898 int use_float = FALSE;
4899 float_T f1 = 0, f2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 /*
4904 * Get the first variable.
4905 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004906 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 return FAIL;
4908
4909 /*
4910 * Repeat computing, until no '*', '/' or '%' is following.
4911 */
4912 for (;;)
4913 {
4914 op = **arg;
4915 if (op != '*' && op != '/' && op != '%')
4916 break;
4917
4918 if (evaluate)
4919 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#ifdef FEAT_FLOAT
4921 if (rettv->v_type == VAR_FLOAT)
4922 {
4923 f1 = rettv->vval.v_float;
4924 use_float = TRUE;
4925 n1 = 0;
4926 }
4927 else
4928#endif
4929 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004930 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004931 if (error)
4932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934 else
4935 n1 = 0;
4936
4937 /*
4938 * Get the second variable.
4939 */
4940 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 if (evaluate)
4945 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004946#ifdef FEAT_FLOAT
4947 if (var2.v_type == VAR_FLOAT)
4948 {
4949 if (!use_float)
4950 {
4951 f1 = n1;
4952 use_float = TRUE;
4953 }
4954 f2 = var2.vval.v_float;
4955 n2 = 0;
4956 }
4957 else
4958#endif
4959 {
4960 n2 = get_tv_number_chk(&var2, &error);
4961 clear_tv(&var2);
4962 if (error)
4963 return FAIL;
4964#ifdef FEAT_FLOAT
4965 if (use_float)
4966 f2 = n2;
4967#endif
4968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004972 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 if (op == '*')
4978 f1 = f1 * f2;
4979 else if (op == '/')
4980 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981# ifdef VMS
4982 /* VMS crashes on divide by zero, work around it */
4983 if (f2 == 0.0)
4984 {
4985 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004986 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004988 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004989 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004990 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004991 }
4992 else
4993 f1 = f1 / f2;
4994# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 /* We rely on the floating point library to handle divide
4996 * by zero to result in "inf" and not a crash. */
4997 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005002 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 return FAIL;
5004 }
5005 rettv->v_type = VAR_FLOAT;
5006 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 n1 = n1 * n2;
5013 else if (op == '/')
5014 {
5015 if (n2 == 0) /* give an error message? */
5016 {
5017 if (n1 == 0)
5018 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5019 else if (n1 < 0)
5020 n1 = -0x7fffffffL;
5021 else
5022 n1 = 0x7fffffffL;
5023 }
5024 else
5025 n1 = n1 / n2;
5026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 {
5029 if (n2 == 0) /* give an error message? */
5030 n1 = 0;
5031 else
5032 n1 = n1 % n2;
5033 }
5034 rettv->v_type = VAR_NUMBER;
5035 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039
5040 return OK;
5041}
5042
5043/*
5044 * Handle sixth level expression:
5045 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005046 * "string" string constant
5047 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * &option-name option value
5049 * @r register contents
5050 * identifier variable value
5051 * function() function call
5052 * $VAR environment variable
5053 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005054 * [expr, expr] List
5055 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *
5057 * Also handle:
5058 * ! in front logical NOT
5059 * - in front unary minus
5060 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * trailing [] subscript in String or List
5062 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *
5064 * "arg" must point to the first non-white of the expression.
5065 * "arg" is advanced to the next non-white after the recognized expression.
5066 *
5067 * Return OK or FAIL.
5068 */
5069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005070eval7(
5071 char_u **arg,
5072 typval_T *rettv,
5073 int evaluate,
5074 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 long n;
5077 int len;
5078 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *start_leader, *end_leader;
5080 int ret = OK;
5081 char_u *alias;
5082
5083 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
5090 * Skip '!' and '-' characters. They are handled later.
5091 */
5092 start_leader = *arg;
5093 while (**arg == '!' || **arg == '-' || **arg == '+')
5094 *arg = skipwhite(*arg + 1);
5095 end_leader = *arg;
5096
5097 switch (**arg)
5098 {
5099 /*
5100 * Number constant.
5101 */
5102 case '0':
5103 case '1':
5104 case '2':
5105 case '3':
5106 case '4':
5107 case '5':
5108 case '6':
5109 case '7':
5110 case '8':
5111 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005112 {
5113#ifdef FEAT_FLOAT
5114 char_u *p = skipdigits(*arg + 1);
5115 int get_float = FALSE;
5116
5117 /* We accept a float when the format matches
5118 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005119 * strict to avoid backwards compatibility problems.
5120 * Don't look for a float after the "." operator, so that
5121 * ":let vers = 1.2.3" doesn't fail. */
5122 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005124 get_float = TRUE;
5125 p = skipdigits(p + 2);
5126 if (*p == 'e' || *p == 'E')
5127 {
5128 ++p;
5129 if (*p == '-' || *p == '+')
5130 ++p;
5131 if (!vim_isdigit(*p))
5132 get_float = FALSE;
5133 else
5134 p = skipdigits(p + 1);
5135 }
5136 if (ASCII_ISALPHA(*p) || *p == '.')
5137 get_float = FALSE;
5138 }
5139 if (get_float)
5140 {
5141 float_T f;
5142
5143 *arg += string2float(*arg, &f);
5144 if (evaluate)
5145 {
5146 rettv->v_type = VAR_FLOAT;
5147 rettv->vval.v_float = f;
5148 }
5149 }
5150 else
5151#endif
5152 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005153 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 *arg += len;
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_NUMBER;
5158 rettv->vval.v_number = n;
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * String constant: "string".
5166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 break;
5169
5170 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005174 break;
5175
5176 /*
5177 * List: [expr, expr]
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 * Dictionary: {key: val, key: val}
5184 */
5185 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5186 break;
5187
5188 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005189 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005191 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193
5194 /*
5195 * Environment variable: $VAR.
5196 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Register contents: @r.
5202 */
5203 case '@': ++*arg;
5204 if (evaluate)
5205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005207 rettv->vval.v_string = get_reg_contents(**arg,
5208 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 if (**arg != NUL)
5211 ++*arg;
5212 break;
5213
5214 /*
5215 * nested expression: (expression).
5216 */
5217 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (**arg == ')')
5220 ++*arg;
5221 else if (ret == OK)
5222 {
5223 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 ret = FAIL;
5226 }
5227 break;
5228
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 if (ret == NOTDONE)
5234 {
5235 /*
5236 * Must be a variable or function name.
5237 * Can also be a curly-braces kind of name: {expr}.
5238 */
5239 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005240 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 if (alias != NULL)
5242 s = alias;
5243
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005244 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245 ret = FAIL;
5246 else
5247 {
5248 if (**arg == '(') /* recursive! */
5249 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005250 partial_T *partial;
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* If "s" is the name of a variable of type VAR_FUNC
5253 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005254 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255
5256 /* Invoke the function. */
5257 ret = get_func_tv(s, len, rettv, arg,
5258 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005259 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005260
5261 /* If evaluate is FALSE rettv->v_type was not set in
5262 * get_func_tv, but it's needed in handle_subscript() to parse
5263 * what follows. So set it here. */
5264 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5265 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005266 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005267 rettv->v_type = VAR_FUNC;
5268 }
5269
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 /* Stop the expression evaluation when immediately
5271 * aborting on error, or when an interrupt occurred or
5272 * an exception was thrown but not caught. */
5273 if (aborting())
5274 {
5275 if (ret == OK)
5276 clear_tv(rettv);
5277 ret = FAIL;
5278 }
5279 }
5280 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005281 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005282 else
5283 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005285 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 }
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 *arg = skipwhite(*arg);
5289
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005290 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5291 * expr(expr). */
5292 if (ret == OK)
5293 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /*
5296 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5297 */
5298 if (ret == OK && evaluate && end_leader > start_leader)
5299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005301 int val = 0;
5302#ifdef FEAT_FLOAT
5303 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005304
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 if (rettv->v_type == VAR_FLOAT)
5306 f = rettv->vval.v_float;
5307 else
5308#endif
5309 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 clear_tv(rettv);
5313 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 else
5316 {
5317 while (end_leader > start_leader)
5318 {
5319 --end_leader;
5320 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 {
5322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 f = !f;
5325 else
5326#endif
5327 val = !val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 {
5331#ifdef FEAT_FLOAT
5332 if (rettv->v_type == VAR_FLOAT)
5333 f = -f;
5334 else
5335#endif
5336 val = -val;
5337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339#ifdef FEAT_FLOAT
5340 if (rettv->v_type == VAR_FLOAT)
5341 {
5342 clear_tv(rettv);
5343 rettv->vval.v_float = f;
5344 }
5345 else
5346#endif
5347 {
5348 clear_tv(rettv);
5349 rettv->v_type = VAR_NUMBER;
5350 rettv->vval.v_number = val;
5351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354
5355 return ret;
5356}
5357
5358/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005359 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5360 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5362 */
5363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005364eval_index(
5365 char_u **arg,
5366 typval_T *rettv,
5367 int evaluate,
5368 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369{
5370 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005371 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005373 long len = -1;
5374 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005381 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005382 if (verbose)
5383 EMSG(_("E695: Cannot index a Funcref"));
5384 return FAIL;
5385 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005386#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 if (verbose)
5388 EMSG(_(e_float_as_string));
5389 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005390#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005391 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005392 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005393 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005394 if (verbose)
5395 EMSG(_("E909: Cannot index a special variable"));
5396 return FAIL;
5397 case VAR_UNKNOWN:
5398 if (evaluate)
5399 return FAIL;
5400 /* FALLTHROUGH */
5401
5402 case VAR_STRING:
5403 case VAR_NUMBER:
5404 case VAR_LIST:
5405 case VAR_DICT:
5406 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005407 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005409 init_tv(&var1);
5410 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413 /*
5414 * dict.name
5415 */
5416 key = *arg + 1;
5417 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5418 ;
5419 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005421 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425 /*
5426 * something[idx]
5427 *
5428 * Get the (first) variable from inside the [].
5429 */
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ':')
5432 empty1 = TRUE;
5433 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5434 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5436 {
5437 /* not a number or string */
5438 clear_tv(&var1);
5439 return FAIL;
5440 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441
5442 /*
5443 * Get the second variable from inside the [:].
5444 */
5445 if (**arg == ':')
5446 {
5447 range = TRUE;
5448 *arg = skipwhite(*arg + 1);
5449 if (**arg == ']')
5450 empty2 = TRUE;
5451 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5452 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005453 if (!empty1)
5454 clear_tv(&var1);
5455 return FAIL;
5456 }
5457 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5458 {
5459 /* not a number or string */
5460 if (!empty1)
5461 clear_tv(&var1);
5462 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 return FAIL;
5464 }
5465 }
5466
5467 /* Check for the ']'. */
5468 if (**arg != ']')
5469 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005470 if (verbose)
5471 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 clear_tv(&var1);
5473 if (range)
5474 clear_tv(&var2);
5475 return FAIL;
5476 }
5477 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479
5480 if (evaluate)
5481 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 n1 = 0;
5483 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 n1 = get_tv_number(&var1);
5486 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488 if (range)
5489 {
5490 if (empty2)
5491 n2 = -1;
5492 else
5493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 n2 = get_tv_number(&var2);
5495 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 }
5498
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005501 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005502 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005503 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005504 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005505 case VAR_SPECIAL:
5506 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005507 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 break; /* not evaluating, skipping over subscript */
5509
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 case VAR_NUMBER:
5511 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005512 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 len = (long)STRLEN(s);
5514 if (range)
5515 {
5516 /* The resulting variable is a substring. If the indexes
5517 * are out of range the result is empty. */
5518 if (n1 < 0)
5519 {
5520 n1 = len + n1;
5521 if (n1 < 0)
5522 n1 = 0;
5523 }
5524 if (n2 < 0)
5525 n2 = len + n2;
5526 else if (n2 >= len)
5527 n2 = len;
5528 if (n1 >= len || n2 < 0 || n1 > n2)
5529 s = NULL;
5530 else
5531 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5532 }
5533 else
5534 {
5535 /* The resulting variable is a string of a single
5536 * character. If the index is too big or negative the
5537 * result is empty. */
5538 if (n1 >= len || n1 < 0)
5539 s = NULL;
5540 else
5541 s = vim_strnsave(s + n1, 1);
5542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005543 clear_tv(rettv);
5544 rettv->v_type = VAR_STRING;
5545 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 break;
5547
5548 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 if (n1 < 0)
5551 n1 = len + n1;
5552 if (!empty1 && (n1 < 0 || n1 >= len))
5553 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 /* For a range we allow invalid values and return an empty
5555 * list. A list index out of range is an error. */
5556 if (!range)
5557 {
5558 if (verbose)
5559 EMSGN(_(e_listidx), n1);
5560 return FAIL;
5561 }
5562 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 }
5564 if (range)
5565 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005566 list_T *l;
5567 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568
5569 if (n2 < 0)
5570 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005571 else if (n2 >= len)
5572 n2 = len - 1;
5573 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005574 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 l = list_alloc();
5576 if (l == NULL)
5577 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 n1 <= n2; ++n1)
5580 {
5581 if (list_append_tv(l, &item->li_tv) == FAIL)
5582 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005583 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 return FAIL;
5585 }
5586 item = item->li_next;
5587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005588 clear_tv(rettv);
5589 rettv->v_type = VAR_LIST;
5590 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005591 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005592 }
5593 else
5594 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005595 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005596 clear_tv(rettv);
5597 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600
5601 case VAR_DICT:
5602 if (range)
5603 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (verbose)
5605 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 if (len == -1)
5607 clear_tv(&var1);
5608 return FAIL;
5609 }
5610 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005611 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612
5613 if (len == -1)
5614 {
5615 key = get_tv_string(&var1);
5616 if (*key == NUL)
5617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618 if (verbose)
5619 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 clear_tv(&var1);
5621 return FAIL;
5622 }
5623 }
5624
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005625 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005627 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005628 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 if (len == -1)
5630 clear_tv(&var1);
5631 if (item == NULL)
5632 return FAIL;
5633
5634 copy_tv(&item->di_tv, &var1);
5635 clear_tv(rettv);
5636 *rettv = var1;
5637 }
5638 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 }
5640 }
5641
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 return OK;
5643}
5644
5645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 * Get an option value.
5647 * "arg" points to the '&' or '+' before the option name.
5648 * "arg" is advanced to character after the option name.
5649 * Return OK or FAIL.
5650 */
5651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005652get_option_tv(
5653 char_u **arg,
5654 typval_T *rettv, /* when NULL, only check if option exists */
5655 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 char_u *option_end;
5658 long numval;
5659 char_u *stringval;
5660 int opt_type;
5661 int c;
5662 int working = (**arg == '+'); /* has("+option") */
5663 int ret = OK;
5664 int opt_flags;
5665
5666 /*
5667 * Isolate the option name and find its value.
5668 */
5669 option_end = find_option_end(arg, &opt_flags);
5670 if (option_end == NULL)
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 EMSG2(_("E112: Option name missing: %s"), *arg);
5674 return FAIL;
5675 }
5676
5677 if (!evaluate)
5678 {
5679 *arg = option_end;
5680 return OK;
5681 }
5682
5683 c = *option_end;
5684 *option_end = NUL;
5685 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688 if (opt_type == -3) /* invalid name */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 EMSG2(_("E113: Unknown option: %s"), *arg);
5692 ret = FAIL;
5693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 if (opt_type == -2) /* hidden string option */
5697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005698 rettv->v_type = VAR_STRING;
5699 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 else if (opt_type == -1) /* hidden number option */
5702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 rettv->v_type = VAR_NUMBER;
5704 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 else if (opt_type == 1) /* number option */
5707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708 rettv->v_type = VAR_NUMBER;
5709 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711 else /* string option */
5712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 rettv->v_type = VAR_STRING;
5714 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716 }
5717 else if (working && (opt_type == -2 || opt_type == -1))
5718 ret = FAIL;
5719
5720 *option_end = c; /* put back for error messages */
5721 *arg = option_end;
5722
5723 return ret;
5724}
5725
5726/*
5727 * Allocate a variable for a string constant.
5728 * Return OK or FAIL.
5729 */
5730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005731get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 char_u *p;
5734 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 int extra = 0;
5736
5737 /*
5738 * Find the end of the string, skipping backslashed characters.
5739 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005740 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742 if (*p == '\\' && p[1] != NUL)
5743 {
5744 ++p;
5745 /* A "\<x>" form occupies at least 4 characters, and produces up
5746 * to 6 characters: reserve space for 2 extra */
5747 if (*p == '<')
5748 extra += 2;
5749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751
5752 if (*p != '"')
5753 {
5754 EMSG2(_("E114: Missing quote: %s"), *arg);
5755 return FAIL;
5756 }
5757
5758 /* If only parsing, set *arg and return here */
5759 if (!evaluate)
5760 {
5761 *arg = p + 1;
5762 return OK;
5763 }
5764
5765 /*
5766 * Copy the string into allocated memory, handling backslashed
5767 * characters.
5768 */
5769 name = alloc((unsigned)(p - *arg + extra));
5770 if (name == NULL)
5771 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 rettv->v_type = VAR_STRING;
5773 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
5777 if (*p == '\\')
5778 {
5779 switch (*++p)
5780 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 case 'b': *name++ = BS; ++p; break;
5782 case 'e': *name++ = ESC; ++p; break;
5783 case 'f': *name++ = FF; ++p; break;
5784 case 'n': *name++ = NL; ++p; break;
5785 case 'r': *name++ = CAR; ++p; break;
5786 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 case 'X': /* hex: "\x1", "\x12" */
5789 case 'x':
5790 case 'u': /* Unicode: "\u0023" */
5791 case 'U':
5792 if (vim_isxdigit(p[1]))
5793 {
5794 int n, nr;
5795 int c = toupper(*p);
5796
5797 if (c == 'X')
5798 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005799 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005801 else
5802 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 nr = 0;
5804 while (--n >= 0 && vim_isxdigit(p[1]))
5805 {
5806 ++p;
5807 nr = (nr << 4) + hex2nr(*p);
5808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810#ifdef FEAT_MBYTE
5811 /* For "\u" store the number according to
5812 * 'encoding'. */
5813 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 else
5816#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820
5821 /* octal: "\1", "\12", "\123" */
5822 case '0':
5823 case '1':
5824 case '2':
5825 case '3':
5826 case '4':
5827 case '5':
5828 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 case '7': *name = *p++ - '0';
5830 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 *name = (*name << 3) + *p++ - '0';
5833 if (*p >= '0' && *p <= '7')
5834 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 break;
5838
5839 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (extra != 0)
5842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 break;
5845 }
5846 /* FALLTHROUGH */
5847
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 break;
5850 }
5851 }
5852 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 *arg = p + 1;
5858
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 return OK;
5860}
5861
5862/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 * Return OK or FAIL.
5865 */
5866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 int reduce = 0;
5872
5873 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 */
5876 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5877 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 break;
5882 ++reduce;
5883 ++p;
5884 }
5885 }
5886
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 return FAIL;
5891 }
5892
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 if (!evaluate)
5895 {
5896 *arg = p + 1;
5897 return OK;
5898 }
5899
5900 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 */
5903 str = alloc((unsigned)((p - *arg) - reduce));
5904 if (str == NULL)
5905 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 rettv->v_type = VAR_STRING;
5907 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 break;
5915 ++p;
5916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 *arg = p + 1;
5921
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return OK;
5923}
5924
5925/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005926 * Allocate a variable for a List and fill it from "*arg".
5927 * Return OK or FAIL.
5928 */
5929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005930get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 list_T *l = NULL;
5933 typval_T tv;
5934 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935
5936 if (evaluate)
5937 {
5938 l = list_alloc();
5939 if (l == NULL)
5940 return FAIL;
5941 }
5942
5943 *arg = skipwhite(*arg + 1);
5944 while (**arg != ']' && **arg != NUL)
5945 {
5946 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5947 goto failret;
5948 if (evaluate)
5949 {
5950 item = listitem_alloc();
5951 if (item != NULL)
5952 {
5953 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005954 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 list_append(l, item);
5956 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005957 else
5958 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960
5961 if (**arg == ']')
5962 break;
5963 if (**arg != ',')
5964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 goto failret;
5967 }
5968 *arg = skipwhite(*arg + 1);
5969 }
5970
5971 if (**arg != ']')
5972 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974failret:
5975 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005976 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 return FAIL;
5978 }
5979
5980 *arg = skipwhite(*arg + 1);
5981 if (evaluate)
5982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005983 rettv->v_type = VAR_LIST;
5984 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 ++l->lv_refcount;
5986 }
5987
5988 return OK;
5989}
5990
5991/*
5992 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005993 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005995 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005998 list_T *l;
5999
6000 l = (list_T *)alloc_clear(sizeof(list_T));
6001 if (l != NULL)
6002 {
6003 /* Prepend the list to the list of lists for garbage collection. */
6004 if (first_list != NULL)
6005 first_list->lv_used_prev = l;
6006 l->lv_used_prev = NULL;
6007 l->lv_used_next = first_list;
6008 first_list = l;
6009 }
6010 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011}
6012
6013/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006014 * Allocate an empty list for a return value.
6015 * Returns OK or FAIL.
6016 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006019{
6020 list_T *l = list_alloc();
6021
6022 if (l == NULL)
6023 return FAIL;
6024
6025 rettv->vval.v_list = l;
6026 rettv->v_type = VAR_LIST;
6027 ++l->lv_refcount;
6028 return OK;
6029}
6030
6031/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 * Unreference a list: decrement the reference count and free it when it
6033 * becomes zero.
6034 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006036list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006038 if (l != NULL && --l->lv_refcount <= 0)
6039 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040}
6041
6042/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006043 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 * Ignores the reference count.
6045 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006047list_free(
6048 list_T *l,
6049 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050{
Bram Moolenaar33570922005-01-25 22:26:29 +00006051 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006053 /* Remove the list from the list of lists for garbage collection. */
6054 if (l->lv_used_prev == NULL)
6055 first_list = l->lv_used_next;
6056 else
6057 l->lv_used_prev->lv_used_next = l->lv_used_next;
6058 if (l->lv_used_next != NULL)
6059 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6060
Bram Moolenaard9fba312005-06-26 22:34:35 +00006061 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006063 /* Remove the item before deleting it. */
6064 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006065 if (recurse || (item->li_tv.v_type != VAR_LIST
6066 && item->li_tv.v_type != VAR_DICT))
6067 clear_tv(&item->li_tv);
6068 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 }
6070 vim_free(l);
6071}
6072
6073/*
6074 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006075 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006077 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006078listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079{
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081}
6082
6083/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006084 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006089 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 vim_free(item);
6091}
6092
6093/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006094 * Remove a list item from a List and free it. Also clears the value.
6095 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006098{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006099 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006100 listitem_free(item);
6101}
6102
6103/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006104 * Get the number of items in a list.
6105 */
6106 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109 if (l == NULL)
6110 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006111 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112}
6113
6114/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 * Return TRUE when two lists have exactly the same values.
6116 */
6117 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118list_equal(
6119 list_T *l1,
6120 list_T *l2,
6121 int ic, /* ignore case for strings */
6122 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123{
Bram Moolenaar33570922005-01-25 22:26:29 +00006124 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006126 if (l1 == NULL || l2 == NULL)
6127 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006128 if (l1 == l2)
6129 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006130 if (list_len(l1) != list_len(l2))
6131 return FALSE;
6132
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 for (item1 = l1->lv_first, item2 = l2->lv_first;
6134 item1 != NULL && item2 != NULL;
6135 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006136 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137 return FALSE;
6138 return item1 == NULL && item2 == NULL;
6139}
6140
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006141/*
6142 * Return the dictitem that an entry in a hashtable points to.
6143 */
6144 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006145dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006146{
6147 return HI2DI(hi);
6148}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006149
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006150/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151 * Return TRUE when two dictionaries have exactly the same key/values.
6152 */
6153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006154dict_equal(
6155 dict_T *d1,
6156 dict_T *d2,
6157 int ic, /* ignore case for strings */
6158 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159{
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 hashitem_T *hi;
6161 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006164 if (d1 == NULL || d2 == NULL)
6165 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006166 if (d1 == d2)
6167 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006168 if (dict_len(d1) != dict_len(d2))
6169 return FALSE;
6170
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006171 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006173 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 if (!HASHITEM_EMPTY(hi))
6175 {
6176 item2 = dict_find(d2, hi->hi_key, -1);
6177 if (item2 == NULL)
6178 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006179 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006180 return FALSE;
6181 --todo;
6182 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006183 }
6184 return TRUE;
6185}
6186
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187static int tv_equal_recurse_limit;
6188
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006189/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006190 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006191 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006192 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 */
6194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006195tv_equal(
6196 typval_T *tv1,
6197 typval_T *tv2,
6198 int ic, /* ignore case */
6199 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200{
6201 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006202 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006204 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006208
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006209 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006210 * recursiveness to a limit. We guess they are equal then.
6211 * A fixed limit has the problem of still taking an awful long time.
6212 * Reduce the limit every time running into it. That should work fine for
6213 * deeply linked structures that are not recursively linked and catch
6214 * recursiveness quickly. */
6215 if (!recursive)
6216 tv_equal_recurse_limit = 1000;
6217 if (recursive_cnt >= tv_equal_recurse_limit)
6218 {
6219 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006220 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006221 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006224 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006225 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006226 ++recursive_cnt;
6227 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6228 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006229 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230
6231 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006232 ++recursive_cnt;
6233 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, 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_FUNC:
6238 return (tv1->vval.v_string != NULL
6239 && tv2->vval.v_string != NULL
6240 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6241
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006242 case VAR_PARTIAL:
6243 return tv1->vval.v_partial != NULL
6244 && tv1->vval.v_partial == tv2->vval.v_partial;
6245
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246 case VAR_NUMBER:
6247 return tv1->vval.v_number == tv2->vval.v_number;
6248
6249 case VAR_STRING:
6250 s1 = get_tv_string_buf(tv1, buf1);
6251 s2 = get_tv_string_buf(tv2, buf2);
6252 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006253
6254 case VAR_SPECIAL:
6255 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006256
6257 case VAR_FLOAT:
6258#ifdef FEAT_FLOAT
6259 return tv1->vval.v_float == tv2->vval.v_float;
6260#endif
6261 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006262#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006263 return tv1->vval.v_job == tv2->vval.v_job;
6264#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006265 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006266#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006267 return tv1->vval.v_channel == tv2->vval.v_channel;
6268#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006269 case VAR_UNKNOWN:
6270 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006272
Bram Moolenaara03f2332016-02-06 18:09:59 +01006273 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6274 * does not equal anything, not even itself. */
6275 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276}
6277
6278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 * Locate item with index "n" in list "l" and return it.
6280 * A negative index is counted from the end; -1 is the last item.
6281 * Returns NULL when "n" is out of range.
6282 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006283 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006284list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285{
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006287 long idx;
6288
6289 if (l == NULL)
6290 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291
6292 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006294 n = l->lv_len + n;
6295
6296 /* Check for index out of range. */
6297 if (n < 0 || n >= l->lv_len)
6298 return NULL;
6299
6300 /* When there is a cached index may start search from there. */
6301 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 if (n < l->lv_idx / 2)
6304 {
6305 /* closest to the start of the list */
6306 item = l->lv_first;
6307 idx = 0;
6308 }
6309 else if (n > (l->lv_idx + l->lv_len) / 2)
6310 {
6311 /* closest to the end of the list */
6312 item = l->lv_last;
6313 idx = l->lv_len - 1;
6314 }
6315 else
6316 {
6317 /* closest to the cached index */
6318 item = l->lv_idx_item;
6319 idx = l->lv_idx;
6320 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006321 }
6322 else
6323 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006324 if (n < l->lv_len / 2)
6325 {
6326 /* closest to the start of the list */
6327 item = l->lv_first;
6328 idx = 0;
6329 }
6330 else
6331 {
6332 /* closest to the end of the list */
6333 item = l->lv_last;
6334 idx = l->lv_len - 1;
6335 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006337
6338 while (n > idx)
6339 {
6340 /* search forward */
6341 item = item->li_next;
6342 ++idx;
6343 }
6344 while (n < idx)
6345 {
6346 /* search backward */
6347 item = item->li_prev;
6348 --idx;
6349 }
6350
6351 /* cache the used index */
6352 l->lv_idx = idx;
6353 l->lv_idx_item = item;
6354
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006355 return item;
6356}
6357
6358/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006359 * Get list item "l[idx]" as a number.
6360 */
6361 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362list_find_nr(
6363 list_T *l,
6364 long idx,
6365 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006366{
6367 listitem_T *li;
6368
6369 li = list_find(l, idx);
6370 if (li == NULL)
6371 {
6372 if (errorp != NULL)
6373 *errorp = TRUE;
6374 return -1L;
6375 }
6376 return get_tv_number_chk(&li->li_tv, errorp);
6377}
6378
6379/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006380 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6381 */
6382 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006384{
6385 listitem_T *li;
6386
6387 li = list_find(l, idx - 1);
6388 if (li == NULL)
6389 {
6390 EMSGN(_(e_listidx), idx);
6391 return NULL;
6392 }
6393 return get_tv_string(&li->li_tv);
6394}
6395
6396/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006397 * Locate "item" list "l" and return its index.
6398 * Returns -1 when "item" is not in the list.
6399 */
6400 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006402{
6403 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006405
6406 if (l == NULL)
6407 return -1;
6408 idx = 0;
6409 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6410 ++idx;
6411 if (li == NULL)
6412 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006413 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006414}
6415
6416/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 * Append item "item" to the end of list "l".
6418 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421{
6422 if (l->lv_last == NULL)
6423 {
6424 /* empty list */
6425 l->lv_first = item;
6426 l->lv_last = item;
6427 item->li_prev = NULL;
6428 }
6429 else
6430 {
6431 l->lv_last->li_next = item;
6432 item->li_prev = l->lv_last;
6433 l->lv_last = item;
6434 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 item->li_next = NULL;
6437}
6438
6439/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006441 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006443 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006446 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447
Bram Moolenaar05159a02005-02-26 23:04:13 +00006448 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006450 copy_tv(tv, &li->li_tv);
6451 list_append(l, li);
6452 return OK;
6453}
6454
6455/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006456 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006457 * Return FAIL when out of memory.
6458 */
6459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006461{
6462 listitem_T *li = listitem_alloc();
6463
6464 if (li == NULL)
6465 return FAIL;
6466 li->li_tv.v_type = VAR_DICT;
6467 li->li_tv.v_lock = 0;
6468 li->li_tv.vval.v_dict = dict;
6469 list_append(list, li);
6470 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471 return OK;
6472}
6473
6474/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006477 * Returns FAIL when out of memory.
6478 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006480list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481{
6482 listitem_T *li = listitem_alloc();
6483
6484 if (li == NULL)
6485 return FAIL;
6486 list_append(l, li);
6487 li->li_tv.v_type = VAR_STRING;
6488 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006489 if (str == NULL)
6490 li->li_tv.vval.v_string = NULL;
6491 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006492 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006493 return FAIL;
6494 return OK;
6495}
6496
6497/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006498 * Append "n" to list "l".
6499 * Returns FAIL when out of memory.
6500 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006502list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006503{
6504 listitem_T *li;
6505
6506 li = listitem_alloc();
6507 if (li == NULL)
6508 return FAIL;
6509 li->li_tv.v_type = VAR_NUMBER;
6510 li->li_tv.v_lock = 0;
6511 li->li_tv.vval.v_number = n;
6512 list_append(l, li);
6513 return OK;
6514}
6515
6516/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 * If "item" is NULL append at the end.
6519 * Return FAIL when out of memory.
6520 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523{
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525
6526 if (ni == NULL)
6527 return FAIL;
6528 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006529 list_insert(l, ni, item);
6530 return OK;
6531}
6532
6533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006535{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 if (item == NULL)
6537 /* Append new item at end of list. */
6538 list_append(l, ni);
6539 else
6540 {
6541 /* Insert new item before existing item. */
6542 ni->li_prev = item->li_prev;
6543 ni->li_next = item;
6544 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006547 ++l->lv_idx;
6548 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006550 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006552 l->lv_idx_item = NULL;
6553 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006555 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557}
6558
6559/*
6560 * Extend "l1" with "l2".
6561 * If "bef" is NULL append at the end, otherwise insert before this item.
6562 * Returns FAIL when out of memory.
6563 */
6564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006568 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006570 /* We also quit the loop when we have inserted the original item count of
6571 * the list, avoid a hang when we extend a list with itself. */
6572 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6574 return FAIL;
6575 return OK;
6576}
6577
6578/*
6579 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6580 * Return FAIL when out of memory.
6581 */
6582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006587 if (l1 == NULL || l2 == NULL)
6588 return FAIL;
6589
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 if (l == NULL)
6593 return FAIL;
6594 tv->v_type = VAR_LIST;
6595 tv->vval.v_list = l;
6596
6597 /* append all items from the second list */
6598 return list_extend(l, l2, NULL);
6599}
6600
6601/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 * Returns NULL when out of memory.
6606 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006607 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609{
Bram Moolenaar33570922005-01-25 22:26:29 +00006610 list_T *copy;
6611 listitem_T *item;
6612 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613
6614 if (orig == NULL)
6615 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616
6617 copy = list_alloc();
6618 if (copy != NULL)
6619 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 if (copyID != 0)
6621 {
6622 /* Do this before adding the items, because one of the items may
6623 * refer back to this list. */
6624 orig->lv_copyID = copyID;
6625 orig->lv_copylist = copy;
6626 }
6627 for (item = orig->lv_first; item != NULL && !got_int;
6628 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 {
6630 ni = listitem_alloc();
6631 if (ni == NULL)
6632 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006633 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 {
6635 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6636 {
6637 vim_free(ni);
6638 break;
6639 }
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006642 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 list_append(copy, ni);
6644 }
6645 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006646 if (item != NULL)
6647 {
6648 list_unref(copy);
6649 copy = NULL;
6650 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 }
6652
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 return copy;
6654}
6655
6656/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006658 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006659 * This used to be called list_remove, but that conflicts with a Sun header
6660 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664{
Bram Moolenaar33570922005-01-25 22:26:29 +00006665 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 /* notify watchers */
6668 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006670 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006671 list_fix_watch(l, ip);
6672 if (ip == item2)
6673 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006675
6676 if (item2->li_next == NULL)
6677 l->lv_last = item->li_prev;
6678 else
6679 item2->li_next->li_prev = item->li_prev;
6680 if (item->li_prev == NULL)
6681 l->lv_first = item2->li_next;
6682 else
6683 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006684 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685}
6686
6687/*
6688 * Return an allocated string with the string representation of a list.
6689 * May return NULL.
6690 */
6691 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693{
6694 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695
6696 if (tv->vval.v_list == NULL)
6697 return NULL;
6698 ga_init2(&ga, (int)sizeof(char), 80);
6699 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006700 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006701 {
6702 vim_free(ga.ga_data);
6703 return NULL;
6704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705 ga_append(&ga, ']');
6706 ga_append(&ga, NUL);
6707 return (char_u *)ga.ga_data;
6708}
6709
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006710typedef struct join_S {
6711 char_u *s;
6712 char_u *tofree;
6713} join_T;
6714
6715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716list_join_inner(
6717 garray_T *gap, /* to store the result in */
6718 list_T *l,
6719 char_u *sep,
6720 int echo_style,
6721 int copyID,
6722 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006723{
6724 int i;
6725 join_T *p;
6726 int len;
6727 int sumlen = 0;
6728 int first = TRUE;
6729 char_u *tofree;
6730 char_u numbuf[NUMBUFLEN];
6731 listitem_T *item;
6732 char_u *s;
6733
6734 /* Stringify each item in the list. */
6735 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6736 {
6737 if (echo_style)
6738 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6739 else
6740 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6741 if (s == NULL)
6742 return FAIL;
6743
6744 len = (int)STRLEN(s);
6745 sumlen += len;
6746
Bram Moolenaarcde88542015-08-11 19:14:00 +02006747 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006748 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6749 if (tofree != NULL || s != numbuf)
6750 {
6751 p->s = s;
6752 p->tofree = tofree;
6753 }
6754 else
6755 {
6756 p->s = vim_strnsave(s, len);
6757 p->tofree = p->s;
6758 }
6759
6760 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006761 if (did_echo_string_emsg) /* recursion error, bail out */
6762 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 }
6764
6765 /* Allocate result buffer with its total size, avoid re-allocation and
6766 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6767 if (join_gap->ga_len >= 2)
6768 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6769 if (ga_grow(gap, sumlen + 2) == FAIL)
6770 return FAIL;
6771
6772 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6773 {
6774 if (first)
6775 first = FALSE;
6776 else
6777 ga_concat(gap, sep);
6778 p = ((join_T *)join_gap->ga_data) + i;
6779
6780 if (p->s != NULL)
6781 ga_concat(gap, p->s);
6782 line_breakcheck();
6783 }
6784
6785 return OK;
6786}
6787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006790 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006791 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006794list_join(
6795 garray_T *gap,
6796 list_T *l,
6797 char_u *sep,
6798 int echo_style,
6799 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006801 garray_T join_ga;
6802 int retval;
6803 join_T *p;
6804 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805
Bram Moolenaard39a7512015-04-16 22:51:22 +02006806 if (l->lv_len < 1)
6807 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006808 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6809 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6810
6811 /* Dispose each item in join_ga. */
6812 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006813 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = (join_T *)join_ga.ga_data;
6815 for (i = 0; i < join_ga.ga_len; ++i)
6816 {
6817 vim_free(p->tofree);
6818 ++p;
6819 }
6820 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822
6823 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006824}
6825
6826/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006827 * Return the next (unique) copy ID.
6828 * Used for serializing nested structures.
6829 */
6830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006832{
6833 current_copyID += COPYID_INC;
6834 return current_copyID;
6835}
6836
6837/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 * Garbage collection for lists and dictionaries.
6839 *
6840 * We use reference counts to be able to free most items right away when they
6841 * are no longer used. But for composite items it's possible that it becomes
6842 * unused while the reference count is > 0: When there is a recursive
6843 * reference. Example:
6844 * :let l = [1, 2, 3]
6845 * :let d = {9: l}
6846 * :let l[1] = d
6847 *
6848 * Since this is quite unusual we handle this with garbage collection: every
6849 * once in a while find out which lists and dicts are not referenced from any
6850 * variable.
6851 *
6852 * Here is a good reference text about garbage collection (refers to Python
6853 * but it applies to all reference-counting mechanisms):
6854 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006855 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006856
6857/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 * Do garbage collection for lists and dicts.
6859 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006862garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006863{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006865 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 buf_T *buf;
6867 win_T *wp;
6868 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006869 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006870 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006872#ifdef FEAT_WINDOWS
6873 tabpage_T *tp;
6874#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006876 /* Only do this once. */
6877 want_garbage_collect = FALSE;
6878 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006879 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006880
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 /* We advance by two because we add one for items referenced through
6882 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006883 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
6886 * 1. Go through all accessible variables and mark all lists and dicts
6887 * with copyID.
6888 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889
6890 /* Don't free variables in the previous_funccal list unless they are only
6891 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006892 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6894 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6896 NULL);
6897 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6898 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 }
6900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /* script-local variables */
6902 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006903 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904
6905 /* buffer-local variables */
6906 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6908 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
6910 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006911 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6913 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006914#ifdef FEAT_AUTOCMD
6915 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6917 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006918#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920#ifdef FEAT_WINDOWS
6921 /* tabpage-local variables */
6922 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6924 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006925#endif
6926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929
6930 /* function-local variables */
6931 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6932 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6934 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 }
6936
Bram Moolenaard812df62008-11-09 12:46:09 +00006937 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006939
Bram Moolenaar1dced572012-04-05 16:54:08 +02006940#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006942#endif
6943
Bram Moolenaardb913952012-06-29 12:54:53 +02006944#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006946#endif
6947
6948#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006950#endif
6951
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006952#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006953 abort = abort || set_ref_in_channel(copyID);
6954#endif
6955
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 /*
6959 * 2. Free lists and dictionaries that are not referenced.
6960 */
6961 did_free = free_unref_items(copyID);
6962
6963 /*
6964 * 3. Check if any funccal can be freed now.
6965 */
6966 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 if (can_free_funccal(*pfc, copyID))
6969 {
6970 fc = *pfc;
6971 *pfc = fc->caller;
6972 free_funccal(fc, TRUE);
6973 did_free = TRUE;
6974 did_free_funccal = TRUE;
6975 }
6976 else
6977 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 if (did_free_funccal)
6980 /* When a funccal was freed some more items might be garbage
6981 * collected, so run again. */
6982 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006984 else if (p_verbose > 0)
6985 {
6986 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6987 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988
6989 return did_free;
6990}
6991
6992/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006993 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 */
6995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006996free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 dict_T *dd, *dd_next;
6999 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 int did_free = FALSE;
7001
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 */
7005 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007006 {
7007 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007008 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007010 /* Free the Dictionary and ordinary items it contains, but don't
7011 * recurse into Lists and Dictionaries, they will be in the list
7012 * of dicts or list of lists. */
7013 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007016 dd = dd_next;
7017 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018
7019 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007020 * Go through the list of lists and free items without the copyID.
7021 * But don't free a list that has a watcher (used in a for loop), these
7022 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
7024 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007025 {
7026 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007027 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7028 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007030 /* Free the List and ordinary items it contains, but don't recurse
7031 * into Lists and Dictionaries, they will be in the list of dicts
7032 * or list of lists. */
7033 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007036 ll = ll_next;
7037 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007038
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 return did_free;
7040}
7041
7042/*
7043 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 * "list_stack" is used to add lists to be marked. Can be NULL.
7045 *
7046 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007049set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050{
7051 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 hashtab_T *cur_ht;
7055 ht_stack_T *ht_stack = NULL;
7056 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 cur_ht = ht;
7059 for (;;)
7060 {
7061 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 /* Mark each item in the hashtab. If the item contains a hashtab
7064 * it is added to ht_stack, if it contains a list it is added to
7065 * list_stack. */
7066 todo = (int)cur_ht->ht_used;
7067 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7068 if (!HASHITEM_EMPTY(hi))
7069 {
7070 --todo;
7071 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7072 &ht_stack, list_stack);
7073 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075
7076 if (ht_stack == NULL)
7077 break;
7078
7079 /* take an item from the stack */
7080 cur_ht = ht_stack->ht;
7081 tempitem = ht_stack;
7082 ht_stack = ht_stack->prev;
7083 free(tempitem);
7084 }
7085
7086 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087}
7088
7089/*
7090 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7092 *
7093 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007098 listitem_T *li;
7099 int abort = FALSE;
7100 list_T *cur_l;
7101 list_stack_T *list_stack = NULL;
7102 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 cur_l = l;
7105 for (;;)
7106 {
7107 if (!abort)
7108 /* Mark each item in the list. If the item contains a hashtab
7109 * it is added to ht_stack, if it contains a list it is added to
7110 * list_stack. */
7111 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7112 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7113 ht_stack, &list_stack);
7114 if (list_stack == NULL)
7115 break;
7116
7117 /* take an item from the stack */
7118 cur_l = list_stack->list;
7119 tempitem = list_stack;
7120 list_stack = list_stack->prev;
7121 free(tempitem);
7122 }
7123
7124 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007125}
7126
7127/*
7128 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 * "list_stack" is used to add lists to be marked. Can be NULL.
7130 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7131 *
7132 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007135set_ref_in_item(
7136 typval_T *tv,
7137 int copyID,
7138 ht_stack_T **ht_stack,
7139 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007140{
7141 dict_T *dd;
7142 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007146 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007147 dd = tv->vval.v_dict;
7148 if (dd != NULL && dd->dv_copyID != copyID)
7149 {
7150 /* Didn't see this dict yet. */
7151 dd->dv_copyID = copyID;
7152 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007154 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7155 }
7156 else
7157 {
7158 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7159 if (newitem == NULL)
7160 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 else
7162 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 newitem->ht = &dd->dv_hashtab;
7164 newitem->prev = *ht_stack;
7165 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007168 }
7169 }
7170 else if (tv->v_type == VAR_LIST)
7171 {
7172 ll = tv->vval.v_list;
7173 if (ll != NULL && ll->lv_copyID != copyID)
7174 {
7175 /* Didn't see this list yet. */
7176 ll->lv_copyID = copyID;
7177 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007178 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007179 abort = set_ref_in_list(ll, copyID, ht_stack);
7180 }
7181 else
7182 {
7183 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007184 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007185 if (newitem == NULL)
7186 abort = TRUE;
7187 else
7188 {
7189 newitem->list = ll;
7190 newitem->prev = *list_stack;
7191 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007194 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007195 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007197}
7198
7199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200 * Allocate an empty header for a dictionary.
7201 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007202 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007203dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204{
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007206
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007208 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007209 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007210 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 if (first_dict != NULL)
7212 first_dict->dv_used_prev = d;
7213 d->dv_used_next = first_dict;
7214 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007215 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216
Bram Moolenaar33570922005-01-25 22:26:29 +00007217 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007218 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007219 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007220 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007222 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224}
7225
7226/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007227 * Allocate an empty dict for a return value.
7228 * Returns OK or FAIL.
7229 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007230 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007231rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007232{
7233 dict_T *d = dict_alloc();
7234
7235 if (d == NULL)
7236 return FAIL;
7237
7238 rettv->vval.v_dict = d;
7239 rettv->v_type = VAR_DICT;
7240 ++d->dv_refcount;
7241 return OK;
7242}
7243
7244
7245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 * Unreference a Dictionary: decrement the reference count and free it when it
7247 * becomes zero.
7248 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007252 if (d != NULL && --d->dv_refcount <= 0)
7253 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254}
7255
7256/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007257 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 * Ignores the reference count.
7259 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261dict_free(
7262 dict_T *d,
7263 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007267 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269 /* Remove the dict from the list of dicts for garbage collection. */
7270 if (d->dv_used_prev == NULL)
7271 first_dict = d->dv_used_next;
7272 else
7273 d->dv_used_prev->dv_used_next = d->dv_used_next;
7274 if (d->dv_used_next != NULL)
7275 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7276
7277 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007278 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007279 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007280 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if (!HASHITEM_EMPTY(hi))
7283 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 /* Remove the item before deleting it, just in case there is
7285 * something recursive causing trouble. */
7286 di = HI2DI(hi);
7287 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007288 if (recurse || (di->di_tv.v_type != VAR_LIST
7289 && di->di_tv.v_type != VAR_DICT))
7290 clear_tv(&di->di_tv);
7291 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 --todo;
7293 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 vim_free(d);
7297}
7298
7299/*
7300 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 * The "key" is copied to the new item.
7302 * Note that the value of the item "di_tv" still needs to be initialized!
7303 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007305 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007306dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307{
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007310 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007314 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007315 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007317}
7318
7319/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007320 * Make a copy of a Dictionary item.
7321 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007323dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007324{
Bram Moolenaar33570922005-01-25 22:26:29 +00007325 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007327 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7328 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007329 if (di != NULL)
7330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007332 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007333 copy_tv(&org->di_tv, &di->di_tv);
7334 }
7335 return di;
7336}
7337
7338/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 * Remove item "item" from Dictionary "dict" and free it.
7340 */
7341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343{
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 if (HASHITEM_EMPTY(hi))
7348 EMSG2(_(e_intern2), "dictitem_remove()");
7349 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 dictitem_free(item);
7352}
7353
7354/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 * Free a dict item. Also clears the value.
7356 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007358dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007361 if (item->di_flags & DI_FLAGS_ALLOC)
7362 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363}
7364
7365/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7367 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 * Returns NULL when out of memory.
7370 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007371 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007372dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373{
Bram Moolenaar33570922005-01-25 22:26:29 +00007374 dict_T *copy;
7375 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007376 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378
7379 if (orig == NULL)
7380 return NULL;
7381
7382 copy = dict_alloc();
7383 if (copy != NULL)
7384 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 if (copyID != 0)
7386 {
7387 orig->dv_copyID = copyID;
7388 orig->dv_copydict = copy;
7389 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007390 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 --todo;
7396
7397 di = dictitem_alloc(hi->hi_key);
7398 if (di == NULL)
7399 break;
7400 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007401 {
7402 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7403 copyID) == FAIL)
7404 {
7405 vim_free(di);
7406 break;
7407 }
7408 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 else
7410 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7411 if (dict_add(copy, di) == FAIL)
7412 {
7413 dictitem_free(di);
7414 break;
7415 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007420 if (todo > 0)
7421 {
7422 dict_unref(copy);
7423 copy = NULL;
7424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 }
7426
7427 return copy;
7428}
7429
7430/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007432 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436{
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438}
7439
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007441 * Add a number or string entry to dictionary "d".
7442 * When "str" is NULL use number "nr", otherwise use "str".
7443 * Returns FAIL when out of memory and when key already exists.
7444 */
7445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007446dict_add_nr_str(
7447 dict_T *d,
7448 char *key,
7449 long nr,
7450 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007451{
7452 dictitem_T *item;
7453
7454 item = dictitem_alloc((char_u *)key);
7455 if (item == NULL)
7456 return FAIL;
7457 item->di_tv.v_lock = 0;
7458 if (str == NULL)
7459 {
7460 item->di_tv.v_type = VAR_NUMBER;
7461 item->di_tv.vval.v_number = nr;
7462 }
7463 else
7464 {
7465 item->di_tv.v_type = VAR_STRING;
7466 item->di_tv.vval.v_string = vim_strsave(str);
7467 }
7468 if (dict_add(d, item) == FAIL)
7469 {
7470 dictitem_free(item);
7471 return FAIL;
7472 }
7473 return OK;
7474}
7475
7476/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007477 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007478 * Returns FAIL when out of memory and when key already exists.
7479 */
7480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007482{
7483 dictitem_T *item;
7484
7485 item = dictitem_alloc((char_u *)key);
7486 if (item == NULL)
7487 return FAIL;
7488 item->di_tv.v_lock = 0;
7489 item->di_tv.v_type = VAR_LIST;
7490 item->di_tv.vval.v_list = list;
7491 if (dict_add(d, item) == FAIL)
7492 {
7493 dictitem_free(item);
7494 return FAIL;
7495 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007496 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007497 return OK;
7498}
7499
7500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 * Get the number of items in a Dictionary.
7502 */
7503 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007504dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007505{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 if (d == NULL)
7507 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007508 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007509}
7510
7511/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 * Find item "key[len]" in Dictionary "d".
7513 * If "len" is negative use strlen(key).
7514 * Returns NULL when not found.
7515 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007516 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007517dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519#define AKEYLEN 200
7520 char_u buf[AKEYLEN];
7521 char_u *akey;
7522 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 if (len < 0)
7526 akey = key;
7527 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007528 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 tofree = akey = vim_strnsave(key, len);
7530 if (akey == NULL)
7531 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 else
7534 {
7535 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007536 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007537 akey = buf;
7538 }
7539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 vim_free(tofree);
7542 if (HASHITEM_EMPTY(hi))
7543 return NULL;
7544 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545}
7546
7547/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007548 * Get a string item from a dictionary.
7549 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550 * Returns NULL if the entry doesn't exist or out of memory.
7551 */
7552 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007553get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007554{
7555 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007556 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007557
7558 di = dict_find(d, key, -1);
7559 if (di == NULL)
7560 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007561 s = get_tv_string(&di->di_tv);
7562 if (save && s != NULL)
7563 s = vim_strsave(s);
7564 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007565}
7566
7567/*
7568 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007569 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007570 */
7571 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007572get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007573{
7574 dictitem_T *di;
7575
7576 di = dict_find(d, key, -1);
7577 if (di == NULL)
7578 return 0;
7579 return get_tv_number(&di->di_tv);
7580}
7581
7582/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 * Return an allocated string with the string representation of a Dictionary.
7584 * May return NULL.
7585 */
7586 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007587dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588{
7589 garray_T ga;
7590 int first = TRUE;
7591 char_u *tofree;
7592 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 return NULL;
7600 ga_init2(&ga, (int)sizeof(char), 80);
7601 ga_append(&ga, '{');
7602
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007604 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007608 --todo;
7609
7610 if (first)
7611 first = FALSE;
7612 else
7613 ga_concat(&ga, (char_u *)", ");
7614
7615 tofree = string_quote(hi->hi_key, FALSE);
7616 if (tofree != NULL)
7617 {
7618 ga_concat(&ga, tofree);
7619 vim_free(tofree);
7620 }
7621 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 if (s != NULL)
7624 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007626 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007627 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007628 line_breakcheck();
7629
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 if (todo > 0)
7633 {
7634 vim_free(ga.ga_data);
7635 return NULL;
7636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637
7638 ga_append(&ga, '}');
7639 ga_append(&ga, NUL);
7640 return (char_u *)ga.ga_data;
7641}
7642
7643/*
7644 * Allocate a variable for a Dictionary and fill it from "*arg".
7645 * Return OK or FAIL. Returns NOTDONE for {expr}.
7646 */
7647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007648get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649{
Bram Moolenaar33570922005-01-25 22:26:29 +00007650 dict_T *d = NULL;
7651 typval_T tvkey;
7652 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007653 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 /*
7659 * First check if it's not a curly-braces thing: {expr}.
7660 * Must do this without evaluating, otherwise a function may be called
7661 * twice. Unfortunately this means we need to call eval1() twice for the
7662 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007663 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007665 if (*start != '}')
7666 {
7667 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7668 return FAIL;
7669 if (*start == '}')
7670 return NOTDONE;
7671 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672
7673 if (evaluate)
7674 {
7675 d = dict_alloc();
7676 if (d == NULL)
7677 return FAIL;
7678 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 tvkey.v_type = VAR_UNKNOWN;
7680 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 *arg = skipwhite(*arg + 1);
7683 while (**arg != '}' && **arg != NUL)
7684 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 goto failret;
7687 if (**arg != ':')
7688 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007689 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 goto failret;
7692 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007693 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007695 key = get_tv_string_buf_chk(&tvkey, buf);
7696 if (key == NULL || *key == NUL)
7697 {
7698 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7699 if (key != NULL)
7700 EMSG(_(e_emptykey));
7701 clear_tv(&tvkey);
7702 goto failret;
7703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705
7706 *arg = skipwhite(*arg + 1);
7707 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7708 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007709 if (evaluate)
7710 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 goto failret;
7712 }
7713 if (evaluate)
7714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 if (item != NULL)
7717 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007718 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007719 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720 clear_tv(&tv);
7721 goto failret;
7722 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007723 item = dictitem_alloc(key);
7724 clear_tv(&tvkey);
7725 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007728 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 if (dict_add(d, item) == FAIL)
7730 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731 }
7732 }
7733
7734 if (**arg == '}')
7735 break;
7736 if (**arg != ',')
7737 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007738 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 goto failret;
7740 }
7741 *arg = skipwhite(*arg + 1);
7742 }
7743
7744 if (**arg != '}')
7745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007746 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747failret:
7748 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007749 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 return FAIL;
7751 }
7752
7753 *arg = skipwhite(*arg + 1);
7754 if (evaluate)
7755 {
7756 rettv->v_type = VAR_DICT;
7757 rettv->vval.v_dict = d;
7758 ++d->dv_refcount;
7759 }
7760
7761 return OK;
7762}
7763
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007764#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007765#endif
7766
Bram Moolenaar17a13432016-01-24 14:22:10 +01007767 static char *
7768get_var_special_name(int nr)
7769{
7770 switch (nr)
7771 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007772 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007773 case VVAL_TRUE: return "v:true";
7774 case VVAL_NONE: return "v:none";
7775 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007776 }
7777 EMSG2(_(e_intern2), "get_var_special_name()");
7778 return "42";
7779}
7780
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007782 * Return a string with the string representation of a variable.
7783 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007784 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007786 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007787 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 */
7789 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790echo_string(
7791 typval_T *tv,
7792 char_u **tofree,
7793 char_u *numbuf,
7794 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007796 static int recurse = 0;
7797 char_u *r = NULL;
7798
Bram Moolenaar33570922005-01-25 22:26:29 +00007799 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007800 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007801 if (!did_echo_string_emsg)
7802 {
7803 /* Only give this message once for a recursive call to avoid
7804 * flooding the user with errors. And stop iterating over lists
7805 * and dicts. */
7806 did_echo_string_emsg = TRUE;
7807 EMSG(_("E724: variable nested too deep for displaying"));
7808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007809 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007810 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007811 }
7812 ++recurse;
7813
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 switch (tv->v_type)
7815 {
7816 case VAR_FUNC:
7817 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007818 r = tv->vval.v_string;
7819 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007820
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007821 case VAR_PARTIAL:
7822 *tofree = NULL;
7823 /* TODO: arguments */
7824 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7825 break;
7826
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007828 if (tv->vval.v_list == NULL)
7829 {
7830 *tofree = NULL;
7831 r = NULL;
7832 }
7833 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7834 {
7835 *tofree = NULL;
7836 r = (char_u *)"[...]";
7837 }
7838 else
7839 {
7840 tv->vval.v_list->lv_copyID = copyID;
7841 *tofree = list2string(tv, copyID);
7842 r = *tofree;
7843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847 if (tv->vval.v_dict == NULL)
7848 {
7849 *tofree = NULL;
7850 r = NULL;
7851 }
7852 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7853 {
7854 *tofree = NULL;
7855 r = (char_u *)"{...}";
7856 }
7857 else
7858 {
7859 tv->vval.v_dict->dv_copyID = copyID;
7860 *tofree = dict2string(tv, copyID);
7861 r = *tofree;
7862 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007865 case VAR_STRING:
7866 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007867 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007868 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007869 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 *tofree = NULL;
7871 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007875#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007876 *tofree = NULL;
7877 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7878 r = numbuf;
7879 break;
7880#endif
7881
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007882 case VAR_SPECIAL:
7883 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007884 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007885 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007887
Bram Moolenaar8502c702014-06-17 12:51:16 +02007888 if (--recurse == 0)
7889 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891}
7892
7893/*
7894 * Return a string with the string representation of a variable.
7895 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7896 * "numbuf" is used for a number.
7897 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007898 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007899 */
7900 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901tv2string(
7902 typval_T *tv,
7903 char_u **tofree,
7904 char_u *numbuf,
7905 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906{
7907 switch (tv->v_type)
7908 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007909 case VAR_FUNC:
7910 *tofree = string_quote(tv->vval.v_string, TRUE);
7911 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007912 case VAR_PARTIAL:
7913 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7914 : tv->vval.v_partial->pt_name, TRUE);
7915 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007916 case VAR_STRING:
7917 *tofree = string_quote(tv->vval.v_string, FALSE);
7918 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007920#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007921 *tofree = NULL;
7922 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7923 return numbuf;
7924#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007925 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007928 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007929 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007930 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007931 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007933 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007934 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007935}
7936
7937/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 * Return string "str" in ' quotes, doubling ' characters.
7939 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007941 */
7942 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007943string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944{
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 char_u *p, *r, *s;
7947
Bram Moolenaar33570922005-01-25 22:26:29 +00007948 len = (function ? 13 : 3);
7949 if (str != NULL)
7950 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007951 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007952 for (p = str; *p != NUL; mb_ptr_adv(p))
7953 if (*p == '\'')
7954 ++len;
7955 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007956 s = r = alloc(len);
7957 if (r != NULL)
7958 {
7959 if (function)
7960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 r += 10;
7963 }
7964 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 if (str != NULL)
7967 for (p = str; *p != NUL; )
7968 {
7969 if (*p == '\'')
7970 *r++ = '\'';
7971 MB_COPY_CHAR(p, r);
7972 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974 if (function)
7975 *r++ = ')';
7976 *r++ = NUL;
7977 }
7978 return s;
7979}
7980
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007981#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007982/*
7983 * Convert the string "text" to a floating point number.
7984 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7985 * this always uses a decimal point.
7986 * Returns the length of the text that was consumed.
7987 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007988 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007989string2float(
7990 char_u *text,
7991 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992{
7993 char *s = (char *)text;
7994 float_T f;
7995
7996 f = strtod(s, &s);
7997 *value = f;
7998 return (int)((char_u *)s - text);
7999}
8000#endif
8001
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 * Get the value of an environment variable.
8004 * "arg" is pointing to the '$'. It is advanced to after the name.
8005 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008006 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 */
8008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008009get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010{
8011 char_u *string = NULL;
8012 int len;
8013 int cc;
8014 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008015 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 ++*arg;
8018 name = *arg;
8019 len = get_env_len(arg);
8020 if (evaluate)
8021 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008023 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008024
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008025 cc = name[len];
8026 name[len] = NUL;
8027 /* first try vim_getenv(), fast for normal environment vars */
8028 string = vim_getenv(name, &mustfree);
8029 if (string != NULL && *string != NUL)
8030 {
8031 if (!mustfree)
8032 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008034 else
8035 {
8036 if (mustfree)
8037 vim_free(string);
8038
8039 /* next try expanding things like $VIM and ${HOME} */
8040 string = expand_env_save(name - 1);
8041 if (string != NULL && *string == '$')
8042 {
8043 vim_free(string);
8044 string = NULL;
8045 }
8046 }
8047 name[len] = cc;
8048
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008049 rettv->v_type = VAR_STRING;
8050 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052
8053 return OK;
8054}
8055
8056/*
8057 * Array with names and number of arguments of all internal functions
8058 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8059 */
8060static struct fst
8061{
8062 char *f_name; /* function name */
8063 char f_min_argc; /* minimal number of arguments */
8064 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008065 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008066 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067} functions[] =
8068{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008069#ifdef FEAT_FLOAT
8070 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008071 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008073 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008074 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008075 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"append", 2, 2, f_append},
8077 {"argc", 0, 0, f_argc},
8078 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008079 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008080 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008081#ifdef FEAT_FLOAT
8082 {"asin", 1, 1, f_asin}, /* WJMc */
8083#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008084 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008085 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008086 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008087 {"assert_false", 1, 2, f_assert_false},
8088 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008089#ifdef FEAT_FLOAT
8090 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008091 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008094 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"bufexists", 1, 1, f_bufexists},
8096 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8097 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8098 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8099 {"buflisted", 1, 1, f_buflisted},
8100 {"bufloaded", 1, 1, f_bufloaded},
8101 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008102 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"bufwinnr", 1, 1, f_bufwinnr},
8104 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008105 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008106 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008107 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"ceil", 1, 1, f_ceil},
8110#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008111#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008112 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008113 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8114 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008115 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008116 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008117 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008118 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008119 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008120 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008121 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008122 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8123 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008124 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008125 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008126#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008127 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008128 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008130 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008132#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008133 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008134 {"complete_add", 1, 1, f_complete_add},
8135 {"complete_check", 0, 0, f_complete_check},
8136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#ifdef FEAT_FLOAT
8140 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008141 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008142#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008143 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008145 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008146 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008147 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008149 {"diff_filler", 1, 1, f_diff_filler},
8150 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008151 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008152 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"eventhandler", 0, 0, f_eventhandler},
8156 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008157 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008159#ifdef FEAT_FLOAT
8160 {"exp", 1, 1, f_exp},
8161#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008162 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008163 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008164 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8166 {"filereadable", 1, 1, f_filereadable},
8167 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008168 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008169 {"finddir", 1, 3, f_finddir},
8170 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171#ifdef FEAT_FLOAT
8172 {"float2nr", 1, 1, f_float2nr},
8173 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008174 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008176 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"fnamemodify", 2, 2, f_fnamemodify},
8178 {"foldclosed", 1, 1, f_foldclosed},
8179 {"foldclosedend", 1, 1, f_foldclosedend},
8180 {"foldlevel", 1, 1, f_foldlevel},
8181 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008182 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008184 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008185 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008186 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008187 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008188 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"getchar", 0, 1, f_getchar},
8190 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008191 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"getcmdline", 0, 0, f_getcmdline},
8193 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008194 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008195 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008196 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008197 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008198 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008199 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getfsize", 1, 1, f_getfsize},
8201 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008202 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008203 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008204 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008205 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008206 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008207 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008208 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008209 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008211 {"gettabvar", 2, 3, f_gettabvar},
8212 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 {"getwinposx", 0, 0, f_getwinposx},
8214 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008215 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008216 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008217 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008218 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008220 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008221 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008222 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8224 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8225 {"histadd", 2, 2, f_histadd},
8226 {"histdel", 1, 2, f_histdel},
8227 {"histget", 1, 2, f_histget},
8228 {"histnr", 1, 1, f_histnr},
8229 {"hlID", 1, 1, f_hlID},
8230 {"hlexists", 1, 1, f_hlexists},
8231 {"hostname", 0, 0, f_hostname},
8232 {"iconv", 3, 3, f_iconv},
8233 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008234 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008235 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008237 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"inputrestore", 0, 0, f_inputrestore},
8239 {"inputsave", 0, 0, f_inputsave},
8240 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008242 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008244 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008245#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8246 {"isnan", 1, 1, f_isnan},
8247#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008248 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008249#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008250 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008251 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008252 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008253 {"job_start", 1, 2, f_job_start},
8254 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008255 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008256#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008257 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008258 {"js_decode", 1, 1, f_js_decode},
8259 {"js_encode", 1, 1, f_js_encode},
8260 {"json_decode", 1, 1, f_json_decode},
8261 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008262 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008264 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"libcall", 3, 3, f_libcall},
8266 {"libcallnr", 3, 3, f_libcallnr},
8267 {"line", 1, 1, f_line},
8268 {"line2byte", 1, 1, f_line2byte},
8269 {"lispindent", 1, 1, f_lispindent},
8270 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273 {"log10", 1, 1, f_log10},
8274#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008275#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008276 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008277#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008278 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008279 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008280 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008281 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008282 {"matchadd", 2, 5, f_matchadd},
8283 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008284 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008285 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008286 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008287 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008288 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008289 {"max", 1, 1, f_max},
8290 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008291#ifdef vim_mkdir
8292 {"mkdir", 1, 3, f_mkdir},
8293#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008295#ifdef FEAT_MZSCHEME
8296 {"mzeval", 1, 1, f_mzeval},
8297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008299 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008300 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008301 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008302#ifdef FEAT_PERL
8303 {"perleval", 1, 1, f_perleval},
8304#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008305#ifdef FEAT_FLOAT
8306 {"pow", 2, 2, f_pow},
8307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008309 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008310 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008311#ifdef FEAT_PYTHON3
8312 {"py3eval", 1, 1, f_py3eval},
8313#endif
8314#ifdef FEAT_PYTHON
8315 {"pyeval", 1, 1, f_pyeval},
8316#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008317 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008318 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008319 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008320#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008321 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008322#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008323 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"remote_expr", 2, 3, f_remote_expr},
8325 {"remote_foreground", 1, 1, f_remote_foreground},
8326 {"remote_peek", 1, 2, f_remote_peek},
8327 {"remote_read", 1, 1, f_remote_read},
8328 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008329 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008331 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008333 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
8335 {"round", 1, 1, f_round},
8336#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008337 {"screenattr", 2, 2, f_screenattr},
8338 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008339 {"screencol", 0, 0, f_screencol},
8340 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008341 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008342 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008343 {"searchpair", 3, 7, f_searchpair},
8344 {"searchpairpos", 3, 7, f_searchpairpos},
8345 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 {"server2client", 2, 2, f_server2client},
8347 {"serverlist", 0, 0, f_serverlist},
8348 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008349 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008351 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008353 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008354 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008355 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008356 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008358 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008359 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008361#ifdef FEAT_CRYPT
8362 {"sha256", 1, 1, f_sha256},
8363#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008364 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008365 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367#ifdef FEAT_FLOAT
8368 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008369 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008371 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008372 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008373 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008374 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008375 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008376#ifdef FEAT_FLOAT
8377 {"sqrt", 1, 1, f_sqrt},
8378 {"str2float", 1, 1, f_str2float},
8379#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008380 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008381 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008382 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#ifdef HAVE_STRFTIME
8384 {"strftime", 1, 2, f_strftime},
8385#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008386 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008387 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"strlen", 1, 1, f_strlen},
8389 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008390 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008392 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008393 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"substitute", 4, 4, f_substitute},
8395 {"synID", 3, 3, f_synID},
8396 {"synIDattr", 2, 3, f_synIDattr},
8397 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008398 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008399 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008400 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008401 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008402 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008403 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008404 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008405 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008406 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008407#ifdef FEAT_FLOAT
8408 {"tan", 1, 1, f_tan},
8409 {"tanh", 1, 1, f_tanh},
8410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008412 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008413#ifdef FEAT_TIMERS
8414 {"timer_start", 2, 3, f_timer_start},
8415 {"timer_stop", 1, 1, f_timer_stop},
8416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"tolower", 1, 1, f_tolower},
8418 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008419 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008420#ifdef FEAT_FLOAT
8421 {"trunc", 1, 1, f_trunc},
8422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008424 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008425 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008426 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008427 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"virtcol", 1, 1, f_virtcol},
8429 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008430 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008431 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008432 {"win_getid", 0, 2, f_win_getid},
8433 {"win_gotoid", 1, 1, f_win_gotoid},
8434 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8435 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"winbufnr", 1, 1, f_winbufnr},
8437 {"wincol", 0, 0, f_wincol},
8438 {"winheight", 1, 1, f_winheight},
8439 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008440 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008442 {"winrestview", 1, 1, f_winrestview},
8443 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008445 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008446 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008447 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448};
8449
8450#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8451
8452/*
8453 * Function given to ExpandGeneric() to obtain the list of internal
8454 * or user defined function names.
8455 */
8456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008457get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 static int intidx = -1;
8460 char_u *name;
8461
8462 if (idx == 0)
8463 intidx = -1;
8464 if (intidx < 0)
8465 {
8466 name = get_user_func_name(xp, idx);
8467 if (name != NULL)
8468 return name;
8469 }
8470 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8471 {
8472 STRCPY(IObuff, functions[intidx].f_name);
8473 STRCAT(IObuff, "(");
8474 if (functions[intidx].f_max_argc == 0)
8475 STRCAT(IObuff, ")");
8476 return IObuff;
8477 }
8478
8479 return NULL;
8480}
8481
8482/*
8483 * Function given to ExpandGeneric() to obtain the list of internal or
8484 * user defined variable or function names.
8485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008487get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488{
8489 static int intidx = -1;
8490 char_u *name;
8491
8492 if (idx == 0)
8493 intidx = -1;
8494 if (intidx < 0)
8495 {
8496 name = get_function_name(xp, idx);
8497 if (name != NULL)
8498 return name;
8499 }
8500 return get_user_var_name(xp, ++intidx);
8501}
8502
8503#endif /* FEAT_CMDL_COMPL */
8504
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008505#if defined(EBCDIC) || defined(PROTO)
8506/*
8507 * Compare struct fst by function name.
8508 */
8509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008510compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008511{
8512 struct fst *p1 = (struct fst *)s1;
8513 struct fst *p2 = (struct fst *)s2;
8514
8515 return STRCMP(p1->f_name, p2->f_name);
8516}
8517
8518/*
8519 * Sort the function table by function name.
8520 * The sorting of the table above is ASCII dependant.
8521 * On machines using EBCDIC we have to sort it.
8522 */
8523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008524sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008525{
8526 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8527
8528 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8529}
8530#endif
8531
8532
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533/*
8534 * Find internal function in table above.
8535 * Return index, or -1 if not found
8536 */
8537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008538find_internal_func(
8539 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 int first = 0;
8542 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8543 int cmp;
8544 int x;
8545
8546 /*
8547 * Find the function name in the table. Binary search.
8548 */
8549 while (first <= last)
8550 {
8551 x = first + ((unsigned)(last - first) >> 1);
8552 cmp = STRCMP(name, functions[x].f_name);
8553 if (cmp < 0)
8554 last = x - 1;
8555 else if (cmp > 0)
8556 first = x + 1;
8557 else
8558 return x;
8559 }
8560 return -1;
8561}
8562
8563/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008564 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8565 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008566 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8567 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008568 */
8569 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008570deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008571{
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008573 int cc;
8574
Bram Moolenaar65639032016-03-16 21:40:30 +01008575 if (partialp != NULL)
8576 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008577
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008578 cc = name[*lenp];
8579 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008580 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008584 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008585 {
8586 *lenp = 0;
8587 return (char_u *)""; /* just in case */
8588 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008589 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 }
8592
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008593 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8594 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008595 partial_T *pt = v->di_tv.vval.v_partial;
8596
8597 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008598 {
8599 *lenp = 0;
8600 return (char_u *)""; /* just in case */
8601 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008602 if (partialp != NULL)
8603 *partialp = pt;
8604 *lenp = (int)STRLEN(pt->pt_name);
8605 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008606 }
8607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008608 return name;
8609}
8610
8611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 * Allocate a variable for the result of a function.
8613 * Return OK or FAIL.
8614 */
8615 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008616get_func_tv(
8617 char_u *name, /* name of the function */
8618 int len, /* length of "name" */
8619 typval_T *rettv,
8620 char_u **arg, /* argument, pointing to the '(' */
8621 linenr_T firstline, /* first line of range */
8622 linenr_T lastline, /* last line of range */
8623 int *doesrange, /* return: function handled range */
8624 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008625 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008626 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 char_u *argp;
8629 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008630 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 int argcount = 0; /* number of arguments found */
8632
8633 /*
8634 * Get the arguments.
8635 */
8636 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008637 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {
8639 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8640 if (*argp == ')' || *argp == ',' || *argp == NUL)
8641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8643 {
8644 ret = FAIL;
8645 break;
8646 }
8647 ++argcount;
8648 if (*argp != ',')
8649 break;
8650 }
8651 if (*argp == ')')
8652 ++argp;
8653 else
8654 ret = FAIL;
8655
8656 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008658 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 {
8661 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008662 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008664 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
8667 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008668 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670 *arg = skipwhite(argp);
8671 return ret;
8672}
8673
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008674#define ERROR_UNKNOWN 0
8675#define ERROR_TOOMANY 1
8676#define ERROR_TOOFEW 2
8677#define ERROR_SCRIPT 3
8678#define ERROR_DICT 4
8679#define ERROR_NONE 5
8680#define ERROR_OTHER 6
8681#define FLEN_FIXED 40
8682
8683/*
8684 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8685 * Change <SNR>123_name() to K_SNR 123_name().
8686 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8687 * (slow).
8688 */
8689 static char_u *
8690fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8691{
8692 int llen;
8693 char_u *fname;
8694 int i;
8695
8696 llen = eval_fname_script(name);
8697 if (llen > 0)
8698 {
8699 fname_buf[0] = K_SPECIAL;
8700 fname_buf[1] = KS_EXTRA;
8701 fname_buf[2] = (int)KE_SNR;
8702 i = 3;
8703 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8704 {
8705 if (current_SID <= 0)
8706 *error = ERROR_SCRIPT;
8707 else
8708 {
8709 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8710 i = (int)STRLEN(fname_buf);
8711 }
8712 }
8713 if (i + STRLEN(name + llen) < FLEN_FIXED)
8714 {
8715 STRCPY(fname_buf + i, name + llen);
8716 fname = fname_buf;
8717 }
8718 else
8719 {
8720 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8721 if (fname == NULL)
8722 *error = ERROR_OTHER;
8723 else
8724 {
8725 *tofree = fname;
8726 mch_memmove(fname, fname_buf, (size_t)i);
8727 STRCPY(fname + i, name + llen);
8728 }
8729 }
8730 }
8731 else
8732 fname = name;
8733 return fname;
8734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736/*
8737 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008738 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008739 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008742call_func(
8743 char_u *funcname, /* name of the function */
8744 int len, /* length of "name" */
8745 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008746 int argcount_in, /* number of "argvars" */
8747 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008748 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008749 linenr_T firstline, /* first line of range */
8750 linenr_T lastline, /* last line of range */
8751 int *doesrange, /* return: function handled range */
8752 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008753 partial_T *partial, /* optional, can be NULL */
8754 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 int error = ERROR_NONE;
8758 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008761 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008763 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008764 int argcount = argcount_in;
8765 typval_T *argvars = argvars_in;
8766 dict_T *selfdict = selfdict_in;
8767 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8768 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769
8770 /* Make a copy of the name, if it comes from a funcref variable it could
8771 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008772 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008773 if (name == NULL)
8774 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008776 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
8778 *doesrange = FALSE;
8779
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008780 if (partial != NULL)
8781 {
8782 if (partial->pt_dict != NULL)
8783 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008784 /* When the function has a partial with a dict and there is a dict
8785 * argument, use the dict argument. That is backwards compatible.
8786 */
8787 if (selfdict_in == NULL)
8788 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008789 }
8790 if (error == ERROR_NONE && partial->pt_argc > 0)
8791 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008792 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8793 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8794 for (i = 0; i < argcount_in; ++i)
8795 argv[i + argv_clear] = argvars_in[i];
8796 argvars = argv;
8797 argcount = partial->pt_argc + argcount_in;
8798 }
8799 }
8800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
8802 /* execute the function if no errors detected and executing */
8803 if (evaluate && error == ERROR_NONE)
8804 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008805 char_u *rfname = fname;
8806
8807 /* Ignore "g:" before a function name. */
8808 if (fname[0] == 'g' && fname[1] == ':')
8809 rfname = fname + 2;
8810
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008811 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8812 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 error = ERROR_UNKNOWN;
8814
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008815 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 /*
8818 * User defined function.
8819 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008820 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008821
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008823 /* Trigger FuncUndefined event, may load the function. */
8824 if (fp == NULL
8825 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008826 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008827 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008829 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 }
8832#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008834 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835 {
8836 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008837 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 }
8839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 if (fp != NULL)
8841 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008842 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008844 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008846 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008848 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008849 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 else
8851 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008852 int did_save_redo = FALSE;
8853
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 /*
8855 * Call the user function.
8856 * Save and restore search patterns, script variables and
8857 * redo buffer.
8858 */
8859 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008860#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008861 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008862#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008863 {
8864 saveRedobuff();
8865 did_save_redo = TRUE;
8866 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008867 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008869 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8871 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8872 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008873 /* Function was unreferenced while being used, free it
8874 * now. */
8875 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008876 if (did_save_redo)
8877 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 restore_search_patterns();
8879 error = ERROR_NONE;
8880 }
8881 }
8882 }
8883 else
8884 {
8885 /*
8886 * Find the function name in the table, call its implementation.
8887 */
8888 i = find_internal_func(fname);
8889 if (i >= 0)
8890 {
8891 if (argcount < functions[i].f_min_argc)
8892 error = ERROR_TOOFEW;
8893 else if (argcount > functions[i].f_max_argc)
8894 error = ERROR_TOOMANY;
8895 else
8896 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008897 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008898 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 error = ERROR_NONE;
8900 }
8901 }
8902 }
8903 /*
8904 * The function call (or "FuncUndefined" autocommand sequence) might
8905 * have been aborted by an error, an interrupt, or an explicitly thrown
8906 * exception that has not been caught so far. This situation can be
8907 * tested for by calling aborting(). For an error in an internal
8908 * function or for the "E132" error in call_user_func(), however, the
8909 * throw point at which the "force_abort" flag (temporarily reset by
8910 * emsg()) is normally updated has not been reached yet. We need to
8911 * update that flag first to make aborting() reliable.
8912 */
8913 update_force_abort();
8914 }
8915 if (error == ERROR_NONE)
8916 ret = OK;
8917
8918 /*
8919 * Report an error unless the argument evaluation or function call has been
8920 * cancelled due to an aborting error, an interrupt, or an exception.
8921 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008922 if (!aborting())
8923 {
8924 switch (error)
8925 {
8926 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008927 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008928 break;
8929 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008930 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008931 break;
8932 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008933 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 name);
8935 break;
8936 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008937 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008938 name);
8939 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008940 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008941 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 name);
8943 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008944 }
8945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008947 while (argv_clear > 0)
8948 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008949 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008950 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952 return ret;
8953}
8954
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008955/*
8956 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008957 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008958 */
8959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008960emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008961{
8962 char_u *p;
8963
8964 if (*name == K_SPECIAL)
8965 p = concat_str((char_u *)"<SNR>", name + 3);
8966 else
8967 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008968 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969 if (p != name)
8970 vim_free(p);
8971}
8972
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008973/*
8974 * Return TRUE for a non-zero Number and a non-empty String.
8975 */
8976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008977non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008978{
8979 return ((argvars[0].v_type == VAR_NUMBER
8980 && argvars[0].vval.v_number != 0)
8981 || (argvars[0].v_type == VAR_STRING
8982 && argvars[0].vval.v_string != NULL
8983 && *argvars[0].vval.v_string != NUL));
8984}
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986/*********************************************
8987 * Implementation of the built-in functions
8988 */
8989
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008990#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008991static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008992
8993/*
8994 * Get the float value of "argvars[0]" into "f".
8995 * Returns FAIL when the argument is not a Number or Float.
8996 */
8997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008998get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008999{
9000 if (argvars[0].v_type == VAR_FLOAT)
9001 {
9002 *f = argvars[0].vval.v_float;
9003 return OK;
9004 }
9005 if (argvars[0].v_type == VAR_NUMBER)
9006 {
9007 *f = (float_T)argvars[0].vval.v_number;
9008 return OK;
9009 }
9010 EMSG(_("E808: Number or Float required"));
9011 return FAIL;
9012}
9013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009014/*
9015 * "abs(expr)" function
9016 */
9017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009018f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009019{
9020 if (argvars[0].v_type == VAR_FLOAT)
9021 {
9022 rettv->v_type = VAR_FLOAT;
9023 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9024 }
9025 else
9026 {
9027 varnumber_T n;
9028 int error = FALSE;
9029
9030 n = get_tv_number_chk(&argvars[0], &error);
9031 if (error)
9032 rettv->vval.v_number = -1;
9033 else if (n > 0)
9034 rettv->vval.v_number = n;
9035 else
9036 rettv->vval.v_number = -n;
9037 }
9038}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009039
9040/*
9041 * "acos()" function
9042 */
9043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009044f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009045{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009046 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009047
9048 rettv->v_type = VAR_FLOAT;
9049 if (get_float_arg(argvars, &f) == OK)
9050 rettv->vval.v_float = acos(f);
9051 else
9052 rettv->vval.v_float = 0.0;
9053}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009054#endif
9055
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009057 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 */
9059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009060f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
Bram Moolenaar33570922005-01-25 22:26:29 +00009062 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009065 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009067 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009068 && !tv_check_lock(l->lv_lock,
9069 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009070 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009072 }
9073 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074 EMSG(_(e_listreq));
9075}
9076
9077/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009078 * "alloc_fail(id, countdown, repeat)" function
9079 */
9080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009081f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009082{
9083 if (argvars[0].v_type != VAR_NUMBER
9084 || argvars[0].vval.v_number <= 0
9085 || argvars[1].v_type != VAR_NUMBER
9086 || argvars[1].vval.v_number < 0
9087 || argvars[2].v_type != VAR_NUMBER)
9088 EMSG(_(e_invarg));
9089 else
9090 {
9091 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009092 if (alloc_fail_id >= aid_last)
9093 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009094 alloc_fail_countdown = argvars[1].vval.v_number;
9095 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009096 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009097 }
9098}
9099
9100/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009101 * "and(expr, expr)" function
9102 */
9103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009104f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009105{
9106 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9107 & get_tv_number_chk(&argvars[1], NULL);
9108}
9109
9110/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009111 * "append(lnum, string/list)" function
9112 */
9113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009114f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009115{
9116 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 list_T *l = NULL;
9119 listitem_T *li = NULL;
9120 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009121 long added = 0;
9122
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009123 /* When coming here from Insert mode, sync undo, so that this can be
9124 * undone separately from what was previously inserted. */
9125 if (u_sync_once == 2)
9126 {
9127 u_sync_once = 1; /* notify that u_sync() was called */
9128 u_sync(TRUE);
9129 }
9130
Bram Moolenaar0d660222005-01-07 21:51:51 +00009131 lnum = get_tv_lnum(argvars);
9132 if (lnum >= 0
9133 && lnum <= curbuf->b_ml.ml_line_count
9134 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009136 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009137 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009138 l = argvars[1].vval.v_list;
9139 if (l == NULL)
9140 return;
9141 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143 for (;;)
9144 {
9145 if (l == NULL)
9146 tv = &argvars[1]; /* append a string */
9147 else if (li == NULL)
9148 break; /* end of list */
9149 else
9150 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009151 line = get_tv_string_chk(tv);
9152 if (line == NULL) /* type error */
9153 {
9154 rettv->vval.v_number = 1; /* Failed */
9155 break;
9156 }
9157 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009158 ++added;
9159 if (l == NULL)
9160 break;
9161 li = li->li_next;
9162 }
9163
9164 appended_lines_mark(lnum, added);
9165 if (curwin->w_cursor.lnum > lnum)
9166 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 else
9169 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170}
9171
9172/*
9173 * "argc()" function
9174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009176f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
9181/*
9182 * "argidx()" function
9183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009185f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188}
9189
9190/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009191 * "arglistid()" function
9192 */
9193 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009194f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009195{
9196 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009197
9198 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009199 wp = find_tabwin(&argvars[0], &argvars[1]);
9200 if (wp != NULL)
9201 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009202}
9203
9204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 * "argv(nr)" function
9206 */
9207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009208f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 int idx;
9211
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009212 if (argvars[0].v_type != VAR_UNKNOWN)
9213 {
9214 idx = get_tv_number_chk(&argvars[0], NULL);
9215 if (idx >= 0 && idx < ARGCOUNT)
9216 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9217 else
9218 rettv->vval.v_string = NULL;
9219 rettv->v_type = VAR_STRING;
9220 }
9221 else if (rettv_list_alloc(rettv) == OK)
9222 for (idx = 0; idx < ARGCOUNT; ++idx)
9223 list_append_string(rettv->vval.v_list,
9224 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225}
9226
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009227static void prepare_assert_error(garray_T*gap);
9228static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9229static void assert_error(garray_T *gap);
9230static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009231
9232/*
9233 * Prepare "gap" for an assert error and add the sourcing position.
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237{
9238 char buf[NUMBUFLEN];
9239
9240 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009241 if (sourcing_name != NULL)
9242 {
9243 ga_concat(gap, sourcing_name);
9244 if (sourcing_lnum > 0)
9245 ga_concat(gap, (char_u *)" ");
9246 }
9247 if (sourcing_lnum > 0)
9248 {
9249 sprintf(buf, "line %ld", (long)sourcing_lnum);
9250 ga_concat(gap, (char_u *)buf);
9251 }
9252 if (sourcing_name != NULL || sourcing_lnum > 0)
9253 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009254}
9255
9256/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009257 * Append "str" to "gap", escaping unprintable characters.
9258 * Changes NL to \n, CR to \r, etc.
9259 */
9260 static void
9261ga_concat_esc(garray_T *gap, char_u *str)
9262{
9263 char_u *p;
9264 char_u buf[NUMBUFLEN];
9265
Bram Moolenaarf1551962016-03-15 12:55:58 +01009266 if (str == NULL)
9267 {
9268 ga_concat(gap, (char_u *)"NULL");
9269 return;
9270 }
9271
Bram Moolenaar23689172016-02-15 22:37:37 +01009272 for (p = str; *p != NUL; ++p)
9273 switch (*p)
9274 {
9275 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9276 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9277 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9278 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9279 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9280 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9281 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9282 default:
9283 if (*p < ' ')
9284 {
9285 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9286 ga_concat(gap, buf);
9287 }
9288 else
9289 ga_append(gap, *p);
9290 break;
9291 }
9292}
9293
9294/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009295 * Fill "gap" with information about an assert error.
9296 */
9297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009298fill_assert_error(
9299 garray_T *gap,
9300 typval_T *opt_msg_tv,
9301 char_u *exp_str,
9302 typval_T *exp_tv,
9303 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304{
9305 char_u numbuf[NUMBUFLEN];
9306 char_u *tofree;
9307
9308 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9309 {
9310 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9311 vim_free(tofree);
9312 }
9313 else
9314 {
9315 ga_concat(gap, (char_u *)"Expected ");
9316 if (exp_str == NULL)
9317 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009318 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319 vim_free(tofree);
9320 }
9321 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009322 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009323 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009324 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009325 vim_free(tofree);
9326 }
9327}
Bram Moolenaar43345542015-11-29 17:35:35 +01009328
9329/*
9330 * Add an assert error to v:errors.
9331 */
9332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009333assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009334{
9335 struct vimvar *vp = &vimvars[VV_ERRORS];
9336
9337 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9338 /* Make sure v:errors is a list. */
9339 set_vim_var_list(VV_ERRORS, list_alloc());
9340 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9341}
9342
Bram Moolenaar43345542015-11-29 17:35:35 +01009343/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009344 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009345 */
9346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009347f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009348{
9349 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009350
9351 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9352 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009353 prepare_assert_error(&ga);
9354 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9355 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009356 ga_clear(&ga);
9357 }
9358}
9359
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009361 * "assert_exception(string[, msg])" function
9362 */
9363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009364f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009365{
9366 garray_T ga;
9367 char *error;
9368
9369 error = (char *)get_tv_string_chk(&argvars[0]);
9370 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9371 {
9372 prepare_assert_error(&ga);
9373 ga_concat(&ga, (char_u *)"v:exception is not set");
9374 assert_error(&ga);
9375 ga_clear(&ga);
9376 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009377 else if (error != NULL
9378 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009379 {
9380 prepare_assert_error(&ga);
9381 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9382 &vimvars[VV_EXCEPTION].vv_tv);
9383 assert_error(&ga);
9384 ga_clear(&ga);
9385 }
9386}
9387
9388/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009389 * "assert_fails(cmd [, error])" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009393{
9394 char_u *cmd = get_tv_string_chk(&argvars[0]);
9395 garray_T ga;
9396
9397 called_emsg = FALSE;
9398 suppress_errthrow = TRUE;
9399 emsg_silent = TRUE;
9400 do_cmdline_cmd(cmd);
9401 if (!called_emsg)
9402 {
9403 prepare_assert_error(&ga);
9404 ga_concat(&ga, (char_u *)"command did not fail: ");
9405 ga_concat(&ga, cmd);
9406 assert_error(&ga);
9407 ga_clear(&ga);
9408 }
9409 else if (argvars[1].v_type != VAR_UNKNOWN)
9410 {
9411 char_u buf[NUMBUFLEN];
9412 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9413
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009414 if (error == NULL
9415 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009416 {
9417 prepare_assert_error(&ga);
9418 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9419 &vimvars[VV_ERRMSG].vv_tv);
9420 assert_error(&ga);
9421 ga_clear(&ga);
9422 }
9423 }
9424
9425 called_emsg = FALSE;
9426 suppress_errthrow = FALSE;
9427 emsg_silent = FALSE;
9428 emsg_on_display = FALSE;
9429 set_vim_var_string(VV_ERRMSG, NULL, 0);
9430}
9431
9432/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009433 * Common for assert_true() and assert_false().
9434 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009436assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009437{
9438 int error = FALSE;
9439 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009440
Bram Moolenaar37127922016-02-06 20:29:28 +01009441 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009442 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009443 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009444 if (argvars[0].v_type != VAR_NUMBER
9445 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9446 || error)
9447 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009448 prepare_assert_error(&ga);
9449 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009450 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009451 NULL, &argvars[0]);
9452 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009453 ga_clear(&ga);
9454 }
9455}
9456
9457/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009459 */
9460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009461f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009462{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009463 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009464}
9465
9466/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009468 */
9469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009470f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009471{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009472 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009473}
9474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009477 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009478 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009480f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009482 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483
9484 rettv->v_type = VAR_FLOAT;
9485 if (get_float_arg(argvars, &f) == OK)
9486 rettv->vval.v_float = asin(f);
9487 else
9488 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009489}
9490
9491/*
9492 * "atan()" function
9493 */
9494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009497 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009498
9499 rettv->v_type = VAR_FLOAT;
9500 if (get_float_arg(argvars, &f) == OK)
9501 rettv->vval.v_float = atan(f);
9502 else
9503 rettv->vval.v_float = 0.0;
9504}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009505
9506/*
9507 * "atan2()" function
9508 */
9509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009511{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009512 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009513
9514 rettv->v_type = VAR_FLOAT;
9515 if (get_float_arg(argvars, &fx) == OK
9516 && get_float_arg(&argvars[1], &fy) == OK)
9517 rettv->vval.v_float = atan2(fx, fy);
9518 else
9519 rettv->vval.v_float = 0.0;
9520}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009521#endif
9522
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523/*
9524 * "browse(save, title, initdir, default)" function
9525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009527f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528{
9529#ifdef FEAT_BROWSE
9530 int save;
9531 char_u *title;
9532 char_u *initdir;
9533 char_u *defname;
9534 char_u buf[NUMBUFLEN];
9535 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 save = get_tv_number_chk(&argvars[0], &error);
9539 title = get_tv_string_chk(&argvars[1]);
9540 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9541 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 if (error || title == NULL || initdir == NULL || defname == NULL)
9544 rettv->vval.v_string = NULL;
9545 else
9546 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009547 do_browse(save ? BROWSE_SAVE : 0,
9548 title, defname, NULL, initdir, NULL, curbuf);
9549#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553}
9554
9555/*
9556 * "browsedir(title, initdir)" function
9557 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009559f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009560{
9561#ifdef FEAT_BROWSE
9562 char_u *title;
9563 char_u *initdir;
9564 char_u buf[NUMBUFLEN];
9565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 title = get_tv_string_chk(&argvars[0]);
9567 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009569 if (title == NULL || initdir == NULL)
9570 rettv->vval.v_string = NULL;
9571 else
9572 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009573 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578}
9579
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009580static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009581
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582/*
9583 * Find a buffer by number or exact name.
9584 */
9585 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009586find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 if (avar->v_type == VAR_NUMBER)
9591 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009592 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009594 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009595 if (buf == NULL)
9596 {
9597 /* No full path name match, try a match with a URL or a "nofile"
9598 * buffer, these don't use the full path. */
9599 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9600 if (buf->b_fname != NULL
9601 && (path_with_url(buf->b_fname)
9602#ifdef FEAT_QUICKFIX
9603 || bt_nofile(buf)
9604#endif
9605 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009606 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009607 break;
9608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 }
9610 return buf;
9611}
9612
9613/*
9614 * "bufexists(expr)" function
9615 */
9616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
9623 * "buflisted(expr)" function
9624 */
9625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009626f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
9628 buf_T *buf;
9629
9630 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
9635 * "bufloaded(expr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
9640 buf_T *buf;
9641
9642 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009643 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644}
9645
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009646 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009647buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 int save_magic;
9650 char_u *save_cpo;
9651 buf_T *buf;
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9654 save_magic = p_magic;
9655 p_magic = TRUE;
9656 save_cpo = p_cpo;
9657 p_cpo = (char_u *)"";
9658
9659 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009660 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661
9662 p_magic = save_magic;
9663 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009664 return buf;
9665}
9666
9667/*
9668 * Get buffer by number or pattern.
9669 */
9670 static buf_T *
9671get_buf_tv(typval_T *tv, int curtab_only)
9672{
9673 char_u *name = tv->vval.v_string;
9674 buf_T *buf;
9675
9676 if (tv->v_type == VAR_NUMBER)
9677 return buflist_findnr((int)tv->vval.v_number);
9678 if (tv->v_type != VAR_STRING)
9679 return NULL;
9680 if (name == NULL || *name == NUL)
9681 return curbuf;
9682 if (name[0] == '$' && name[1] == NUL)
9683 return lastbuf;
9684
9685 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686
9687 /* If not found, try expanding the name, like done for bufexists(). */
9688 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
9691 return buf;
9692}
9693
9694/*
9695 * "bufname(expr)" function
9696 */
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699{
9700 buf_T *buf;
9701
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009704 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 --emsg_off;
9711}
9712
9713/*
9714 * "bufnr(expr)" function
9715 */
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 int error = FALSE;
9721 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009725 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 --emsg_off;
9727
9728 /* If the buffer isn't found and the second argument is not zero create a
9729 * new buffer. */
9730 if (buf == NULL
9731 && argvars[1].v_type != VAR_UNKNOWN
9732 && get_tv_number_chk(&argvars[1], &error) != 0
9733 && !error
9734 && (name = get_tv_string_chk(&argvars[0])) != NULL
9735 && !error)
9736 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9737
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
9745 * "bufwinnr(nr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750#ifdef FEAT_WINDOWS
9751 win_T *wp;
9752 int winnr = 0;
9753#endif
9754 buf_T *buf;
9755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009756 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009758 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759#ifdef FEAT_WINDOWS
9760 for (wp = firstwin; wp; wp = wp->w_next)
9761 {
9762 ++winnr;
9763 if (wp->w_buffer == buf)
9764 break;
9765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#endif
9770 --emsg_off;
9771}
9772
9773/*
9774 * "byte2line(byte)" function
9775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781#else
9782 long boff = 0;
9783
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009788 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 (linenr_T)0, &boff);
9790#endif
9791}
9792
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009794byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009795{
9796#ifdef FEAT_MBYTE
9797 char_u *t;
9798#endif
9799 char_u *str;
9800 long idx;
9801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 str = get_tv_string_chk(&argvars[0]);
9803 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806 return;
9807
9808#ifdef FEAT_MBYTE
9809 t = str;
9810 for ( ; idx > 0; idx--)
9811 {
9812 if (*t == NUL) /* EOL reached */
9813 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814 if (enc_utf8 && comp)
9815 t += utf_ptr2len(t);
9816 else
9817 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009818 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009819 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009820#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009821 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009823#endif
9824}
9825
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009826/*
9827 * "byteidx()" function
9828 */
9829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009830f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009831{
9832 byteidx(argvars, rettv, FALSE);
9833}
9834
9835/*
9836 * "byteidxcomp()" function
9837 */
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009840{
9841 byteidx(argvars, rettv, TRUE);
9842}
9843
Bram Moolenaardb913952012-06-29 12:54:53 +02009844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845func_call(
9846 char_u *name,
9847 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009848 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009849 dict_T *selfdict,
9850 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009851{
9852 listitem_T *item;
9853 typval_T argv[MAX_FUNC_ARGS + 1];
9854 int argc = 0;
9855 int dummy;
9856 int r = 0;
9857
9858 for (item = args->vval.v_list->lv_first; item != NULL;
9859 item = item->li_next)
9860 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009861 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009862 {
9863 EMSG(_("E699: Too many arguments"));
9864 break;
9865 }
9866 /* Make a copy of each argument. This is needed to be able to set
9867 * v_lock to VAR_FIXED in the copy without changing the original list.
9868 */
9869 copy_tv(&item->li_tv, &argv[argc++]);
9870 }
9871
9872 if (item == NULL)
9873 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9874 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009875 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009876
9877 /* Free the arguments. */
9878 while (argc > 0)
9879 clear_tv(&argv[--argc]);
9880
9881 return r;
9882}
9883
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009884/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009885 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009886 */
9887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009888f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009889{
9890 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009891 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009893
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894 if (argvars[1].v_type != VAR_LIST)
9895 {
9896 EMSG(_(e_listreq));
9897 return;
9898 }
9899 if (argvars[1].vval.v_list == NULL)
9900 return;
9901
9902 if (argvars[0].v_type == VAR_FUNC)
9903 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009904 else if (argvars[0].v_type == VAR_PARTIAL)
9905 {
9906 partial = argvars[0].vval.v_partial;
9907 func = partial->pt_name;
9908 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909 else
9910 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 if (*func == NUL)
9912 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 if (argvars[2].v_type != VAR_UNKNOWN)
9915 {
9916 if (argvars[2].v_type != VAR_DICT)
9917 {
9918 EMSG(_(e_dictreq));
9919 return;
9920 }
9921 selfdict = argvars[2].vval.v_dict;
9922 }
9923
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009924 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925}
9926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927#ifdef FEAT_FLOAT
9928/*
9929 * "ceil({float})" function
9930 */
9931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009932f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009934 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = ceil(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
9942#endif
9943
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009944#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009945/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009946 * "ch_close()" function
9947 */
9948 static void
9949f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9950{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009951 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009952
9953 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009954 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009955 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009956 channel_clear(channel);
9957 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958}
9959
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009960/*
9961 * "ch_getbufnr()" function
9962 */
9963 static void
9964f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9965{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009966 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009967
9968 rettv->vval.v_number = -1;
9969 if (channel != NULL)
9970 {
9971 char_u *what = get_tv_string(&argvars[1]);
9972 int part;
9973
9974 if (STRCMP(what, "err") == 0)
9975 part = PART_ERR;
9976 else if (STRCMP(what, "out") == 0)
9977 part = PART_OUT;
9978 else if (STRCMP(what, "in") == 0)
9979 part = PART_IN;
9980 else
9981 part = PART_SOCK;
9982 if (channel->ch_part[part].ch_buffer != NULL)
9983 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9984 }
9985}
9986
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009987/*
9988 * "ch_getjob()" function
9989 */
9990 static void
9991f_ch_getjob(typval_T *argvars, typval_T *rettv)
9992{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009993 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009994
9995 if (channel != NULL)
9996 {
9997 rettv->v_type = VAR_JOB;
9998 rettv->vval.v_job = channel->ch_job;
9999 if (channel->ch_job != NULL)
10000 ++channel->ch_job->jv_refcount;
10001 }
10002}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010003
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010004/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010005 * "ch_log()" function
10006 */
10007 static void
10008f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10009{
10010 char_u *msg = get_tv_string(&argvars[0]);
10011 channel_T *channel = NULL;
10012
10013 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010014 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010015
10016 ch_log(channel, (char *)msg);
10017}
10018
10019/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010020 * "ch_logfile()" function
10021 */
10022 static void
10023f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10024{
10025 char_u *fname;
10026 char_u *opt = (char_u *)"";
10027 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010028
10029 fname = get_tv_string(&argvars[0]);
10030 if (argvars[1].v_type == VAR_STRING)
10031 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010032 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010033}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010034
10035/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010036 * "ch_open()" function
10037 */
10038 static void
10039f_ch_open(typval_T *argvars, typval_T *rettv)
10040{
Bram Moolenaar77073442016-02-13 23:23:53 +010010041 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010042 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010043}
10044
10045/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010046 * "ch_read()" function
10047 */
10048 static void
10049f_ch_read(typval_T *argvars, typval_T *rettv)
10050{
10051 common_channel_read(argvars, rettv, FALSE);
10052}
10053
10054/*
10055 * "ch_readraw()" function
10056 */
10057 static void
10058f_ch_readraw(typval_T *argvars, typval_T *rettv)
10059{
10060 common_channel_read(argvars, rettv, TRUE);
10061}
10062
10063/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010064 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065 */
10066 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010067f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10068{
10069 ch_expr_common(argvars, rettv, TRUE);
10070}
10071
10072/*
10073 * "ch_sendexpr()" function
10074 */
10075 static void
10076f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10077{
10078 ch_expr_common(argvars, rettv, FALSE);
10079}
10080
10081/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010082 * "ch_evalraw()" function
10083 */
10084 static void
10085f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10086{
10087 ch_raw_common(argvars, rettv, TRUE);
10088}
10089
10090/*
10091 * "ch_sendraw()" function
10092 */
10093 static void
10094f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10095{
10096 ch_raw_common(argvars, rettv, FALSE);
10097}
10098
10099/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 * "ch_setoptions()" function
10101 */
10102 static void
10103f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10104{
10105 channel_T *channel;
10106 jobopt_T opt;
10107
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010108 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010109 if (channel == NULL)
10110 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010111 clear_job_options(&opt);
10112 if (get_job_options(&argvars[1], &opt,
10113 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010114 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010115 channel_set_options(channel, &opt);
10116}
10117
10118/*
10119 * "ch_status()" function
10120 */
10121 static void
10122f_ch_status(typval_T *argvars, typval_T *rettv)
10123{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010124 channel_T *channel;
10125
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010126 /* return an empty string by default */
10127 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010128 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010129
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010130 channel = get_channel_arg(&argvars[0], FALSE);
10131 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010132}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010133#endif
10134
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010135/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010136 * "changenr()" function
10137 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010139f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010140{
10141 rettv->vval.v_number = curbuf->b_u_seq_cur;
10142}
10143
10144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 * "char2nr(string)" function
10146 */
10147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010148f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149{
10150#ifdef FEAT_MBYTE
10151 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010152 {
10153 int utf8 = 0;
10154
10155 if (argvars[1].v_type != VAR_UNKNOWN)
10156 utf8 = get_tv_number_chk(&argvars[1], NULL);
10157
10158 if (utf8)
10159 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10160 else
10161 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 else
10164#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010165 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166}
10167
10168/*
10169 * "cindent(lnum)" function
10170 */
10171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010172f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174#ifdef FEAT_CINDENT
10175 pos_T pos;
10176 linenr_T lnum;
10177
10178 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10181 {
10182 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 curwin->w_cursor = pos;
10185 }
10186 else
10187#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189}
10190
10191/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010192 * "clearmatches()" function
10193 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010195f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010196{
10197#ifdef FEAT_SEARCH_EXTRA
10198 clear_matches(curwin);
10199#endif
10200}
10201
10202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 * "col(string)" function
10204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 colnr_T col = 0;
10209 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010210 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010212 fp = var2fpos(&argvars[0], FALSE, &fnum);
10213 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 {
10215 if (fp->col == MAXCOL)
10216 {
10217 /* '> can be MAXCOL, get the length of the line then */
10218 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010219 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 else
10221 col = MAXCOL;
10222 }
10223 else
10224 {
10225 col = fp->col + 1;
10226#ifdef FEAT_VIRTUALEDIT
10227 /* col(".") when the cursor is on the NUL at the end of the line
10228 * because of "coladd" can be seen as an extra column. */
10229 if (virtual_active() && fp == &curwin->w_cursor)
10230 {
10231 char_u *p = ml_get_cursor();
10232
10233 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10234 curwin->w_virtcol - curwin->w_cursor.coladd))
10235 {
10236# ifdef FEAT_MBYTE
10237 int l;
10238
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010239 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 col += l;
10241# else
10242 if (*p != NUL && p[1] == NUL)
10243 ++col;
10244# endif
10245 }
10246 }
10247#endif
10248 }
10249 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
Bram Moolenaar572cb562005-08-05 21:35:02 +000010253#if defined(FEAT_INS_EXPAND)
10254/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010255 * "complete()" function
10256 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010258f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010259{
10260 int startcol;
10261
10262 if ((State & INSERT) == 0)
10263 {
10264 EMSG(_("E785: complete() can only be used in Insert mode"));
10265 return;
10266 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010267
10268 /* Check for undo allowed here, because if something was already inserted
10269 * the line was already saved for undo and this check isn't done. */
10270 if (!undo_allowed())
10271 return;
10272
Bram Moolenaarade00832006-03-10 21:46:58 +000010273 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10274 {
10275 EMSG(_(e_invarg));
10276 return;
10277 }
10278
10279 startcol = get_tv_number_chk(&argvars[0], NULL);
10280 if (startcol <= 0)
10281 return;
10282
10283 set_completion(startcol - 1, argvars[1].vval.v_list);
10284}
10285
10286/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010287 * "complete_add()" function
10288 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010290f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010291{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010292 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010293}
10294
10295/*
10296 * "complete_check()" function
10297 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010299f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010300{
10301 int saved = RedrawingDisabled;
10302
10303 RedrawingDisabled = 0;
10304 ins_compl_check_keys(0);
10305 rettv->vval.v_number = compl_interrupted;
10306 RedrawingDisabled = saved;
10307}
10308#endif
10309
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310/*
10311 * "confirm(message, buttons[, default [, type]])" function
10312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315{
10316#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10317 char_u *message;
10318 char_u *buttons = NULL;
10319 char_u buf[NUMBUFLEN];
10320 char_u buf2[NUMBUFLEN];
10321 int def = 1;
10322 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 char_u *typestr;
10324 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 message = get_tv_string_chk(&argvars[0]);
10327 if (message == NULL)
10328 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010329 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10332 if (buttons == NULL)
10333 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010334 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010336 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010337 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10340 if (typestr == NULL)
10341 error = TRUE;
10342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 switch (TOUPPER_ASC(*typestr))
10345 {
10346 case 'E': type = VIM_ERROR; break;
10347 case 'Q': type = VIM_QUESTION; break;
10348 case 'I': type = VIM_INFO; break;
10349 case 'W': type = VIM_WARNING; break;
10350 case 'G': type = VIM_GENERIC; break;
10351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 }
10353 }
10354 }
10355 }
10356
10357 if (buttons == NULL || *buttons == NUL)
10358 buttons = (char_u *)_("&Ok");
10359
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010360 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010361 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010362 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363#endif
10364}
10365
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010366/*
10367 * "copy()" function
10368 */
10369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010370f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010371{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010372 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010373}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010375#ifdef FEAT_FLOAT
10376/*
10377 * "cos()" function
10378 */
10379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010380f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010381{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010382 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010383
10384 rettv->v_type = VAR_FLOAT;
10385 if (get_float_arg(argvars, &f) == OK)
10386 rettv->vval.v_float = cos(f);
10387 else
10388 rettv->vval.v_float = 0.0;
10389}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010390
10391/*
10392 * "cosh()" function
10393 */
10394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010395f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010396{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010397 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010398
10399 rettv->v_type = VAR_FLOAT;
10400 if (get_float_arg(argvars, &f) == OK)
10401 rettv->vval.v_float = cosh(f);
10402 else
10403 rettv->vval.v_float = 0.0;
10404}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010405#endif
10406
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010408 * "count()" function
10409 */
10410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010411f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010412{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010413 long n = 0;
10414 int ic = FALSE;
10415
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010417 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 listitem_T *li;
10419 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010421
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 if ((l = argvars[0].vval.v_list) != NULL)
10423 {
10424 li = l->lv_first;
10425 if (argvars[2].v_type != VAR_UNKNOWN)
10426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 int error = FALSE;
10428
10429 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 if (argvars[3].v_type != VAR_UNKNOWN)
10431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432 idx = get_tv_number_chk(&argvars[3], &error);
10433 if (!error)
10434 {
10435 li = list_find(l, idx);
10436 if (li == NULL)
10437 EMSGN(_(e_listidx), idx);
10438 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010439 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 if (error)
10441 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010442 }
10443
10444 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010445 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010446 ++n;
10447 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010449 else if (argvars[0].v_type == VAR_DICT)
10450 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010451 int todo;
10452 dict_T *d;
10453 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010455 if ((d = argvars[0].vval.v_dict) != NULL)
10456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 int error = FALSE;
10458
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459 if (argvars[2].v_type != VAR_UNKNOWN)
10460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010462 if (argvars[3].v_type != VAR_UNKNOWN)
10463 EMSG(_(e_invarg));
10464 }
10465
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010466 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010467 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010468 {
10469 if (!HASHITEM_EMPTY(hi))
10470 {
10471 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010472 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010473 ++n;
10474 }
10475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010476 }
10477 }
10478 else
10479 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010480 rettv->vval.v_number = n;
10481}
10482
10483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10485 *
10486 * Checks the existence of a cscope connection.
10487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010489f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
10491#ifdef FEAT_CSCOPE
10492 int num = 0;
10493 char_u *dbpath = NULL;
10494 char_u *prepend = NULL;
10495 char_u buf[NUMBUFLEN];
10496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010497 if (argvars[0].v_type != VAR_UNKNOWN
10498 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500 num = (int)get_tv_number(&argvars[0]);
10501 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 }
10505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507#endif
10508}
10509
10510/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010511 * "cursor(lnum, col)" function, or
10512 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010514 * Moves the cursor to the specified line and column.
10515 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010518f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010521#ifdef FEAT_VIRTUALEDIT
10522 long coladd = 0;
10523#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010524 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010526 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010527 if (argvars[1].v_type == VAR_UNKNOWN)
10528 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010529 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010530 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010531
Bram Moolenaar493c1782014-05-28 14:34:46 +020010532 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010533 {
10534 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010535 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010536 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010537 line = pos.lnum;
10538 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010539#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010540 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010541#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010542 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010543 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010544 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010545 set_curswant = FALSE;
10546 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010547 }
10548 else
10549 {
10550 line = get_tv_lnum(argvars);
10551 col = get_tv_number_chk(&argvars[1], NULL);
10552#ifdef FEAT_VIRTUALEDIT
10553 if (argvars[2].v_type != VAR_UNKNOWN)
10554 coladd = get_tv_number_chk(&argvars[2], NULL);
10555#endif
10556 }
10557 if (line < 0 || col < 0
10558#ifdef FEAT_VIRTUALEDIT
10559 || coladd < 0
10560#endif
10561 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 if (line > 0)
10564 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 if (col > 0)
10566 curwin->w_cursor.col = col - 1;
10567#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010568 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#endif
10570
10571 /* Make sure the cursor is in a valid position. */
10572 check_cursor();
10573#ifdef FEAT_MBYTE
10574 /* Correct cursor for multi-byte character. */
10575 if (has_mbyte)
10576 mb_adjust_cursor();
10577#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010578
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010579 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010580 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010584 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 */
10586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010589 int noref = 0;
10590
10591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010593 if (noref < 0 || noref > 1)
10594 EMSG(_(e_invarg));
10595 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010596 {
10597 current_copyID += COPYID_INC;
10598 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600}
10601
10602/*
10603 * "delete()" function
10604 */
10605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010606f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010608 char_u nbuf[NUMBUFLEN];
10609 char_u *name;
10610 char_u *flags;
10611
10612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010614 return;
10615
10616 name = get_tv_string(&argvars[0]);
10617 if (name == NULL || *name == NUL)
10618 {
10619 EMSG(_(e_invarg));
10620 return;
10621 }
10622
10623 if (argvars[1].v_type != VAR_UNKNOWN)
10624 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010626 flags = (char_u *)"";
10627
10628 if (*flags == NUL)
10629 /* delete a file */
10630 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10631 else if (STRCMP(flags, "d") == 0)
10632 /* delete an empty directory */
10633 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10634 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010635 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010636 rettv->vval.v_number = delete_recursive(name);
10637 else
10638 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639}
10640
10641/*
10642 * "did_filetype()" function
10643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010645f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
10647#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649#endif
10650}
10651
10652/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010653 * "diff_filler()" function
10654 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010656f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010657{
10658#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010659 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010660#endif
10661}
10662
10663/*
10664 * "diff_hlID()" function
10665 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010667f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010668{
10669#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010671 static linenr_T prev_lnum = 0;
10672 static int changedtick = 0;
10673 static int fnum = 0;
10674 static int change_start = 0;
10675 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010676 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010677 int filler_lines;
10678 int col;
10679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010680 if (lnum < 0) /* ignore type error in {lnum} arg */
10681 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010682 if (lnum != prev_lnum
10683 || changedtick != curbuf->b_changedtick
10684 || fnum != curbuf->b_fnum)
10685 {
10686 /* New line, buffer, change: need to get the values. */
10687 filler_lines = diff_check(curwin, lnum);
10688 if (filler_lines < 0)
10689 {
10690 if (filler_lines == -1)
10691 {
10692 change_start = MAXCOL;
10693 change_end = -1;
10694 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10695 hlID = HLF_ADD; /* added line */
10696 else
10697 hlID = HLF_CHD; /* changed line */
10698 }
10699 else
10700 hlID = HLF_ADD; /* added line */
10701 }
10702 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010703 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010704 prev_lnum = lnum;
10705 changedtick = curbuf->b_changedtick;
10706 fnum = curbuf->b_fnum;
10707 }
10708
10709 if (hlID == HLF_CHD || hlID == HLF_TXD)
10710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010712 if (col >= change_start && col <= change_end)
10713 hlID = HLF_TXD; /* changed text */
10714 else
10715 hlID = HLF_CHD; /* changed line */
10716 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010717 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010718#endif
10719}
10720
10721/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010722 * "disable_char_avail_for_testing({expr})" function
10723 */
10724 static void
10725f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10726{
10727 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10728}
10729
10730/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010731 * "empty({expr})" function
10732 */
10733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010735{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010736 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010737
10738 switch (argvars[0].v_type)
10739 {
10740 case VAR_STRING:
10741 case VAR_FUNC:
10742 n = argvars[0].vval.v_string == NULL
10743 || *argvars[0].vval.v_string == NUL;
10744 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010745 case VAR_PARTIAL:
10746 n = FALSE;
10747 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010748 case VAR_NUMBER:
10749 n = argvars[0].vval.v_number == 0;
10750 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010751 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010752#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010753 n = argvars[0].vval.v_float == 0.0;
10754 break;
10755#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010756 case VAR_LIST:
10757 n = argvars[0].vval.v_list == NULL
10758 || argvars[0].vval.v_list->lv_first == NULL;
10759 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 case VAR_DICT:
10761 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010762 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010763 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010764 case VAR_SPECIAL:
10765 n = argvars[0].vval.v_number != VVAL_TRUE;
10766 break;
10767
Bram Moolenaar835dc632016-02-07 14:27:38 +010010768 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010770 n = argvars[0].vval.v_job == NULL
10771 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10772 break;
10773#endif
10774 case VAR_CHANNEL:
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_channel == NULL
10777 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010778 break;
10779#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010780 case VAR_UNKNOWN:
10781 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10782 n = TRUE;
10783 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010784 }
10785
10786 rettv->vval.v_number = n;
10787}
10788
10789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 * "escape({string}, {chars})" function
10791 */
10792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010793f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794{
10795 char_u buf[NUMBUFLEN];
10796
Bram Moolenaar758711c2005-02-02 23:11:38 +000010797 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10798 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010799 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800}
10801
10802/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010803 * "eval()" function
10804 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010806f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010807{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010808 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010810 s = get_tv_string_chk(&argvars[0]);
10811 if (s != NULL)
10812 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813
Bram Moolenaar615b9972015-01-14 17:15:05 +010010814 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010815 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10816 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010817 if (p != NULL && !aborting())
10818 EMSG2(_(e_invexpr2), p);
10819 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010820 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010821 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010823 else if (*s != NUL)
10824 EMSG(_(e_trailing));
10825}
10826
10827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 * "eventhandler()" function
10829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010831f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834}
10835
10836/*
10837 * "executable()" function
10838 */
10839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010840f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010842 char_u *name = get_tv_string(&argvars[0]);
10843
10844 /* Check in $PATH and also check directly if there is a directory name. */
10845 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10846 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010847}
10848
10849/*
10850 * "exepath()" function
10851 */
10852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010853f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010854{
10855 char_u *p = NULL;
10856
Bram Moolenaarb5971142015-03-21 17:32:19 +010010857 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010858 rettv->v_type = VAR_STRING;
10859 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860}
10861
10862/*
10863 * "exists()" function
10864 */
10865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010866f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867{
10868 char_u *p;
10869 char_u *name;
10870 int n = FALSE;
10871 int len = 0;
10872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 if (*p == '$') /* environment variable */
10875 {
10876 /* first try "normal" environment variables (fast) */
10877 if (mch_getenv(p + 1) != NULL)
10878 n = TRUE;
10879 else
10880 {
10881 /* try expanding things like $VIM and ${HOME} */
10882 p = expand_env_save(p);
10883 if (p != NULL && *p != '$')
10884 n = TRUE;
10885 vim_free(p);
10886 }
10887 }
10888 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010889 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010891 if (*skipwhite(p) != NUL)
10892 n = FALSE; /* trailing garbage */
10893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 else if (*p == '*') /* internal or user defined function */
10895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010896 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 }
10898 else if (*p == ':')
10899 {
10900 n = cmd_exists(p + 1);
10901 }
10902 else if (*p == '#')
10903 {
10904#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010905 if (p[1] == '#')
10906 n = autocmd_supported(p + 2);
10907 else
10908 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909#endif
10910 }
10911 else /* internal variable */
10912 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010913 char_u *tofree;
10914 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010916 /* get_name_len() takes care of expanding curly braces */
10917 name = p;
10918 len = get_name_len(&p, &tofree, TRUE, FALSE);
10919 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010921 if (tofree != NULL)
10922 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010923 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010924 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010926 /* handle d.key, l[idx], f(expr) */
10927 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10928 if (n)
10929 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 }
10931 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010932 if (*p != NUL)
10933 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010935 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939}
10940
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010941#ifdef FEAT_FLOAT
10942/*
10943 * "exp()" function
10944 */
10945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010946f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010947{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010948 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010949
10950 rettv->v_type = VAR_FLOAT;
10951 if (get_float_arg(argvars, &f) == OK)
10952 rettv->vval.v_float = exp(f);
10953 else
10954 rettv->vval.v_float = 0.0;
10955}
10956#endif
10957
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958/*
10959 * "expand()" function
10960 */
10961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010962f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963{
10964 char_u *s;
10965 int len;
10966 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010967 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010970 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010972 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010973 if (argvars[1].v_type != VAR_UNKNOWN
10974 && argvars[2].v_type != VAR_UNKNOWN
10975 && get_tv_number_chk(&argvars[2], &error)
10976 && !error)
10977 {
10978 rettv->v_type = VAR_LIST;
10979 rettv->vval.v_list = NULL;
10980 }
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 if (*s == '%' || *s == '#' || *s == '<')
10984 {
10985 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010986 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010988 if (rettv->v_type == VAR_LIST)
10989 {
10990 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10991 list_append_string(rettv->vval.v_list, result, -1);
10992 else
10993 vim_free(result);
10994 }
10995 else
10996 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 }
10998 else
10999 {
11000 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 if (argvars[1].v_type != VAR_UNKNOWN
11003 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011004 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 if (!error)
11006 {
11007 ExpandInit(&xpc);
11008 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011009 if (p_wic)
11010 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011011 if (rettv->v_type == VAR_STRING)
11012 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11013 options, WILD_ALL);
11014 else if (rettv_list_alloc(rettv) != FAIL)
11015 {
11016 int i;
11017
11018 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11019 for (i = 0; i < xpc.xp_numfiles; i++)
11020 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11021 ExpandCleanup(&xpc);
11022 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 }
11024 else
11025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 }
11027}
11028
11029/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011030 * Go over all entries in "d2" and add them to "d1".
11031 * When "action" is "error" then a duplicate key is an error.
11032 * When "action" is "force" then a duplicate key is overwritten.
11033 * Otherwise duplicate keys are ignored ("action" is "keep").
11034 */
11035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011036dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011037{
11038 dictitem_T *di1;
11039 hashitem_T *hi2;
11040 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011041 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011042
11043 todo = (int)d2->dv_hashtab.ht_used;
11044 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11045 {
11046 if (!HASHITEM_EMPTY(hi2))
11047 {
11048 --todo;
11049 di1 = dict_find(d1, hi2->hi_key, -1);
11050 if (d1->dv_scope != 0)
11051 {
11052 /* Disallow replacing a builtin function in l: and g:.
11053 * Check the key to be valid when adding to any
11054 * scope. */
11055 if (d1->dv_scope == VAR_DEF_SCOPE
11056 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11057 && var_check_func_name(hi2->hi_key,
11058 di1 == NULL))
11059 break;
11060 if (!valid_varname(hi2->hi_key))
11061 break;
11062 }
11063 if (di1 == NULL)
11064 {
11065 di1 = dictitem_copy(HI2DI(hi2));
11066 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11067 dictitem_free(di1);
11068 }
11069 else if (*action == 'e')
11070 {
11071 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11072 break;
11073 }
11074 else if (*action == 'f' && HI2DI(hi2) != di1)
11075 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011076 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11077 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011078 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011079 clear_tv(&di1->di_tv);
11080 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11081 }
11082 }
11083 }
11084}
11085
11086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011087 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011088 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011089 */
11090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011091f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011092{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011093 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011094
Bram Moolenaare9a41262005-01-15 22:18:47 +000011095 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011096 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011097 list_T *l1, *l2;
11098 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011099 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011101
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102 l1 = argvars[0].vval.v_list;
11103 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011104 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011105 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106 {
11107 if (argvars[2].v_type != VAR_UNKNOWN)
11108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 before = get_tv_number_chk(&argvars[2], &error);
11110 if (error)
11111 return; /* type error; errmsg already given */
11112
Bram Moolenaar758711c2005-02-02 23:11:38 +000011113 if (before == l1->lv_len)
11114 item = NULL;
11115 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011116 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011117 item = list_find(l1, before);
11118 if (item == NULL)
11119 {
11120 EMSGN(_(e_listidx), before);
11121 return;
11122 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011123 }
11124 }
11125 else
11126 item = NULL;
11127 list_extend(l1, l2, item);
11128
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 copy_tv(&argvars[0], rettv);
11130 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011131 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11133 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011134 dict_T *d1, *d2;
11135 char_u *action;
11136 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011137
11138 d1 = argvars[0].vval.v_dict;
11139 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011140 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011141 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011142 {
11143 /* Check the third argument. */
11144 if (argvars[2].v_type != VAR_UNKNOWN)
11145 {
11146 static char *(av[]) = {"keep", "force", "error"};
11147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 action = get_tv_string_chk(&argvars[2]);
11149 if (action == NULL)
11150 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011151 for (i = 0; i < 3; ++i)
11152 if (STRCMP(action, av[i]) == 0)
11153 break;
11154 if (i == 3)
11155 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011156 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 return;
11158 }
11159 }
11160 else
11161 action = (char_u *)"force";
11162
Bram Moolenaara9922d62013-05-30 13:01:18 +020011163 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164
Bram Moolenaare9a41262005-01-15 22:18:47 +000011165 copy_tv(&argvars[0], rettv);
11166 }
11167 }
11168 else
11169 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011170}
11171
11172/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011173 * "feedkeys()" function
11174 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011176f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011177{
11178 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011179 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011180 char_u *keys, *flags;
11181 char_u nbuf[NUMBUFLEN];
11182 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011183 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011184 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011185
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011186 /* This is not allowed in the sandbox. If the commands would still be
11187 * executed in the sandbox it would be OK, but it probably happens later,
11188 * when "sandbox" is no longer set. */
11189 if (check_secure())
11190 return;
11191
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011192 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011193
11194 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011195 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011196 flags = get_tv_string_buf(&argvars[1], nbuf);
11197 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011198 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011199 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011200 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011201 case 'n': remap = FALSE; break;
11202 case 'm': remap = TRUE; break;
11203 case 't': typed = TRUE; break;
11204 case 'i': insert = TRUE; break;
11205 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011206 }
11207 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011208 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011209
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011210 if (*keys != NUL || execute)
11211 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011212 /* Need to escape K_SPECIAL and CSI before putting the string in the
11213 * typeahead buffer. */
11214 keys_esc = vim_strsave_escape_csi(keys);
11215 if (keys_esc != NULL)
11216 {
11217 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011218 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011219 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011220 if (vgetc_busy)
11221 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011222 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011223 {
11224 int save_msg_scroll = msg_scroll;
11225
11226 /* Avoid a 1 second delay when the keys start Insert mode. */
11227 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011228 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011229 msg_scroll |= save_msg_scroll;
11230 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011231 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011232 }
11233}
11234
11235/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * "filereadable()" function
11237 */
11238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011239f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011241 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 char_u *p;
11243 int n;
11244
Bram Moolenaarc236c162008-07-13 17:41:49 +000011245#ifndef O_NONBLOCK
11246# define O_NONBLOCK 0
11247#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011249 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11250 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251 {
11252 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011253 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 }
11255 else
11256 n = FALSE;
11257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259}
11260
11261/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011262 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 * rights to write into.
11264 */
11265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011266f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011268 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269}
11270
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011271 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011272findfilendir(
11273 typval_T *argvars UNUSED,
11274 typval_T *rettv,
11275 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011276{
11277#ifdef FEAT_SEARCHPATH
11278 char_u *fname;
11279 char_u *fresult = NULL;
11280 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11281 char_u *p;
11282 char_u pathbuf[NUMBUFLEN];
11283 int count = 1;
11284 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011285 int error = FALSE;
11286#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011287
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011288 rettv->vval.v_string = NULL;
11289 rettv->v_type = VAR_STRING;
11290
11291#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011293
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011294 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11297 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011298 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 else
11300 {
11301 if (*p != NUL)
11302 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011305 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011307 }
11308
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011309 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11310 error = TRUE;
11311
11312 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314 do
11315 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011316 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011317 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 fresult = find_file_in_path_option(first ? fname : NULL,
11319 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011320 0, first, path,
11321 find_what,
11322 curbuf->b_ffname,
11323 find_what == FINDFILE_DIR
11324 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011326
11327 if (fresult != NULL && rettv->v_type == VAR_LIST)
11328 list_append_string(rettv->vval.v_list, fresult, -1);
11329
11330 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011332
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011333 if (rettv->v_type == VAR_STRING)
11334 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011335#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011336}
11337
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011338static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11339static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011340
11341/*
11342 * Implementation of map() and filter().
11343 */
11344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011345filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011346{
11347 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011349 listitem_T *li, *nli;
11350 list_T *l = NULL;
11351 dictitem_T *di;
11352 hashtab_T *ht;
11353 hashitem_T *hi;
11354 dict_T *d = NULL;
11355 typval_T save_val;
11356 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011357 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011358 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011359 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011360 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011361 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011362 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011363 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011364
Bram Moolenaare9a41262005-01-15 22:18:47 +000011365 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011366 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011367 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011368 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 return;
11370 }
11371 else if (argvars[0].v_type == VAR_DICT)
11372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011373 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011374 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375 return;
11376 }
11377 else
11378 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011379 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011380 return;
11381 }
11382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011383 expr = get_tv_string_buf_chk(&argvars[1], buf);
11384 /* On type errors, the preceding call has already displayed an error
11385 * message. Avoid a misleading error message for an empty string that
11386 * was not passed as argument. */
11387 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 prepare_vimvar(VV_VAL, &save_val);
11390 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011391
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011392 /* We reset "did_emsg" to be able to detect whether an error
11393 * occurred during evaluation of the expression. */
11394 save_did_emsg = did_emsg;
11395 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011396
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011397 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 vimvars[VV_KEY].vv_type = VAR_STRING;
11401
11402 ht = &d->dv_hashtab;
11403 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011404 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 if (!HASHITEM_EMPTY(hi))
11408 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011409 int r;
11410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 --todo;
11412 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011413 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011414 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11415 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 break;
11417 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011418 r = filter_map_one(&di->di_tv, expr, map, &rem);
11419 clear_tv(&vimvars[VV_KEY].vv_tv);
11420 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 break;
11422 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011423 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011424 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11425 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011426 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429 }
11430 }
11431 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432 }
11433 else
11434 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011435 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 for (li = l->lv_first; li != NULL; li = nli)
11438 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011439 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011440 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011442 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011443 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011444 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011445 break;
11446 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011448 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011449 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011450 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011451
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011452 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011453 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011454
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011455 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011457
11458 copy_tv(&argvars[0], rettv);
11459}
11460
11461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011462filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463{
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011466 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467
Bram Moolenaar33570922005-01-25 22:26:29 +000011468 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469 s = expr;
11470 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011471 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 if (*s != NUL) /* check for trailing chars after expr */
11473 {
11474 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011475 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011476 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011477 }
11478 if (map)
11479 {
11480 /* map(): replace the list item value */
11481 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011482 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 *tv = rettv;
11484 }
11485 else
11486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 int error = FALSE;
11488
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 /* On type error, nothing has been removed; return FAIL to stop the
11493 * loop. The error message was given by get_tv_number_chk(). */
11494 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011495 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011497 retval = OK;
11498theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011500 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011501}
11502
11503/*
11504 * "filter()" function
11505 */
11506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011507f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011508{
11509 filter_map(argvars, rettv, FALSE);
11510}
11511
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 * "finddir({fname}[, {path}[, {count}]])" function
11514 */
11515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011516f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011518 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519}
11520
11521/*
11522 * "findfile({fname}[, {path}[, {count}]])" function
11523 */
11524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011525f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011526{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011527 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011528}
11529
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011530#ifdef FEAT_FLOAT
11531/*
11532 * "float2nr({float})" function
11533 */
11534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011535f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011536{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011537 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011538
11539 if (get_float_arg(argvars, &f) == OK)
11540 {
11541 if (f < -0x7fffffff)
11542 rettv->vval.v_number = -0x7fffffff;
11543 else if (f > 0x7fffffff)
11544 rettv->vval.v_number = 0x7fffffff;
11545 else
11546 rettv->vval.v_number = (varnumber_T)f;
11547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011548}
11549
11550/*
11551 * "floor({float})" function
11552 */
11553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011554f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011555{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011556 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011557
11558 rettv->v_type = VAR_FLOAT;
11559 if (get_float_arg(argvars, &f) == OK)
11560 rettv->vval.v_float = floor(f);
11561 else
11562 rettv->vval.v_float = 0.0;
11563}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011564
11565/*
11566 * "fmod()" function
11567 */
11568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011569f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011570{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011571 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011572
11573 rettv->v_type = VAR_FLOAT;
11574 if (get_float_arg(argvars, &fx) == OK
11575 && get_float_arg(&argvars[1], &fy) == OK)
11576 rettv->vval.v_float = fmod(fx, fy);
11577 else
11578 rettv->vval.v_float = 0.0;
11579}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011580#endif
11581
Bram Moolenaar0d660222005-01-07 21:51:51 +000011582/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011583 * "fnameescape({string})" function
11584 */
11585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011586f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011587{
11588 rettv->vval.v_string = vim_strsave_fnameescape(
11589 get_tv_string(&argvars[0]), FALSE);
11590 rettv->v_type = VAR_STRING;
11591}
11592
11593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594 * "fnamemodify({fname}, {mods})" function
11595 */
11596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011597f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598{
11599 char_u *fname;
11600 char_u *mods;
11601 int usedlen = 0;
11602 int len;
11603 char_u *fbuf = NULL;
11604 char_u buf[NUMBUFLEN];
11605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011606 fname = get_tv_string_chk(&argvars[0]);
11607 mods = get_tv_string_buf_chk(&argvars[1], buf);
11608 if (fname == NULL || mods == NULL)
11609 fname = NULL;
11610 else
11611 {
11612 len = (int)STRLEN(fname);
11613 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011620 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 vim_free(fbuf);
11622}
11623
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011624static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625
11626/*
11627 * "foldclosed()" function
11628 */
11629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011630foldclosed_both(
11631 typval_T *argvars UNUSED,
11632 typval_T *rettv,
11633 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634{
11635#ifdef FEAT_FOLDING
11636 linenr_T lnum;
11637 linenr_T first, last;
11638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11641 {
11642 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11643 {
11644 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011648 return;
11649 }
11650 }
11651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653}
11654
11655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 * "foldclosed()" function
11657 */
11658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011659f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660{
11661 foldclosed_both(argvars, rettv, FALSE);
11662}
11663
11664/*
11665 * "foldclosedend()" function
11666 */
11667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011668f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011669{
11670 foldclosed_both(argvars, rettv, TRUE);
11671}
11672
11673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 * "foldlevel()" function
11675 */
11676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011677f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678{
11679#ifdef FEAT_FOLDING
11680 linenr_T lnum;
11681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686}
11687
11688/*
11689 * "foldtext()" function
11690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011692f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693{
11694#ifdef FEAT_FOLDING
11695 linenr_T lnum;
11696 char_u *s;
11697 char_u *r;
11698 int len;
11699 char *txt;
11700#endif
11701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702 rettv->v_type = VAR_STRING;
11703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11706 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11707 <= curbuf->b_ml.ml_line_count
11708 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 {
11710 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11712 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 {
11714 if (!linewhite(lnum))
11715 break;
11716 ++lnum;
11717 }
11718
11719 /* Find interesting text in this line. */
11720 s = skipwhite(ml_get(lnum));
11721 /* skip C comment-start */
11722 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011725 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011726 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011727 {
11728 s = skipwhite(ml_get(lnum + 1));
11729 if (*s == '*')
11730 s = skipwhite(s + 1);
11731 }
11732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 txt = _("+-%s%3ld lines: ");
11734 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 + 20 /* for %3ld */
11737 + STRLEN(s))); /* concatenated */
11738 if (r != NULL)
11739 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11741 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11742 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743 len = (int)STRLEN(r);
11744 STRCAT(r, s);
11745 /* remove 'foldmarker' and 'commentstring' */
11746 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 }
11749 }
11750#endif
11751}
11752
11753/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011754 * "foldtextresult(lnum)" function
11755 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011757f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011758{
11759#ifdef FEAT_FOLDING
11760 linenr_T lnum;
11761 char_u *text;
11762 char_u buf[51];
11763 foldinfo_T foldinfo;
11764 int fold_count;
11765#endif
11766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->v_type = VAR_STRING;
11768 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011769#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011770 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 /* treat illegal types and illegal string values for {lnum} the same */
11772 if (lnum < 0)
11773 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011774 fold_count = foldedCount(curwin, lnum, &foldinfo);
11775 if (fold_count > 0)
11776 {
11777 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11778 &foldinfo, buf);
11779 if (text == buf)
11780 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011782 }
11783#endif
11784}
11785
11786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 * "foreground()" function
11788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011790f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792#ifdef FEAT_GUI
11793 if (gui.in_use)
11794 gui_mch_set_foreground();
11795#else
11796# ifdef WIN32
11797 win32_set_foreground();
11798# endif
11799#endif
11800}
11801
11802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011803 * "function()" function
11804 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011806f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011807{
11808 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011809 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011810 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011811 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011812
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011813 if (argvars[0].v_type == VAR_FUNC)
11814 {
11815 /* function(MyFunc, [arg], dict) */
11816 s = argvars[0].vval.v_string;
11817 }
11818 else if (argvars[0].v_type == VAR_PARTIAL
11819 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011820 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011821 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011822 arg_pt = argvars[0].vval.v_partial;
11823 s = arg_pt->pt_name;
11824 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011825 else
11826 {
11827 /* function('MyFunc', [arg], dict) */
11828 s = get_tv_string(&argvars[0]);
11829 use_string = TRUE;
11830 }
11831
11832 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011833 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011834 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011835 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11836 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011837 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011838 else
11839 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011840 int dict_idx = 0;
11841 int arg_idx = 0;
11842 list_T *list = NULL;
11843
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011844 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011845 {
11846 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011847 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011848
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011849 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11850 * also be called from another script. Using trans_function_name()
11851 * would also work, but some plugins depend on the name being
11852 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011853 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011854 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11855 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011856 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011857 STRCPY(name, sid_buf);
11858 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011859 }
11860 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011861 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011862 name = vim_strsave(s);
11863
11864 if (argvars[1].v_type != VAR_UNKNOWN)
11865 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011866 if (argvars[2].v_type != VAR_UNKNOWN)
11867 {
11868 /* function(name, [args], dict) */
11869 arg_idx = 1;
11870 dict_idx = 2;
11871 }
11872 else if (argvars[1].v_type == VAR_DICT)
11873 /* function(name, dict) */
11874 dict_idx = 1;
11875 else
11876 /* function(name, [args]) */
11877 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011878 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011879 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011880 if (argvars[dict_idx].v_type != VAR_DICT)
11881 {
11882 EMSG(_("E922: expected a dict"));
11883 vim_free(name);
11884 return;
11885 }
11886 if (argvars[dict_idx].vval.v_dict == NULL)
11887 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011888 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011889 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011890 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011891 if (argvars[arg_idx].v_type != VAR_LIST)
11892 {
11893 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11894 vim_free(name);
11895 return;
11896 }
11897 list = argvars[arg_idx].vval.v_list;
11898 if (list == NULL || list->lv_len == 0)
11899 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011900 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011901 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011902 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011903 {
11904 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011905
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011906 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011907 if (pt == NULL)
11908 vim_free(name);
11909 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011910 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011911 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011912 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011913 listitem_T *li;
11914 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011915 int arg_len = 0;
11916 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011917
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011918 if (arg_pt != NULL)
11919 arg_len = arg_pt->pt_argc;
11920 if (list != NULL)
11921 lv_len = list->lv_len;
11922 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011923 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011924 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011925 if (pt->pt_argv == NULL)
11926 {
11927 vim_free(pt);
11928 vim_free(name);
11929 return;
11930 }
11931 else
11932 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011933 for (i = 0; i < arg_len; i++)
11934 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11935 if (lv_len > 0)
11936 for (li = list->lv_first; li != NULL;
11937 li = li->li_next)
11938 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011939 }
11940 }
11941
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011942 /* For "function(dict.func, [], dict)" and "func" is a partial
11943 * use "dict". That is backwards compatible. */
11944 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011945 {
11946 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11947 ++pt->pt_dict->dv_refcount;
11948 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011949 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011950 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011951 pt->pt_dict = arg_pt->pt_dict;
11952 if (pt->pt_dict != NULL)
11953 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011954 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011955
11956 pt->pt_refcount = 1;
11957 pt->pt_name = name;
11958 func_ref(pt->pt_name);
11959 }
11960 rettv->v_type = VAR_PARTIAL;
11961 rettv->vval.v_partial = pt;
11962 }
11963 else
11964 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011965 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011966 rettv->v_type = VAR_FUNC;
11967 rettv->vval.v_string = name;
11968 func_ref(name);
11969 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011970 }
11971}
11972
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011973 static void
11974partial_free(partial_T *pt)
11975{
11976 int i;
11977
11978 for (i = 0; i < pt->pt_argc; ++i)
11979 clear_tv(&pt->pt_argv[i]);
11980 vim_free(pt->pt_argv);
11981 func_unref(pt->pt_name);
11982 vim_free(pt->pt_name);
11983 vim_free(pt);
11984}
11985
11986/*
11987 * Unreference a closure: decrement the reference count and free it when it
11988 * becomes zero.
11989 */
11990 void
11991partial_unref(partial_T *pt)
11992{
11993 if (pt != NULL && --pt->pt_refcount <= 0)
11994 partial_free(pt);
11995}
11996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011997/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011998 * "garbagecollect()" function
11999 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012001f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012002{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012003 /* This is postponed until we are back at the toplevel, because we may be
12004 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12005 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012006
12007 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12008 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012009}
12010
12011/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012012 * "get()" function
12013 */
12014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012015f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016{
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 listitem_T *li;
12018 list_T *l;
12019 dictitem_T *di;
12020 dict_T *d;
12021 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022
Bram Moolenaare9a41262005-01-15 22:18:47 +000012023 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012025 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 int error = FALSE;
12028
12029 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12030 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012032 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012034 else if (argvars[0].v_type == VAR_DICT)
12035 {
12036 if ((d = argvars[0].vval.v_dict) != NULL)
12037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012038 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012039 if (di != NULL)
12040 tv = &di->di_tv;
12041 }
12042 }
12043 else
12044 EMSG2(_(e_listdictarg), "get()");
12045
12046 if (tv == NULL)
12047 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012048 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012049 copy_tv(&argvars[2], rettv);
12050 }
12051 else
12052 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012053}
12054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012055static 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 +000012056
12057/*
12058 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012059 * Return a range (from start to end) of lines in rettv from the specified
12060 * buffer.
12061 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012062 */
12063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012064get_buffer_lines(
12065 buf_T *buf,
12066 linenr_T start,
12067 linenr_T end,
12068 int retlist,
12069 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012070{
12071 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012072
Bram Moolenaar959a1432013-12-14 12:17:38 +010012073 rettv->v_type = VAR_STRING;
12074 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012075 if (retlist && rettv_list_alloc(rettv) == FAIL)
12076 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012077
12078 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12079 return;
12080
12081 if (!retlist)
12082 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012083 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12084 p = ml_get_buf(buf, start, FALSE);
12085 else
12086 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 rettv->vval.v_string = vim_strsave(p);
12088 }
12089 else
12090 {
12091 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012092 return;
12093
12094 if (start < 1)
12095 start = 1;
12096 if (end > buf->b_ml.ml_line_count)
12097 end = buf->b_ml.ml_line_count;
12098 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012099 if (list_append_string(rettv->vval.v_list,
12100 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012101 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012102 }
12103}
12104
12105/*
12106 * "getbufline()" function
12107 */
12108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012109f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012110{
12111 linenr_T lnum;
12112 linenr_T end;
12113 buf_T *buf;
12114
12115 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12116 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012117 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012118 --emsg_off;
12119
Bram Moolenaar661b1822005-07-28 22:36:45 +000012120 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012121 if (argvars[2].v_type == VAR_UNKNOWN)
12122 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012123 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012124 end = get_tv_lnum_buf(&argvars[2], buf);
12125
Bram Moolenaar342337a2005-07-21 21:11:17 +000012126 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012127}
12128
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129/*
12130 * "getbufvar()" function
12131 */
12132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134{
12135 buf_T *buf;
12136 buf_T *save_curbuf;
12137 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012138 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012139 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12142 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012144 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012146 rettv->v_type = VAR_STRING;
12147 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148
12149 if (buf != NULL && varname != NULL)
12150 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012151 /* set curbuf to be our buf, temporarily */
12152 save_curbuf = curbuf;
12153 curbuf = buf;
12154
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012156 {
12157 if (get_option_tv(&varname, rettv, TRUE) == OK)
12158 done = TRUE;
12159 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012160 else if (STRCMP(varname, "changedtick") == 0)
12161 {
12162 rettv->v_type = VAR_NUMBER;
12163 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012164 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012165 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012166 else
12167 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012168 /* Look up the variable. */
12169 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12170 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12171 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012173 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012174 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012175 done = TRUE;
12176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012177 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012178
12179 /* restore previous notion of curbuf */
12180 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012181 }
12182
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012183 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12184 /* use the default value */
12185 copy_tv(&argvars[2], rettv);
12186
Bram Moolenaar0d660222005-01-07 21:51:51 +000012187 --emsg_off;
12188}
12189
12190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191 * "getchar()" function
12192 */
12193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012194f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012199 /* Position the cursor. Needed after a message that ends in a space. */
12200 windgoto(msg_row, msg_col);
12201
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 ++no_mapping;
12203 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012204 for (;;)
12205 {
12206 if (argvars[0].v_type == VAR_UNKNOWN)
12207 /* getchar(): blocking wait. */
12208 n = safe_vgetc();
12209 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12210 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012211 n = vpeekc_any();
12212 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012213 /* illegal argument or getchar(0) and no char avail: return zero */
12214 n = 0;
12215 else
12216 /* getchar(0) and char avail: return char */
12217 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012218
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012219 if (n == K_IGNORE)
12220 continue;
12221 break;
12222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223 --no_mapping;
12224 --allow_keys;
12225
Bram Moolenaar219b8702006-11-01 14:32:36 +000012226 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12227 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12228 vimvars[VV_MOUSE_COL].vv_nr = 0;
12229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 if (IS_SPECIAL(n) || mod_mask != 0)
12232 {
12233 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12234 int i = 0;
12235
12236 /* Turn a special key into three bytes, plus modifier. */
12237 if (mod_mask != 0)
12238 {
12239 temp[i++] = K_SPECIAL;
12240 temp[i++] = KS_MODIFIER;
12241 temp[i++] = mod_mask;
12242 }
12243 if (IS_SPECIAL(n))
12244 {
12245 temp[i++] = K_SPECIAL;
12246 temp[i++] = K_SECOND(n);
12247 temp[i++] = K_THIRD(n);
12248 }
12249#ifdef FEAT_MBYTE
12250 else if (has_mbyte)
12251 i += (*mb_char2bytes)(n, temp + i);
12252#endif
12253 else
12254 temp[i++] = n;
12255 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 rettv->v_type = VAR_STRING;
12257 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012258
12259#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012260 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012261 {
12262 int row = mouse_row;
12263 int col = mouse_col;
12264 win_T *win;
12265 linenr_T lnum;
12266# ifdef FEAT_WINDOWS
12267 win_T *wp;
12268# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012269 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012270
12271 if (row >= 0 && col >= 0)
12272 {
12273 /* Find the window at the mouse coordinates and compute the
12274 * text position. */
12275 win = mouse_find_win(&row, &col);
12276 (void)mouse_comp_pos(win, &row, &col, &lnum);
12277# ifdef FEAT_WINDOWS
12278 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012279 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012280# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012281 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012282 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12283 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12284 }
12285 }
12286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 }
12288}
12289
12290/*
12291 * "getcharmod()" function
12292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012296 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297}
12298
12299/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012300 * "getcharsearch()" function
12301 */
12302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012303f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012304{
12305 if (rettv_dict_alloc(rettv) != FAIL)
12306 {
12307 dict_T *dict = rettv->vval.v_dict;
12308
12309 dict_add_nr_str(dict, "char", 0L, last_csearch());
12310 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12311 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12312 }
12313}
12314
12315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 * "getcmdline()" function
12317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012319f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 rettv->v_type = VAR_STRING;
12322 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323}
12324
12325/*
12326 * "getcmdpos()" function
12327 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012329f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332}
12333
12334/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012335 * "getcmdtype()" function
12336 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012338f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012339{
12340 rettv->v_type = VAR_STRING;
12341 rettv->vval.v_string = alloc(2);
12342 if (rettv->vval.v_string != NULL)
12343 {
12344 rettv->vval.v_string[0] = get_cmdline_type();
12345 rettv->vval.v_string[1] = NUL;
12346 }
12347}
12348
12349/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012350 * "getcmdwintype()" function
12351 */
12352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012354{
12355 rettv->v_type = VAR_STRING;
12356 rettv->vval.v_string = NULL;
12357#ifdef FEAT_CMDWIN
12358 rettv->vval.v_string = alloc(2);
12359 if (rettv->vval.v_string != NULL)
12360 {
12361 rettv->vval.v_string[0] = cmdwin_type;
12362 rettv->vval.v_string[1] = NUL;
12363 }
12364#endif
12365}
12366
12367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 * "getcwd()" function
12369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012373 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012374 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012377 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012378
12379 wp = find_tabwin(&argvars[0], &argvars[1]);
12380 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012382 if (wp->w_localdir != NULL)
12383 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12384 else if(globaldir != NULL)
12385 rettv->vval.v_string = vim_strsave(globaldir);
12386 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012387 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012388 cwd = alloc(MAXPATHL);
12389 if (cwd != NULL)
12390 {
12391 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12392 rettv->vval.v_string = vim_strsave(cwd);
12393 vim_free(cwd);
12394 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012395 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012396#ifdef BACKSLASH_IN_FILENAME
12397 if (rettv->vval.v_string != NULL)
12398 slash_adjust(rettv->vval.v_string);
12399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 }
12401}
12402
12403/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012404 * "getfontname()" function
12405 */
12406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 rettv->v_type = VAR_STRING;
12410 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012411#ifdef FEAT_GUI
12412 if (gui.in_use)
12413 {
12414 GuiFont font;
12415 char_u *name = NULL;
12416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012417 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012418 {
12419 /* Get the "Normal" font. Either the name saved by
12420 * hl_set_font_name() or from the font ID. */
12421 font = gui.norm_font;
12422 name = hl_get_font_name();
12423 }
12424 else
12425 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012427 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12428 return;
12429 font = gui_mch_get_font(name, FALSE);
12430 if (font == NOFONT)
12431 return; /* Invalid font name, return empty string. */
12432 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012433 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012434 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012435 gui_mch_free_font(font);
12436 }
12437#endif
12438}
12439
12440/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012441 * "getfperm({fname})" function
12442 */
12443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012444f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012445{
12446 char_u *fname;
12447 struct stat st;
12448 char_u *perm = NULL;
12449 char_u flags[] = "rwx";
12450 int i;
12451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012455 if (mch_stat((char *)fname, &st) >= 0)
12456 {
12457 perm = vim_strsave((char_u *)"---------");
12458 if (perm != NULL)
12459 {
12460 for (i = 0; i < 9; i++)
12461 {
12462 if (st.st_mode & (1 << (8 - i)))
12463 perm[i] = flags[i % 3];
12464 }
12465 }
12466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012468}
12469
12470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 * "getfsize({fname})" function
12472 */
12473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012474f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
12476 char_u *fname;
12477 struct stat st;
12478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482
12483 if (mch_stat((char *)fname, &st) >= 0)
12484 {
12485 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012490
12491 /* non-perfect check for overflow */
12492 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12493 rettv->vval.v_number = -2;
12494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012495 }
12496 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012497 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498}
12499
12500/*
12501 * "getftime({fname})" function
12502 */
12503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012504f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505{
12506 char_u *fname;
12507 struct stat st;
12508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510
12511 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515}
12516
12517/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012518 * "getftype({fname})" function
12519 */
12520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012521f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012522{
12523 char_u *fname;
12524 struct stat st;
12525 char_u *type = NULL;
12526 char *t;
12527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012531 if (mch_lstat((char *)fname, &st) >= 0)
12532 {
12533#ifdef S_ISREG
12534 if (S_ISREG(st.st_mode))
12535 t = "file";
12536 else if (S_ISDIR(st.st_mode))
12537 t = "dir";
12538# ifdef S_ISLNK
12539 else if (S_ISLNK(st.st_mode))
12540 t = "link";
12541# endif
12542# ifdef S_ISBLK
12543 else if (S_ISBLK(st.st_mode))
12544 t = "bdev";
12545# endif
12546# ifdef S_ISCHR
12547 else if (S_ISCHR(st.st_mode))
12548 t = "cdev";
12549# endif
12550# ifdef S_ISFIFO
12551 else if (S_ISFIFO(st.st_mode))
12552 t = "fifo";
12553# endif
12554# ifdef S_ISSOCK
12555 else if (S_ISSOCK(st.st_mode))
12556 t = "fifo";
12557# endif
12558 else
12559 t = "other";
12560#else
12561# ifdef S_IFMT
12562 switch (st.st_mode & S_IFMT)
12563 {
12564 case S_IFREG: t = "file"; break;
12565 case S_IFDIR: t = "dir"; break;
12566# ifdef S_IFLNK
12567 case S_IFLNK: t = "link"; break;
12568# endif
12569# ifdef S_IFBLK
12570 case S_IFBLK: t = "bdev"; break;
12571# endif
12572# ifdef S_IFCHR
12573 case S_IFCHR: t = "cdev"; break;
12574# endif
12575# ifdef S_IFIFO
12576 case S_IFIFO: t = "fifo"; break;
12577# endif
12578# ifdef S_IFSOCK
12579 case S_IFSOCK: t = "socket"; break;
12580# endif
12581 default: t = "other";
12582 }
12583# else
12584 if (mch_isdir(fname))
12585 t = "dir";
12586 else
12587 t = "file";
12588# endif
12589#endif
12590 type = vim_strsave((char_u *)t);
12591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012593}
12594
12595/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012596 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597 */
12598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012599f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600{
12601 linenr_T lnum;
12602 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012603 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604
12605 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012606 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012607 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012608 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012609 retlist = FALSE;
12610 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012612 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012614 retlist = TRUE;
12615 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012616
Bram Moolenaar342337a2005-07-21 21:11:17 +000012617 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012618}
12619
12620/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012621 * "getmatches()" function
12622 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012624f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012625{
12626#ifdef FEAT_SEARCH_EXTRA
12627 dict_T *dict;
12628 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012629 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012630
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012631 if (rettv_list_alloc(rettv) == OK)
12632 {
12633 while (cur != NULL)
12634 {
12635 dict = dict_alloc();
12636 if (dict == NULL)
12637 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012638 if (cur->match.regprog == NULL)
12639 {
12640 /* match added with matchaddpos() */
12641 for (i = 0; i < MAXPOSMATCH; ++i)
12642 {
12643 llpos_T *llpos;
12644 char buf[6];
12645 list_T *l;
12646
12647 llpos = &cur->pos.pos[i];
12648 if (llpos->lnum == 0)
12649 break;
12650 l = list_alloc();
12651 if (l == NULL)
12652 break;
12653 list_append_number(l, (varnumber_T)llpos->lnum);
12654 if (llpos->col > 0)
12655 {
12656 list_append_number(l, (varnumber_T)llpos->col);
12657 list_append_number(l, (varnumber_T)llpos->len);
12658 }
12659 sprintf(buf, "pos%d", i + 1);
12660 dict_add_list(dict, buf, l);
12661 }
12662 }
12663 else
12664 {
12665 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12666 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012667 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012668 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12669 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012670# ifdef FEAT_CONCEAL
12671 if (cur->conceal_char)
12672 {
12673 char_u buf[MB_MAXBYTES + 1];
12674
12675 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12676 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12677 }
12678# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012679 list_append_dict(rettv->vval.v_list, dict);
12680 cur = cur->next;
12681 }
12682 }
12683#endif
12684}
12685
12686/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012687 * "getpid()" function
12688 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012690f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012691{
12692 rettv->vval.v_number = mch_get_pid();
12693}
12694
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012695static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012696
12697/*
12698 * "getcurpos()" function
12699 */
12700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012701f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012702{
12703 getpos_both(argvars, rettv, TRUE);
12704}
12705
Bram Moolenaar18081e32008-02-20 19:11:07 +000012706/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012707 * "getpos(string)" function
12708 */
12709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012710f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012711{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012712 getpos_both(argvars, rettv, FALSE);
12713}
12714
12715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012716getpos_both(
12717 typval_T *argvars,
12718 typval_T *rettv,
12719 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012720{
Bram Moolenaara5525202006-03-02 22:52:09 +000012721 pos_T *fp;
12722 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012723 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012724
12725 if (rettv_list_alloc(rettv) == OK)
12726 {
12727 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012728 if (getcurpos)
12729 fp = &curwin->w_cursor;
12730 else
12731 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012732 if (fnum != -1)
12733 list_append_number(l, (varnumber_T)fnum);
12734 else
12735 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012736 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12737 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012738 list_append_number(l, (fp != NULL)
12739 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012740 : (varnumber_T)0);
12741 list_append_number(l,
12742#ifdef FEAT_VIRTUALEDIT
12743 (fp != NULL) ? (varnumber_T)fp->coladd :
12744#endif
12745 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012746 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012747 {
12748 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012749 list_append_number(l, curwin->w_curswant == MAXCOL ?
12750 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012751 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012752 }
12753 else
12754 rettv->vval.v_number = FALSE;
12755}
12756
12757/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012758 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012759 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012762{
12763#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012764 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012765#endif
12766
Bram Moolenaar2641f772005-03-25 21:58:17 +000012767#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012768 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012769 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012770 wp = NULL;
12771 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12772 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012773 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012774 if (wp == NULL)
12775 return;
12776 }
12777
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012778 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012779 }
12780#endif
12781}
12782
12783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 * "getreg()" function
12785 */
12786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012787f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
12789 char_u *strregname;
12790 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012791 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012792 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012795 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 strregname = get_tv_string_chk(&argvars[0]);
12798 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012799 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012801 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012802 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12803 return_list = get_tv_number_chk(&argvars[2], &error);
12804 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012807 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012808
12809 if (error)
12810 return;
12811
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 regname = (strregname == NULL ? '"' : *strregname);
12813 if (regname == 0)
12814 regname = '"';
12815
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012816 if (return_list)
12817 {
12818 rettv->v_type = VAR_LIST;
12819 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12820 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012821 if (rettv->vval.v_list != NULL)
12822 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012823 }
12824 else
12825 {
12826 rettv->v_type = VAR_STRING;
12827 rettv->vval.v_string = get_reg_contents(regname,
12828 arg2 ? GREG_EXPR_SRC : 0);
12829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830}
12831
12832/*
12833 * "getregtype()" function
12834 */
12835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012836f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
12838 char_u *strregname;
12839 int regname;
12840 char_u buf[NUMBUFLEN + 2];
12841 long reglen = 0;
12842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012843 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012844 {
12845 strregname = get_tv_string_chk(&argvars[0]);
12846 if (strregname == NULL) /* type error; errmsg already given */
12847 {
12848 rettv->v_type = VAR_STRING;
12849 rettv->vval.v_string = NULL;
12850 return;
12851 }
12852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 else
12854 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012855 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 regname = (strregname == NULL ? '"' : *strregname);
12858 if (regname == 0)
12859 regname = '"';
12860
12861 buf[0] = NUL;
12862 buf[1] = NUL;
12863 switch (get_reg_type(regname, &reglen))
12864 {
12865 case MLINE: buf[0] = 'V'; break;
12866 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 case MBLOCK:
12868 buf[0] = Ctrl_V;
12869 sprintf((char *)buf + 1, "%ld", reglen + 1);
12870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->v_type = VAR_STRING;
12873 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874}
12875
12876/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012877 * "gettabvar()" function
12878 */
12879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012881{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012882 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012883 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012884 dictitem_T *v;
12885 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012886 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012887
12888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = NULL;
12890
12891 varname = get_tv_string_chk(&argvars[1]);
12892 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12893 if (tp != NULL && varname != NULL)
12894 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012895 /* Set tp to be our tabpage, temporarily. Also set the window to the
12896 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012897 if (switch_win(&oldcurwin, &oldtabpage,
12898 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012899 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012900 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012901 /* look up the variable */
12902 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12903 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12904 if (v != NULL)
12905 {
12906 copy_tv(&v->di_tv, rettv);
12907 done = TRUE;
12908 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012909 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012910
12911 /* restore previous notion of curwin */
12912 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012913 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012914
12915 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012916 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012917}
12918
12919/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012920 * "gettabwinvar()" function
12921 */
12922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012923f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012924{
12925 getwinvar(argvars, rettv, 1);
12926}
12927
12928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 * "getwinposx()" function
12930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012932f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935#ifdef FEAT_GUI
12936 if (gui.in_use)
12937 {
12938 int x, y;
12939
12940 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942 }
12943#endif
12944}
12945
12946/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012947 * "win_findbuf()" function
12948 */
12949 static void
12950f_win_findbuf(typval_T *argvars, typval_T *rettv)
12951{
12952 if (rettv_list_alloc(rettv) != FAIL)
12953 win_findbuf(argvars, rettv->vval.v_list);
12954}
12955
12956/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012957 * "win_getid()" function
12958 */
12959 static void
12960f_win_getid(typval_T *argvars, typval_T *rettv)
12961{
12962 rettv->vval.v_number = win_getid(argvars);
12963}
12964
12965/*
12966 * "win_gotoid()" function
12967 */
12968 static void
12969f_win_gotoid(typval_T *argvars, typval_T *rettv)
12970{
12971 rettv->vval.v_number = win_gotoid(argvars);
12972}
12973
12974/*
12975 * "win_id2tabwin()" function
12976 */
12977 static void
12978f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12979{
12980 if (rettv_list_alloc(rettv) != FAIL)
12981 win_id2tabwin(argvars, rettv->vval.v_list);
12982}
12983
12984/*
12985 * "win_id2win()" function
12986 */
12987 static void
12988f_win_id2win(typval_T *argvars, typval_T *rettv)
12989{
12990 rettv->vval.v_number = win_id2win(argvars);
12991}
12992
12993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 * "getwinposy()" function
12995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012997f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000#ifdef FEAT_GUI
13001 if (gui.in_use)
13002 {
13003 int x, y;
13004
13005 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013006 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 }
13008#endif
13009}
13010
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013011/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013012 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013013 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013014 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013015find_win_by_nr(
13016 typval_T *vp,
13017 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013018{
13019#ifdef FEAT_WINDOWS
13020 win_T *wp;
13021#endif
13022 int nr;
13023
13024 nr = get_tv_number_chk(vp, NULL);
13025
13026#ifdef FEAT_WINDOWS
13027 if (nr < 0)
13028 return NULL;
13029 if (nr == 0)
13030 return curwin;
13031
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013032 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13033 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013034 if (--nr <= 0)
13035 break;
13036 return wp;
13037#else
13038 if (nr == 0 || nr == 1)
13039 return curwin;
13040 return NULL;
13041#endif
13042}
13043
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013045 * Find window specified by "wvp" in tabpage "tvp".
13046 */
13047 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013048find_tabwin(
13049 typval_T *wvp, /* VAR_UNKNOWN for current window */
13050 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013051{
13052 win_T *wp = NULL;
13053 tabpage_T *tp = NULL;
13054 long n;
13055
13056 if (wvp->v_type != VAR_UNKNOWN)
13057 {
13058 if (tvp->v_type != VAR_UNKNOWN)
13059 {
13060 n = get_tv_number(tvp);
13061 if (n >= 0)
13062 tp = find_tabpage(n);
13063 }
13064 else
13065 tp = curtab;
13066
13067 if (tp != NULL)
13068 wp = find_win_by_nr(wvp, tp);
13069 }
13070 else
13071 wp = curwin;
13072
13073 return wp;
13074}
13075
13076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 * "getwinvar()" function
13078 */
13079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013080f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013082 getwinvar(argvars, rettv, 0);
13083}
13084
13085/*
13086 * getwinvar() and gettabwinvar()
13087 */
13088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013089getwinvar(
13090 typval_T *argvars,
13091 typval_T *rettv,
13092 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013093{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013094 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013096 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013097 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013098 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013099#ifdef FEAT_WINDOWS
13100 win_T *oldcurwin;
13101 tabpage_T *oldtabpage;
13102 int need_switch_win;
13103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013105#ifdef FEAT_WINDOWS
13106 if (off == 1)
13107 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13108 else
13109 tp = curtab;
13110#endif
13111 win = find_win_by_nr(&argvars[off], tp);
13112 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013113 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013115 rettv->v_type = VAR_STRING;
13116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117
13118 if (win != NULL && varname != NULL)
13119 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013120#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013121 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013122 * otherwise the window is not valid. Only do this when needed,
13123 * autocommands get blocked. */
13124 need_switch_win = !(tp == curtab && win == curwin);
13125 if (!need_switch_win
13126 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13127#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013128 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013129 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013130 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013131 if (get_option_tv(&varname, rettv, 1) == OK)
13132 done = TRUE;
13133 }
13134 else
13135 {
13136 /* Look up the variable. */
13137 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13138 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13139 varname, FALSE);
13140 if (v != NULL)
13141 {
13142 copy_tv(&v->di_tv, rettv);
13143 done = TRUE;
13144 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013147
Bram Moolenaarba117c22015-09-29 16:53:22 +020013148#ifdef FEAT_WINDOWS
13149 if (need_switch_win)
13150 /* restore previous notion of curwin */
13151 restore_win(oldcurwin, oldtabpage, TRUE);
13152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 }
13154
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013155 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13156 /* use the default return value */
13157 copy_tv(&argvars[off + 2], rettv);
13158
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 --emsg_off;
13160}
13161
13162/*
13163 * "glob()" function
13164 */
13165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013166f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013168 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013170 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013172 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013173 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013175 if (argvars[1].v_type != VAR_UNKNOWN)
13176 {
13177 if (get_tv_number_chk(&argvars[1], &error))
13178 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013179 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013180 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013181 if (get_tv_number_chk(&argvars[2], &error))
13182 {
13183 rettv->v_type = VAR_LIST;
13184 rettv->vval.v_list = NULL;
13185 }
13186 if (argvars[3].v_type != VAR_UNKNOWN
13187 && get_tv_number_chk(&argvars[3], &error))
13188 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013189 }
13190 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013191 if (!error)
13192 {
13193 ExpandInit(&xpc);
13194 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013195 if (p_wic)
13196 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013197 if (rettv->v_type == VAR_STRING)
13198 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013199 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013200 else if (rettv_list_alloc(rettv) != FAIL)
13201 {
13202 int i;
13203
13204 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13205 NULL, options, WILD_ALL_KEEP);
13206 for (i = 0; i < xpc.xp_numfiles; i++)
13207 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13208
13209 ExpandCleanup(&xpc);
13210 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013211 }
13212 else
13213 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214}
13215
13216/*
13217 * "globpath()" function
13218 */
13219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013220f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013222 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013225 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013226 garray_T ga;
13227 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013229 /* When the optional second argument is non-zero, don't remove matches
13230 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013232 if (argvars[2].v_type != VAR_UNKNOWN)
13233 {
13234 if (get_tv_number_chk(&argvars[2], &error))
13235 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013236 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013237 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013238 if (get_tv_number_chk(&argvars[3], &error))
13239 {
13240 rettv->v_type = VAR_LIST;
13241 rettv->vval.v_list = NULL;
13242 }
13243 if (argvars[4].v_type != VAR_UNKNOWN
13244 && get_tv_number_chk(&argvars[4], &error))
13245 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013246 }
13247 }
13248 if (file != NULL && !error)
13249 {
13250 ga_init2(&ga, (int)sizeof(char_u *), 10);
13251 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13252 if (rettv->v_type == VAR_STRING)
13253 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13254 else if (rettv_list_alloc(rettv) != FAIL)
13255 for (i = 0; i < ga.ga_len; ++i)
13256 list_append_string(rettv->vval.v_list,
13257 ((char_u **)(ga.ga_data))[i], -1);
13258 ga_clear_strings(&ga);
13259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013260 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013261 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262}
13263
13264/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013265 * "glob2regpat()" function
13266 */
13267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013268f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013269{
13270 char_u *pat = get_tv_string_chk(&argvars[0]);
13271
13272 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013273 rettv->vval.v_string = (pat == NULL)
13274 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013275}
13276
13277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 * "has()" function
13279 */
13280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013281f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282{
13283 int i;
13284 char_u *name;
13285 int n = FALSE;
13286 static char *(has_list[]) =
13287 {
13288#ifdef AMIGA
13289 "amiga",
13290# ifdef FEAT_ARP
13291 "arp",
13292# endif
13293#endif
13294#ifdef __BEOS__
13295 "beos",
13296#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013297#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 "mac",
13299#endif
13300#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013301 "macunix", /* built with 'darwin' enabled */
13302#endif
13303#if defined(__APPLE__) && __APPLE__ == 1
13304 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306#ifdef __QNX__
13307 "qnx",
13308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#ifdef UNIX
13310 "unix",
13311#endif
13312#ifdef VMS
13313 "vms",
13314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#ifdef WIN32
13316 "win32",
13317#endif
13318#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13319 "win32unix",
13320#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013321#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 "win64",
13323#endif
13324#ifdef EBCDIC
13325 "ebcdic",
13326#endif
13327#ifndef CASE_INSENSITIVE_FILENAME
13328 "fname_case",
13329#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013330#ifdef HAVE_ACL
13331 "acl",
13332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333#ifdef FEAT_ARABIC
13334 "arabic",
13335#endif
13336#ifdef FEAT_AUTOCMD
13337 "autocmd",
13338#endif
13339#ifdef FEAT_BEVAL
13340 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013341# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13342 "balloon_multiline",
13343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344#endif
13345#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13346 "builtin_terms",
13347# ifdef ALL_BUILTIN_TCAPS
13348 "all_builtin_terms",
13349# endif
13350#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013351#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13352 || defined(FEAT_GUI_W32) \
13353 || defined(FEAT_GUI_MOTIF))
13354 "browsefilter",
13355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356#ifdef FEAT_BYTEOFF
13357 "byte_offset",
13358#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013360 "channel",
13361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef FEAT_CINDENT
13363 "cindent",
13364#endif
13365#ifdef FEAT_CLIENTSERVER
13366 "clientserver",
13367#endif
13368#ifdef FEAT_CLIPBOARD
13369 "clipboard",
13370#endif
13371#ifdef FEAT_CMDL_COMPL
13372 "cmdline_compl",
13373#endif
13374#ifdef FEAT_CMDHIST
13375 "cmdline_hist",
13376#endif
13377#ifdef FEAT_COMMENTS
13378 "comments",
13379#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013380#ifdef FEAT_CONCEAL
13381 "conceal",
13382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383#ifdef FEAT_CRYPT
13384 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013385 "crypt-blowfish",
13386 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387#endif
13388#ifdef FEAT_CSCOPE
13389 "cscope",
13390#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013391#ifdef FEAT_CURSORBIND
13392 "cursorbind",
13393#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013394#ifdef CURSOR_SHAPE
13395 "cursorshape",
13396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397#ifdef DEBUG
13398 "debug",
13399#endif
13400#ifdef FEAT_CON_DIALOG
13401 "dialog_con",
13402#endif
13403#ifdef FEAT_GUI_DIALOG
13404 "dialog_gui",
13405#endif
13406#ifdef FEAT_DIFF
13407 "diff",
13408#endif
13409#ifdef FEAT_DIGRAPHS
13410 "digraphs",
13411#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013412#ifdef FEAT_DIRECTX
13413 "directx",
13414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415#ifdef FEAT_DND
13416 "dnd",
13417#endif
13418#ifdef FEAT_EMACS_TAGS
13419 "emacs_tags",
13420#endif
13421 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013422 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_SEARCH_EXTRA
13424 "extra_search",
13425#endif
13426#ifdef FEAT_FKMAP
13427 "farsi",
13428#endif
13429#ifdef FEAT_SEARCHPATH
13430 "file_in_path",
13431#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013432#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013433 "filterpipe",
13434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435#ifdef FEAT_FIND_ID
13436 "find_in_path",
13437#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013438#ifdef FEAT_FLOAT
13439 "float",
13440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441#ifdef FEAT_FOLDING
13442 "folding",
13443#endif
13444#ifdef FEAT_FOOTER
13445 "footer",
13446#endif
13447#if !defined(USE_SYSTEM) && defined(UNIX)
13448 "fork",
13449#endif
13450#ifdef FEAT_GETTEXT
13451 "gettext",
13452#endif
13453#ifdef FEAT_GUI
13454 "gui",
13455#endif
13456#ifdef FEAT_GUI_ATHENA
13457# ifdef FEAT_GUI_NEXTAW
13458 "gui_neXtaw",
13459# else
13460 "gui_athena",
13461# endif
13462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463#ifdef FEAT_GUI_GTK
13464 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013465# ifdef USE_GTK3
13466 "gui_gtk3",
13467# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013469# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013471#ifdef FEAT_GUI_GNOME
13472 "gui_gnome",
13473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474#ifdef FEAT_GUI_MAC
13475 "gui_mac",
13476#endif
13477#ifdef FEAT_GUI_MOTIF
13478 "gui_motif",
13479#endif
13480#ifdef FEAT_GUI_PHOTON
13481 "gui_photon",
13482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483#ifdef FEAT_GUI_W32
13484 "gui_win32",
13485#endif
13486#ifdef FEAT_HANGULIN
13487 "hangul_input",
13488#endif
13489#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13490 "iconv",
13491#endif
13492#ifdef FEAT_INS_EXPAND
13493 "insert_expand",
13494#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013496 "job",
13497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498#ifdef FEAT_JUMPLIST
13499 "jumplist",
13500#endif
13501#ifdef FEAT_KEYMAP
13502 "keymap",
13503#endif
13504#ifdef FEAT_LANGMAP
13505 "langmap",
13506#endif
13507#ifdef FEAT_LIBCALL
13508 "libcall",
13509#endif
13510#ifdef FEAT_LINEBREAK
13511 "linebreak",
13512#endif
13513#ifdef FEAT_LISP
13514 "lispindent",
13515#endif
13516#ifdef FEAT_LISTCMDS
13517 "listcmds",
13518#endif
13519#ifdef FEAT_LOCALMAP
13520 "localmap",
13521#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013522#ifdef FEAT_LUA
13523# ifndef DYNAMIC_LUA
13524 "lua",
13525# endif
13526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527#ifdef FEAT_MENU
13528 "menu",
13529#endif
13530#ifdef FEAT_SESSION
13531 "mksession",
13532#endif
13533#ifdef FEAT_MODIFY_FNAME
13534 "modify_fname",
13535#endif
13536#ifdef FEAT_MOUSE
13537 "mouse",
13538#endif
13539#ifdef FEAT_MOUSESHAPE
13540 "mouseshape",
13541#endif
13542#if defined(UNIX) || defined(VMS)
13543# ifdef FEAT_MOUSE_DEC
13544 "mouse_dec",
13545# endif
13546# ifdef FEAT_MOUSE_GPM
13547 "mouse_gpm",
13548# endif
13549# ifdef FEAT_MOUSE_JSB
13550 "mouse_jsbterm",
13551# endif
13552# ifdef FEAT_MOUSE_NET
13553 "mouse_netterm",
13554# endif
13555# ifdef FEAT_MOUSE_PTERM
13556 "mouse_pterm",
13557# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013558# ifdef FEAT_MOUSE_SGR
13559 "mouse_sgr",
13560# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013561# ifdef FEAT_SYSMOUSE
13562 "mouse_sysmouse",
13563# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013564# ifdef FEAT_MOUSE_URXVT
13565 "mouse_urxvt",
13566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567# ifdef FEAT_MOUSE_XTERM
13568 "mouse_xterm",
13569# endif
13570#endif
13571#ifdef FEAT_MBYTE
13572 "multi_byte",
13573#endif
13574#ifdef FEAT_MBYTE_IME
13575 "multi_byte_ime",
13576#endif
13577#ifdef FEAT_MULTI_LANG
13578 "multi_lang",
13579#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013580#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013581#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013582 "mzscheme",
13583#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585#ifdef FEAT_OLE
13586 "ole",
13587#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013588 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#ifdef FEAT_PATH_EXTRA
13590 "path_extra",
13591#endif
13592#ifdef FEAT_PERL
13593#ifndef DYNAMIC_PERL
13594 "perl",
13595#endif
13596#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013597#ifdef FEAT_PERSISTENT_UNDO
13598 "persistent_undo",
13599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600#ifdef FEAT_PYTHON
13601#ifndef DYNAMIC_PYTHON
13602 "python",
13603#endif
13604#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013605#ifdef FEAT_PYTHON3
13606#ifndef DYNAMIC_PYTHON3
13607 "python3",
13608#endif
13609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610#ifdef FEAT_POSTSCRIPT
13611 "postscript",
13612#endif
13613#ifdef FEAT_PRINTER
13614 "printer",
13615#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013616#ifdef FEAT_PROFILE
13617 "profile",
13618#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013619#ifdef FEAT_RELTIME
13620 "reltime",
13621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622#ifdef FEAT_QUICKFIX
13623 "quickfix",
13624#endif
13625#ifdef FEAT_RIGHTLEFT
13626 "rightleft",
13627#endif
13628#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13629 "ruby",
13630#endif
13631#ifdef FEAT_SCROLLBIND
13632 "scrollbind",
13633#endif
13634#ifdef FEAT_CMDL_INFO
13635 "showcmd",
13636 "cmdline_info",
13637#endif
13638#ifdef FEAT_SIGNS
13639 "signs",
13640#endif
13641#ifdef FEAT_SMARTINDENT
13642 "smartindent",
13643#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013644#ifdef STARTUPTIME
13645 "startuptime",
13646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#ifdef FEAT_STL_OPT
13648 "statusline",
13649#endif
13650#ifdef FEAT_SUN_WORKSHOP
13651 "sun_workshop",
13652#endif
13653#ifdef FEAT_NETBEANS_INTG
13654 "netbeans_intg",
13655#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013656#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013657 "spell",
13658#endif
13659#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 "syntax",
13661#endif
13662#if defined(USE_SYSTEM) || !defined(UNIX)
13663 "system",
13664#endif
13665#ifdef FEAT_TAG_BINS
13666 "tag_binary",
13667#endif
13668#ifdef FEAT_TAG_OLDSTATIC
13669 "tag_old_static",
13670#endif
13671#ifdef FEAT_TAG_ANYWHITE
13672 "tag_any_white",
13673#endif
13674#ifdef FEAT_TCL
13675# ifndef DYNAMIC_TCL
13676 "tcl",
13677# endif
13678#endif
13679#ifdef TERMINFO
13680 "terminfo",
13681#endif
13682#ifdef FEAT_TERMRESPONSE
13683 "termresponse",
13684#endif
13685#ifdef FEAT_TEXTOBJ
13686 "textobjects",
13687#endif
13688#ifdef HAVE_TGETENT
13689 "tgetent",
13690#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013691#ifdef FEAT_TIMERS
13692 "timers",
13693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694#ifdef FEAT_TITLE
13695 "title",
13696#endif
13697#ifdef FEAT_TOOLBAR
13698 "toolbar",
13699#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013700#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13701 "unnamedplus",
13702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703#ifdef FEAT_USR_CMDS
13704 "user-commands", /* was accidentally included in 5.4 */
13705 "user_commands",
13706#endif
13707#ifdef FEAT_VIMINFO
13708 "viminfo",
13709#endif
13710#ifdef FEAT_VERTSPLIT
13711 "vertsplit",
13712#endif
13713#ifdef FEAT_VIRTUALEDIT
13714 "virtualedit",
13715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#ifdef FEAT_VISUALEXTRA
13718 "visualextra",
13719#endif
13720#ifdef FEAT_VREPLACE
13721 "vreplace",
13722#endif
13723#ifdef FEAT_WILDIGN
13724 "wildignore",
13725#endif
13726#ifdef FEAT_WILDMENU
13727 "wildmenu",
13728#endif
13729#ifdef FEAT_WINDOWS
13730 "windows",
13731#endif
13732#ifdef FEAT_WAK
13733 "winaltkeys",
13734#endif
13735#ifdef FEAT_WRITEBACKUP
13736 "writebackup",
13737#endif
13738#ifdef FEAT_XIM
13739 "xim",
13740#endif
13741#ifdef FEAT_XFONTSET
13742 "xfontset",
13743#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013744#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013745 "xpm",
13746 "xpm_w32", /* for backward compatibility */
13747#else
13748# if defined(HAVE_XPM)
13749 "xpm",
13750# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef USE_XSMP
13753 "xsmp",
13754#endif
13755#ifdef USE_XSMP_INTERACT
13756 "xsmp_interact",
13757#endif
13758#ifdef FEAT_XCLIPBOARD
13759 "xterm_clipboard",
13760#endif
13761#ifdef FEAT_XTERM_SAVE
13762 "xterm_save",
13763#endif
13764#if defined(UNIX) && defined(FEAT_X11)
13765 "X11",
13766#endif
13767 NULL
13768 };
13769
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013770 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 for (i = 0; has_list[i] != NULL; ++i)
13772 if (STRICMP(name, has_list[i]) == 0)
13773 {
13774 n = TRUE;
13775 break;
13776 }
13777
13778 if (n == FALSE)
13779 {
13780 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013781 {
13782 if (name[5] == '-'
13783 && STRLEN(name) > 11
13784 && vim_isdigit(name[6])
13785 && vim_isdigit(name[8])
13786 && vim_isdigit(name[10]))
13787 {
13788 int major = atoi((char *)name + 6);
13789 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013790
13791 /* Expect "patch-9.9.01234". */
13792 n = (major < VIM_VERSION_MAJOR
13793 || (major == VIM_VERSION_MAJOR
13794 && (minor < VIM_VERSION_MINOR
13795 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013796 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013797 }
13798 else
13799 n = has_patch(atoi((char *)name + 5));
13800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801 else if (STRICMP(name, "vim_starting") == 0)
13802 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013803#ifdef FEAT_MBYTE
13804 else if (STRICMP(name, "multi_byte_encoding") == 0)
13805 n = has_mbyte;
13806#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013807#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13808 else if (STRICMP(name, "balloon_multiline") == 0)
13809 n = multiline_balloon_available();
13810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811#ifdef DYNAMIC_TCL
13812 else if (STRICMP(name, "tcl") == 0)
13813 n = tcl_enabled(FALSE);
13814#endif
13815#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13816 else if (STRICMP(name, "iconv") == 0)
13817 n = iconv_enabled(FALSE);
13818#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013819#ifdef DYNAMIC_LUA
13820 else if (STRICMP(name, "lua") == 0)
13821 n = lua_enabled(FALSE);
13822#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013823#ifdef DYNAMIC_MZSCHEME
13824 else if (STRICMP(name, "mzscheme") == 0)
13825 n = mzscheme_enabled(FALSE);
13826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#ifdef DYNAMIC_RUBY
13828 else if (STRICMP(name, "ruby") == 0)
13829 n = ruby_enabled(FALSE);
13830#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013831#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832#ifdef DYNAMIC_PYTHON
13833 else if (STRICMP(name, "python") == 0)
13834 n = python_enabled(FALSE);
13835#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013836#endif
13837#ifdef FEAT_PYTHON3
13838#ifdef DYNAMIC_PYTHON3
13839 else if (STRICMP(name, "python3") == 0)
13840 n = python3_enabled(FALSE);
13841#endif
13842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843#ifdef DYNAMIC_PERL
13844 else if (STRICMP(name, "perl") == 0)
13845 n = perl_enabled(FALSE);
13846#endif
13847#ifdef FEAT_GUI
13848 else if (STRICMP(name, "gui_running") == 0)
13849 n = (gui.in_use || gui.starting);
13850# ifdef FEAT_GUI_W32
13851 else if (STRICMP(name, "gui_win32s") == 0)
13852 n = gui_is_win32s();
13853# endif
13854# ifdef FEAT_BROWSE
13855 else if (STRICMP(name, "browse") == 0)
13856 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13857# endif
13858#endif
13859#ifdef FEAT_SYN_HL
13860 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013861 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862#endif
13863#if defined(WIN3264)
13864 else if (STRICMP(name, "win95") == 0)
13865 n = mch_windows95();
13866#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013867#ifdef FEAT_NETBEANS_INTG
13868 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013869 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 }
13872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874}
13875
13876/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013877 * "has_key()" function
13878 */
13879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013880f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013881{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013882 if (argvars[0].v_type != VAR_DICT)
13883 {
13884 EMSG(_(e_dictreq));
13885 return;
13886 }
13887 if (argvars[0].vval.v_dict == NULL)
13888 return;
13889
13890 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013891 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013892}
13893
13894/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013895 * "haslocaldir()" function
13896 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013898f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013899{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013900 win_T *wp = NULL;
13901
13902 wp = find_tabwin(&argvars[0], &argvars[1]);
13903 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013904}
13905
13906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 * "hasmapto()" function
13908 */
13909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013910f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911{
13912 char_u *name;
13913 char_u *mode;
13914 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013915 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013918 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 mode = (char_u *)"nvo";
13920 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013922 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013923 if (argvars[2].v_type != VAR_UNKNOWN)
13924 abbr = get_tv_number(&argvars[2]);
13925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926
Bram Moolenaar2c932302006-03-18 21:42:09 +000013927 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013930 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931}
13932
13933/*
13934 * "histadd()" function
13935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013937f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939#ifdef FEAT_CMDHIST
13940 int histype;
13941 char_u *str;
13942 char_u buf[NUMBUFLEN];
13943#endif
13944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013945 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 if (check_restricted() || check_secure())
13947 return;
13948#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13950 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 if (histype >= 0)
13952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013953 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 if (*str != NUL)
13955 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013956 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 return;
13960 }
13961 }
13962#endif
13963}
13964
13965/*
13966 * "histdel()" function
13967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013969f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970{
13971#ifdef FEAT_CMDHIST
13972 int n;
13973 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13977 if (str == NULL)
13978 n = 0;
13979 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013982 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 else
13987 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 get_tv_string_buf(&argvars[1], buf));
13990 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991#endif
13992}
13993
13994/*
13995 * "histget()" function
13996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013998f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999{
14000#ifdef FEAT_CMDHIST
14001 int type;
14002 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14006 if (str == NULL)
14007 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 {
14010 type = get_histtype(str);
14011 if (argvars[1].v_type == VAR_UNKNOWN)
14012 idx = get_history_idx(type);
14013 else
14014 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14015 /* -1 on type error */
14016 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022}
14023
14024/*
14025 * "histnr()" function
14026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014028f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029{
14030 int i;
14031
14032#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 char_u *history = get_tv_string_chk(&argvars[0]);
14034
14035 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 if (i >= HIST_CMD && i < HIST_COUNT)
14037 i = get_history_idx(i);
14038 else
14039#endif
14040 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042}
14043
14044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 * "highlightID(name)" function
14046 */
14047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014048f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014051}
14052
14053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054 * "highlight_exists()" function
14055 */
14056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014057f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058{
14059 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14060}
14061
14062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 * "hostname()" function
14064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014066f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067{
14068 char_u hostname[256];
14069
14070 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014071 rettv->v_type = VAR_STRING;
14072 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073}
14074
14075/*
14076 * iconv() function
14077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014079f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080{
14081#ifdef FEAT_MBYTE
14082 char_u buf1[NUMBUFLEN];
14083 char_u buf2[NUMBUFLEN];
14084 char_u *from, *to, *str;
14085 vimconv_T vimconv;
14086#endif
14087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->v_type = VAR_STRING;
14089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090
14091#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 str = get_tv_string(&argvars[0]);
14093 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14094 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 vimconv.vc_type = CONV_NONE;
14096 convert_setup(&vimconv, from, to);
14097
14098 /* If the encodings are equal, no conversion needed. */
14099 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103
14104 convert_setup(&vimconv, NULL, NULL);
14105 vim_free(from);
14106 vim_free(to);
14107#endif
14108}
14109
14110/*
14111 * "indent()" function
14112 */
14113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014114f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115{
14116 linenr_T lnum;
14117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123}
14124
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014125/*
14126 * "index()" function
14127 */
14128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014129f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014130{
Bram Moolenaar33570922005-01-25 22:26:29 +000014131 list_T *l;
14132 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014133 long idx = 0;
14134 int ic = FALSE;
14135
14136 rettv->vval.v_number = -1;
14137 if (argvars[0].v_type != VAR_LIST)
14138 {
14139 EMSG(_(e_listreq));
14140 return;
14141 }
14142 l = argvars[0].vval.v_list;
14143 if (l != NULL)
14144 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014145 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014146 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014147 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 int error = FALSE;
14149
Bram Moolenaar758711c2005-02-02 23:11:38 +000014150 /* Start at specified item. Use the cached index that list_find()
14151 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014153 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014154 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 ic = get_tv_number_chk(&argvars[3], &error);
14156 if (error)
14157 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014158 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014159
Bram Moolenaar758711c2005-02-02 23:11:38 +000014160 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014161 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014162 {
14163 rettv->vval.v_number = idx;
14164 break;
14165 }
14166 }
14167}
14168
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169static int inputsecret_flag = 0;
14170
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014171static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014172
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014174 * This function is used by f_input() and f_inputdialog() functions. The third
14175 * argument to f_input() specifies the type of completion to use at the
14176 * prompt. The third argument to f_inputdialog() specifies the value to return
14177 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 */
14179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014180get_user_input(
14181 typval_T *argvars,
14182 typval_T *rettv,
14183 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 char_u *p = NULL;
14187 int c;
14188 char_u buf[NUMBUFLEN];
14189 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014191 int xp_type = EXPAND_NOTHING;
14192 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014194 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014195 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196
14197#ifdef NO_CONSOLE_INPUT
14198 /* While starting up, there is no place to enter text. */
14199 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201#endif
14202
14203 cmd_silent = FALSE; /* Want to see the prompt. */
14204 if (prompt != NULL)
14205 {
14206 /* Only the part of the message after the last NL is considered as
14207 * prompt for the command line */
14208 p = vim_strrchr(prompt, '\n');
14209 if (p == NULL)
14210 p = prompt;
14211 else
14212 {
14213 ++p;
14214 c = *p;
14215 *p = NUL;
14216 msg_start();
14217 msg_clr_eos();
14218 msg_puts_attr(prompt, echo_attr);
14219 msg_didout = FALSE;
14220 msg_starthere();
14221 *p = c;
14222 }
14223 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 if (argvars[1].v_type != VAR_UNKNOWN)
14226 {
14227 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14228 if (defstr != NULL)
14229 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014231 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014232 {
14233 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014234 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014235 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014236
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014237 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014238 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014239
Bram Moolenaar4463f292005-09-25 22:20:24 +000014240 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14241 if (xp_name == NULL)
14242 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014243
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014244 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014245
Bram Moolenaar4463f292005-09-25 22:20:24 +000014246 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14247 &xp_arg) == FAIL)
14248 return;
14249 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014250 }
14251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014253 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014254 int save_ex_normal_busy = ex_normal_busy;
14255 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014257 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14258 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014259 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014260 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014261 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014262 && argvars[1].v_type != VAR_UNKNOWN
14263 && argvars[2].v_type != VAR_UNKNOWN)
14264 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14265 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014266
14267 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014269 /* since the user typed this, no need to wait for return */
14270 need_wait_return = FALSE;
14271 msg_didout = FALSE;
14272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273 cmd_silent = cmd_silent_save;
14274}
14275
14276/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014277 * "input()" function
14278 * Also handles inputsecret() when inputsecret is set.
14279 */
14280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014281f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014282{
14283 get_user_input(argvars, rettv, FALSE);
14284}
14285
14286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 * "inputdialog()" function
14288 */
14289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014290f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292#if defined(FEAT_GUI_TEXTDIALOG)
14293 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14294 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14295 {
14296 char_u *message;
14297 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 message = get_tv_string_chk(&argvars[0]);
14301 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014302 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014303 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 else
14305 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014306 if (message != NULL && defstr != NULL
14307 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014308 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 else
14311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 if (message != NULL && defstr != NULL
14313 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014314 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 rettv->vval.v_string = vim_strsave(
14316 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 }
14322 else
14323#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014324 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325}
14326
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014327/*
14328 * "inputlist()" function
14329 */
14330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014331f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014332{
14333 listitem_T *li;
14334 int selected;
14335 int mouse_used;
14336
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014337#ifdef NO_CONSOLE_INPUT
14338 /* While starting up, there is no place to enter text. */
14339 if (no_console_input())
14340 return;
14341#endif
14342 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14343 {
14344 EMSG2(_(e_listarg), "inputlist()");
14345 return;
14346 }
14347
14348 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014349 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014350 lines_left = Rows; /* avoid more prompt */
14351 msg_scroll = TRUE;
14352 msg_clr_eos();
14353
14354 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14355 {
14356 msg_puts(get_tv_string(&li->li_tv));
14357 msg_putchar('\n');
14358 }
14359
14360 /* Ask for choice. */
14361 selected = prompt_for_number(&mouse_used);
14362 if (mouse_used)
14363 selected -= lines_left;
14364
14365 rettv->vval.v_number = selected;
14366}
14367
14368
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14370
14371/*
14372 * "inputrestore()" function
14373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014375f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376{
14377 if (ga_userinput.ga_len > 0)
14378 {
14379 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14381 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014382 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 }
14384 else if (p_verbose > 1)
14385 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014386 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 }
14389}
14390
14391/*
14392 * "inputsave()" function
14393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014395f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014397 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 if (ga_grow(&ga_userinput, 1) == OK)
14399 {
14400 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14401 + ga_userinput.ga_len);
14402 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014403 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 }
14405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407}
14408
14409/*
14410 * "inputsecret()" function
14411 */
14412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014413f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414{
14415 ++cmdline_star;
14416 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 --cmdline_star;
14419 --inputsecret_flag;
14420}
14421
14422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014423 * "insert()" function
14424 */
14425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427{
14428 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014429 listitem_T *item;
14430 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014432
14433 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014435 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014436 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014437 {
14438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 before = get_tv_number_chk(&argvars[2], &error);
14440 if (error)
14441 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442
Bram Moolenaar758711c2005-02-02 23:11:38 +000014443 if (before == l->lv_len)
14444 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014445 else
14446 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014447 item = list_find(l, before);
14448 if (item == NULL)
14449 {
14450 EMSGN(_(e_listidx), before);
14451 l = NULL;
14452 }
14453 }
14454 if (l != NULL)
14455 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014456 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014457 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014458 }
14459 }
14460}
14461
14462/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014463 * "invert(expr)" function
14464 */
14465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014466f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014467{
14468 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14469}
14470
14471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 * "isdirectory()" function
14473 */
14474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014475f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014480/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014481 * "islocked()" function
14482 */
14483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014485{
14486 lval_T lv;
14487 char_u *end;
14488 dictitem_T *di;
14489
14490 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014491 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14492 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014493 if (end != NULL && lv.ll_name != NULL)
14494 {
14495 if (*end != NUL)
14496 EMSG(_(e_trailing));
14497 else
14498 {
14499 if (lv.ll_tv == NULL)
14500 {
14501 if (check_changedtick(lv.ll_name))
14502 rettv->vval.v_number = 1; /* always locked */
14503 else
14504 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014505 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014506 if (di != NULL)
14507 {
14508 /* Consider a variable locked when:
14509 * 1. the variable itself is locked
14510 * 2. the value of the variable is locked.
14511 * 3. the List or Dict value is locked.
14512 */
14513 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14514 || tv_islocked(&di->di_tv));
14515 }
14516 }
14517 }
14518 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014519 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014520 else if (lv.ll_newkey != NULL)
14521 EMSG2(_(e_dictkey), lv.ll_newkey);
14522 else if (lv.ll_list != NULL)
14523 /* List item. */
14524 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14525 else
14526 /* Dictionary item. */
14527 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14528 }
14529 }
14530
14531 clear_lval(&lv);
14532}
14533
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014534#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14535/*
14536 * "isnan()" function
14537 */
14538 static void
14539f_isnan(typval_T *argvars, typval_T *rettv)
14540{
14541 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14542 && isnan(argvars[0].vval.v_float);
14543}
14544#endif
14545
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014546static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014547
14548/*
14549 * Turn a dict into a list:
14550 * "what" == 0: list of keys
14551 * "what" == 1: list of values
14552 * "what" == 2: list of items
14553 */
14554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014555dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014556{
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 list_T *l2;
14558 dictitem_T *di;
14559 hashitem_T *hi;
14560 listitem_T *li;
14561 listitem_T *li2;
14562 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014563 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014564
Bram Moolenaar8c711452005-01-14 21:53:12 +000014565 if (argvars[0].v_type != VAR_DICT)
14566 {
14567 EMSG(_(e_dictreq));
14568 return;
14569 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014570 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014571 return;
14572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014573 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014574 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014576 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014581 --todo;
14582 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014583
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014584 li = listitem_alloc();
14585 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014586 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014587 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014588
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014589 if (what == 0)
14590 {
14591 /* keys() */
14592 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014593 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014594 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14595 }
14596 else if (what == 1)
14597 {
14598 /* values() */
14599 copy_tv(&di->di_tv, &li->li_tv);
14600 }
14601 else
14602 {
14603 /* items() */
14604 l2 = list_alloc();
14605 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014606 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014607 li->li_tv.vval.v_list = l2;
14608 if (l2 == NULL)
14609 break;
14610 ++l2->lv_refcount;
14611
14612 li2 = listitem_alloc();
14613 if (li2 == NULL)
14614 break;
14615 list_append(l2, li2);
14616 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014617 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014618 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14619
14620 li2 = listitem_alloc();
14621 if (li2 == NULL)
14622 break;
14623 list_append(l2, li2);
14624 copy_tv(&di->di_tv, &li2->li_tv);
14625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 }
14627 }
14628}
14629
14630/*
14631 * "items(dict)" function
14632 */
14633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014634f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635{
14636 dict_list(argvars, rettv, 2);
14637}
14638
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014639#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014640/*
14641 * Get the job from the argument.
14642 * Returns NULL if the job is invalid.
14643 */
14644 static job_T *
14645get_job_arg(typval_T *tv)
14646{
14647 job_T *job;
14648
14649 if (tv->v_type != VAR_JOB)
14650 {
14651 EMSG2(_(e_invarg2), get_tv_string(tv));
14652 return NULL;
14653 }
14654 job = tv->vval.v_job;
14655
14656 if (job == NULL)
14657 EMSG(_("E916: not a valid job"));
14658 return job;
14659}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014660
Bram Moolenaar835dc632016-02-07 14:27:38 +010014661/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014662 * "job_getchannel()" function
14663 */
14664 static void
14665f_job_getchannel(typval_T *argvars, typval_T *rettv)
14666{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014667 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014668
Bram Moolenaar65edff82016-02-21 16:40:11 +010014669 if (job != NULL)
14670 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014671 rettv->v_type = VAR_CHANNEL;
14672 rettv->vval.v_channel = job->jv_channel;
14673 if (job->jv_channel != NULL)
14674 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014675 }
14676}
14677
14678/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014679 * "job_info()" function
14680 */
14681 static void
14682f_job_info(typval_T *argvars, typval_T *rettv)
14683{
14684 job_T *job = get_job_arg(&argvars[0]);
14685
14686 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14687 job_info(job, rettv->vval.v_dict);
14688}
14689
14690/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014691 * "job_setoptions()" function
14692 */
14693 static void
14694f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14695{
14696 job_T *job = get_job_arg(&argvars[0]);
14697 jobopt_T opt;
14698
14699 if (job == NULL)
14700 return;
14701 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014702 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014703 return;
14704 job_set_options(job, &opt);
14705}
14706
14707/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014708 * "job_start()" function
14709 */
14710 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014711f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014712{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014713 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014714 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014715}
14716
14717/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014718 * "job_status()" function
14719 */
14720 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014721f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014722{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014723 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014724
Bram Moolenaar65edff82016-02-21 16:40:11 +010014725 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014726 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014727 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014728 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014729 }
14730}
14731
14732/*
14733 * "job_stop()" function
14734 */
14735 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014736f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014737{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014738 job_T *job = get_job_arg(&argvars[0]);
14739
14740 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014741 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014742}
14743#endif
14744
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014746 * "join()" function
14747 */
14748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014749f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014750{
14751 garray_T ga;
14752 char_u *sep;
14753
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014754 if (argvars[0].v_type != VAR_LIST)
14755 {
14756 EMSG(_(e_listreq));
14757 return;
14758 }
14759 if (argvars[0].vval.v_list == NULL)
14760 return;
14761 if (argvars[1].v_type == VAR_UNKNOWN)
14762 sep = (char_u *)" ";
14763 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014765
14766 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014767
14768 if (sep != NULL)
14769 {
14770 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014771 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 ga_append(&ga, NUL);
14773 rettv->vval.v_string = (char_u *)ga.ga_data;
14774 }
14775 else
14776 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014777}
14778
14779/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014780 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014781 */
14782 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014783f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014784{
14785 js_read_T reader;
14786
14787 reader.js_buf = get_tv_string(&argvars[0]);
14788 reader.js_fill = NULL;
14789 reader.js_used = 0;
14790 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14791 EMSG(_(e_invarg));
14792}
14793
14794/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014795 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014796 */
14797 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014798f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014799{
14800 rettv->v_type = VAR_STRING;
14801 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14802}
14803
14804/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014805 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014806 */
14807 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014808f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014809{
14810 js_read_T reader;
14811
14812 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014813 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014814 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014815 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014816 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014817}
14818
14819/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014820 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014821 */
14822 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014823f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014824{
14825 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014826 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014827}
14828
14829/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014830 * "keys()" function
14831 */
14832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014833f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014834{
14835 dict_list(argvars, rettv, 0);
14836}
14837
14838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 * "last_buffer_nr()" function.
14840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014842f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843{
14844 int n = 0;
14845 buf_T *buf;
14846
14847 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14848 if (n < buf->b_fnum)
14849 n = buf->b_fnum;
14850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014852}
14853
14854/*
14855 * "len()" function
14856 */
14857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014858f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014859{
14860 switch (argvars[0].v_type)
14861 {
14862 case VAR_STRING:
14863 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->vval.v_number = (varnumber_T)STRLEN(
14865 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014866 break;
14867 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014869 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014870 case VAR_DICT:
14871 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14872 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014873 case VAR_UNKNOWN:
14874 case VAR_SPECIAL:
14875 case VAR_FLOAT:
14876 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014877 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014878 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014879 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014880 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881 break;
14882 }
14883}
14884
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014885static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014886
14887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014888libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014889{
14890#ifdef FEAT_LIBCALL
14891 char_u *string_in;
14892 char_u **string_result;
14893 int nr_result;
14894#endif
14895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014896 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014897 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014899
14900 if (check_restricted() || check_secure())
14901 return;
14902
14903#ifdef FEAT_LIBCALL
14904 /* The first two args must be strings, otherwise its meaningless */
14905 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14906 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014907 string_in = NULL;
14908 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014909 string_in = argvars[2].vval.v_string;
14910 if (type == VAR_NUMBER)
14911 string_result = NULL;
14912 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014914 if (mch_libcall(argvars[0].vval.v_string,
14915 argvars[1].vval.v_string,
14916 string_in,
14917 argvars[2].vval.v_number,
14918 string_result,
14919 &nr_result) == OK
14920 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014922 }
14923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924}
14925
14926/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014927 * "libcall()" function
14928 */
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931{
14932 libcall_common(argvars, rettv, VAR_STRING);
14933}
14934
14935/*
14936 * "libcallnr()" function
14937 */
14938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014939f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014940{
14941 libcall_common(argvars, rettv, VAR_NUMBER);
14942}
14943
14944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 * "line(string)" function
14946 */
14947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014948f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949{
14950 linenr_T lnum = 0;
14951 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014952 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014954 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 if (fp != NULL)
14956 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958}
14959
14960/*
14961 * "line2byte(lnum)" function
14962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014964f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014965{
14966#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968#else
14969 linenr_T lnum;
14970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14976 if (rettv->vval.v_number >= 0)
14977 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978#endif
14979}
14980
14981/*
14982 * "lispindent(lnum)" function
14983 */
14984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014985f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986{
14987#ifdef FEAT_LISP
14988 pos_T pos;
14989 linenr_T lnum;
14990
14991 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014992 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14994 {
14995 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 curwin->w_cursor = pos;
14998 }
14999 else
15000#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002}
15003
15004/*
15005 * "localtime()" function
15006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015008f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015010 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011}
15012
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015013static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
15015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015016get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017{
15018 char_u *keys;
15019 char_u *which;
15020 char_u buf[NUMBUFLEN];
15021 char_u *keys_buf = NULL;
15022 char_u *rhs;
15023 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015024 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015025 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015026 mapblock_T *mp;
15027 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028
15029 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->v_type = VAR_STRING;
15031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015033 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034 if (*keys == NUL)
15035 return;
15036
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015037 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015039 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015041 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015042 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015043 if (argvars[3].v_type != VAR_UNKNOWN)
15044 get_dict = get_tv_number(&argvars[3]);
15045 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047 else
15048 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 if (which == NULL)
15050 return;
15051
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 mode = get_map_mode(&which, 0);
15053
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015054 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015055 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015057
15058 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015060 /* Return a string. */
15061 if (rhs != NULL)
15062 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063
Bram Moolenaarbd743252010-10-20 21:23:33 +020015064 }
15065 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15066 {
15067 /* Return a dictionary. */
15068 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15069 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15070 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071
Bram Moolenaarbd743252010-10-20 21:23:33 +020015072 dict_add_nr_str(dict, "lhs", 0L, lhs);
15073 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15074 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15075 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15076 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15077 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15078 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015079 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015080 dict_add_nr_str(dict, "mode", 0L, mapmode);
15081
15082 vim_free(lhs);
15083 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 }
15085}
15086
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015087#ifdef FEAT_FLOAT
15088/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015089 * "log()" function
15090 */
15091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015092f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015093{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015094 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015095
15096 rettv->v_type = VAR_FLOAT;
15097 if (get_float_arg(argvars, &f) == OK)
15098 rettv->vval.v_float = log(f);
15099 else
15100 rettv->vval.v_float = 0.0;
15101}
15102
15103/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015104 * "log10()" function
15105 */
15106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015107f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015108{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015109 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015110
15111 rettv->v_type = VAR_FLOAT;
15112 if (get_float_arg(argvars, &f) == OK)
15113 rettv->vval.v_float = log10(f);
15114 else
15115 rettv->vval.v_float = 0.0;
15116}
15117#endif
15118
Bram Moolenaar1dced572012-04-05 16:54:08 +020015119#ifdef FEAT_LUA
15120/*
15121 * "luaeval()" function
15122 */
15123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015125{
15126 char_u *str;
15127 char_u buf[NUMBUFLEN];
15128
15129 str = get_tv_string_buf(&argvars[0], buf);
15130 do_luaeval(str, argvars + 1, rettv);
15131}
15132#endif
15133
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015135 * "map()" function
15136 */
15137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015138f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139{
15140 filter_map(argvars, rettv, TRUE);
15141}
15142
15143/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 */
15146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015147f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150}
15151
15152/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154 */
15155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015156f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159}
15160
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015161static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
15163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015164find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015166 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015167 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 char_u *pat;
15170 regmatch_T regmatch;
15171 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015172 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 char_u *save_cpo;
15174 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015175 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015176 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015177 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015178 list_T *l = NULL;
15179 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015181 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182
15183 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15184 save_cpo = p_cpo;
15185 p_cpo = (char_u *)"";
15186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015187 rettv->vval.v_number = -1;
15188 if (type == 3)
15189 {
15190 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015191 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015192 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015193 }
15194 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015196 rettv->v_type = VAR_STRING;
15197 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015200 if (argvars[0].v_type == VAR_LIST)
15201 {
15202 if ((l = argvars[0].vval.v_list) == NULL)
15203 goto theend;
15204 li = l->lv_first;
15205 }
15206 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015207 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015208 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015209 len = (long)STRLEN(str);
15210 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15213 if (pat == NULL)
15214 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015216 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015218 int error = FALSE;
15219
15220 start = get_tv_number_chk(&argvars[2], &error);
15221 if (error)
15222 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015223 if (l != NULL)
15224 {
15225 li = list_find(l, start);
15226 if (li == NULL)
15227 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015228 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015229 }
15230 else
15231 {
15232 if (start < 0)
15233 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015234 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015235 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015236 /* When "count" argument is there ignore matches before "start",
15237 * otherwise skip part of the string. Differs when pattern is "^"
15238 * or "\<". */
15239 if (argvars[3].v_type != VAR_UNKNOWN)
15240 startcol = start;
15241 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015242 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015243 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015244 len -= start;
15245 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015246 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015247
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015249 nth = get_tv_number_chk(&argvars[3], &error);
15250 if (error)
15251 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 }
15253
15254 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15255 if (regmatch.regprog != NULL)
15256 {
15257 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015258
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015259 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015260 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015261 if (l != NULL)
15262 {
15263 if (li == NULL)
15264 {
15265 match = FALSE;
15266 break;
15267 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015268 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015269 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015270 if (str == NULL)
15271 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015272 }
15273
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015274 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015275
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015277 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015278 if (l == NULL && !match)
15279 break;
15280
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015281 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015282 if (l != NULL)
15283 {
15284 li = li->li_next;
15285 ++idx;
15286 }
15287 else
15288 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015289#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015290 startcol = (colnr_T)(regmatch.startp[0]
15291 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015292#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015293 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015294#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015295 if (startcol > (colnr_T)len
15296 || str + startcol <= regmatch.startp[0])
15297 {
15298 match = FALSE;
15299 break;
15300 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015301 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015302 }
15303
15304 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015306 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015308 int i;
15309
15310 /* return list with matched string and submatches */
15311 for (i = 0; i < NSUBEXP; ++i)
15312 {
15313 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015314 {
15315 if (list_append_string(rettv->vval.v_list,
15316 (char_u *)"", 0) == FAIL)
15317 break;
15318 }
15319 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015320 regmatch.startp[i],
15321 (int)(regmatch.endp[i] - regmatch.startp[i]))
15322 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015323 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015324 }
15325 }
15326 else if (type == 2)
15327 {
15328 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015329 if (l != NULL)
15330 copy_tv(&li->li_tv, rettv);
15331 else
15332 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015334 }
15335 else if (l != NULL)
15336 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 else
15338 {
15339 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015340 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 (varnumber_T)(regmatch.startp[0] - str);
15342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015343 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015345 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346 }
15347 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015348 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 }
15350
15351theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015352 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 p_cpo = save_cpo;
15354}
15355
15356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357 * "match()" function
15358 */
15359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015360f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361{
15362 find_some_match(argvars, rettv, 1);
15363}
15364
15365/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015366 * "matchadd()" function
15367 */
15368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015369f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015370{
15371#ifdef FEAT_SEARCH_EXTRA
15372 char_u buf[NUMBUFLEN];
15373 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15374 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15375 int prio = 10; /* default priority */
15376 int id = -1;
15377 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015378 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015379
15380 rettv->vval.v_number = -1;
15381
15382 if (grp == NULL || pat == NULL)
15383 return;
15384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015385 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015386 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015387 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015388 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015389 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015390 if (argvars[4].v_type != VAR_UNKNOWN)
15391 {
15392 if (argvars[4].v_type != VAR_DICT)
15393 {
15394 EMSG(_(e_dictreq));
15395 return;
15396 }
15397 if (dict_find(argvars[4].vval.v_dict,
15398 (char_u *)"conceal", -1) != NULL)
15399 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15400 (char_u *)"conceal", FALSE);
15401 }
15402 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015403 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015404 if (error == TRUE)
15405 return;
15406 if (id >= 1 && id <= 3)
15407 {
15408 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15409 return;
15410 }
15411
Bram Moolenaar6561d522015-07-21 15:48:27 +020015412 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15413 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015414#endif
15415}
15416
15417/*
15418 * "matchaddpos()" function
15419 */
15420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015421f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015422{
15423#ifdef FEAT_SEARCH_EXTRA
15424 char_u buf[NUMBUFLEN];
15425 char_u *group;
15426 int prio = 10;
15427 int id = -1;
15428 int error = FALSE;
15429 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015430 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015431
15432 rettv->vval.v_number = -1;
15433
15434 group = get_tv_string_buf_chk(&argvars[0], buf);
15435 if (group == NULL)
15436 return;
15437
15438 if (argvars[1].v_type != VAR_LIST)
15439 {
15440 EMSG2(_(e_listarg), "matchaddpos()");
15441 return;
15442 }
15443 l = argvars[1].vval.v_list;
15444 if (l == NULL)
15445 return;
15446
15447 if (argvars[2].v_type != VAR_UNKNOWN)
15448 {
15449 prio = get_tv_number_chk(&argvars[2], &error);
15450 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015451 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015452 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015453 if (argvars[4].v_type != VAR_UNKNOWN)
15454 {
15455 if (argvars[4].v_type != VAR_DICT)
15456 {
15457 EMSG(_(e_dictreq));
15458 return;
15459 }
15460 if (dict_find(argvars[4].vval.v_dict,
15461 (char_u *)"conceal", -1) != NULL)
15462 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15463 (char_u *)"conceal", FALSE);
15464 }
15465 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015466 }
15467 if (error == TRUE)
15468 return;
15469
15470 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15471 if (id == 1 || id == 2)
15472 {
15473 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15474 return;
15475 }
15476
Bram Moolenaar6561d522015-07-21 15:48:27 +020015477 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15478 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015479#endif
15480}
15481
15482/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015483 * "matcharg()" function
15484 */
15485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015486f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015487{
15488 if (rettv_list_alloc(rettv) == OK)
15489 {
15490#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015491 int id = get_tv_number(&argvars[0]);
15492 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015493
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015494 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015495 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015496 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15497 {
15498 list_append_string(rettv->vval.v_list,
15499 syn_id2name(m->hlg_id), -1);
15500 list_append_string(rettv->vval.v_list, m->pattern, -1);
15501 }
15502 else
15503 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015504 list_append_string(rettv->vval.v_list, NULL, -1);
15505 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015506 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015507 }
15508#endif
15509 }
15510}
15511
15512/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015513 * "matchdelete()" function
15514 */
15515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015516f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015517{
15518#ifdef FEAT_SEARCH_EXTRA
15519 rettv->vval.v_number = match_delete(curwin,
15520 (int)get_tv_number(&argvars[0]), TRUE);
15521#endif
15522}
15523
15524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015525 * "matchend()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529{
15530 find_some_match(argvars, rettv, 0);
15531}
15532
15533/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015534 * "matchlist()" function
15535 */
15536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015537f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015538{
15539 find_some_match(argvars, rettv, 3);
15540}
15541
15542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015543 * "matchstr()" function
15544 */
15545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015546f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015547{
15548 find_some_match(argvars, rettv, 2);
15549}
15550
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015551static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015552
15553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015554max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015555{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015556 long n = 0;
15557 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015559
15560 if (argvars[0].v_type == VAR_LIST)
15561 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015562 list_T *l;
15563 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015564
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015565 l = argvars[0].vval.v_list;
15566 if (l != NULL)
15567 {
15568 li = l->lv_first;
15569 if (li != NULL)
15570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015571 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015572 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015573 {
15574 li = li->li_next;
15575 if (li == NULL)
15576 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015577 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015578 if (domax ? i > n : i < n)
15579 n = i;
15580 }
15581 }
15582 }
15583 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015584 else if (argvars[0].v_type == VAR_DICT)
15585 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015587 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015589 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015590
15591 d = argvars[0].vval.v_dict;
15592 if (d != NULL)
15593 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015594 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015596 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015597 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015598 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015599 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015601 if (first)
15602 {
15603 n = i;
15604 first = FALSE;
15605 }
15606 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015607 n = i;
15608 }
15609 }
15610 }
15611 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015612 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015613 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015615}
15616
15617/*
15618 * "max()" function
15619 */
15620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015621f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015622{
15623 max_min(argvars, rettv, TRUE);
15624}
15625
15626/*
15627 * "min()" function
15628 */
15629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015630f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015631{
15632 max_min(argvars, rettv, FALSE);
15633}
15634
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015635static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015636
15637/*
15638 * Create the directory in which "dir" is located, and higher levels when
15639 * needed.
15640 */
15641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015642mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015643{
15644 char_u *p;
15645 char_u *updir;
15646 int r = FAIL;
15647
15648 /* Get end of directory name in "dir".
15649 * We're done when it's "/" or "c:/". */
15650 p = gettail_sep(dir);
15651 if (p <= get_past_head(dir))
15652 return OK;
15653
15654 /* If the directory exists we're done. Otherwise: create it.*/
15655 updir = vim_strnsave(dir, (int)(p - dir));
15656 if (updir == NULL)
15657 return FAIL;
15658 if (mch_isdir(updir))
15659 r = OK;
15660 else if (mkdir_recurse(updir, prot) == OK)
15661 r = vim_mkdir_emsg(updir, prot);
15662 vim_free(updir);
15663 return r;
15664}
15665
15666#ifdef vim_mkdir
15667/*
15668 * "mkdir()" function
15669 */
15670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015671f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015672{
15673 char_u *dir;
15674 char_u buf[NUMBUFLEN];
15675 int prot = 0755;
15676
15677 rettv->vval.v_number = FAIL;
15678 if (check_restricted() || check_secure())
15679 return;
15680
15681 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015682 if (*dir == NUL)
15683 rettv->vval.v_number = FAIL;
15684 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015685 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015686 if (*gettail(dir) == NUL)
15687 /* remove trailing slashes */
15688 *gettail_sep(dir) = NUL;
15689
15690 if (argvars[1].v_type != VAR_UNKNOWN)
15691 {
15692 if (argvars[2].v_type != VAR_UNKNOWN)
15693 prot = get_tv_number_chk(&argvars[2], NULL);
15694 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15695 mkdir_recurse(dir, prot);
15696 }
15697 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015698 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015699}
15700#endif
15701
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 * "mode()" function
15704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015706f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015708 char_u buf[3];
15709
15710 buf[1] = NUL;
15711 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 if (VIsual_active)
15714 {
15715 if (VIsual_select)
15716 buf[0] = VIsual_mode + 's' - 'v';
15717 else
15718 buf[0] = VIsual_mode;
15719 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015720 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015721 || State == CONFIRM)
15722 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015724 if (State == ASKMORE)
15725 buf[1] = 'm';
15726 else if (State == CONFIRM)
15727 buf[1] = '?';
15728 }
15729 else if (State == EXTERNCMD)
15730 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 else if (State & INSERT)
15732 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015733#ifdef FEAT_VREPLACE
15734 if (State & VREPLACE_FLAG)
15735 {
15736 buf[0] = 'R';
15737 buf[1] = 'v';
15738 }
15739 else
15740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741 if (State & REPLACE_FLAG)
15742 buf[0] = 'R';
15743 else
15744 buf[0] = 'i';
15745 }
15746 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015749 if (exmode_active)
15750 buf[1] = 'v';
15751 }
15752 else if (exmode_active)
15753 {
15754 buf[0] = 'c';
15755 buf[1] = 'e';
15756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015760 if (finish_op)
15761 buf[1] = 'o';
15762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015764 /* Clear out the minor mode when the argument is not a non-zero number or
15765 * non-empty string. */
15766 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015767 buf[1] = NUL;
15768
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769 rettv->vval.v_string = vim_strsave(buf);
15770 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771}
15772
Bram Moolenaar429fa852013-04-15 12:27:36 +020015773#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015774/*
15775 * "mzeval()" function
15776 */
15777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015778f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015779{
15780 char_u *str;
15781 char_u buf[NUMBUFLEN];
15782
15783 str = get_tv_string_buf(&argvars[0], buf);
15784 do_mzeval(str, rettv);
15785}
Bram Moolenaar75676462013-01-30 14:55:42 +010015786
15787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015788mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015789{
15790 typval_T argvars[3];
15791
15792 argvars[0].v_type = VAR_STRING;
15793 argvars[0].vval.v_string = name;
15794 copy_tv(args, &argvars[1]);
15795 argvars[2].v_type = VAR_UNKNOWN;
15796 f_call(argvars, rettv);
15797 clear_tv(&argvars[1]);
15798}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015799#endif
15800
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015802 * "nextnonblank()" function
15803 */
15804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015805f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806{
15807 linenr_T lnum;
15808
15809 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812 {
15813 lnum = 0;
15814 break;
15815 }
15816 if (*skipwhite(ml_get(lnum)) != NUL)
15817 break;
15818 }
15819 rettv->vval.v_number = lnum;
15820}
15821
15822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 * "nr2char()" function
15824 */
15825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015826f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827{
15828 char_u buf[NUMBUFLEN];
15829
15830#ifdef FEAT_MBYTE
15831 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015832 {
15833 int utf8 = 0;
15834
15835 if (argvars[1].v_type != VAR_UNKNOWN)
15836 utf8 = get_tv_number_chk(&argvars[1], NULL);
15837 if (utf8)
15838 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15839 else
15840 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 else
15843#endif
15844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015845 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 buf[1] = NUL;
15847 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015848 rettv->v_type = VAR_STRING;
15849 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015850}
15851
15852/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015853 * "or(expr, expr)" function
15854 */
15855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015856f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015857{
15858 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15859 | get_tv_number_chk(&argvars[1], NULL);
15860}
15861
15862/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015863 * "pathshorten()" function
15864 */
15865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015866f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015867{
15868 char_u *p;
15869
15870 rettv->v_type = VAR_STRING;
15871 p = get_tv_string_chk(&argvars[0]);
15872 if (p == NULL)
15873 rettv->vval.v_string = NULL;
15874 else
15875 {
15876 p = vim_strsave(p);
15877 rettv->vval.v_string = p;
15878 if (p != NULL)
15879 shorten_dir(p);
15880 }
15881}
15882
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015883#ifdef FEAT_PERL
15884/*
15885 * "perleval()" function
15886 */
15887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015888f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015889{
15890 char_u *str;
15891 char_u buf[NUMBUFLEN];
15892
15893 str = get_tv_string_buf(&argvars[0], buf);
15894 do_perleval(str, rettv);
15895}
15896#endif
15897
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015898#ifdef FEAT_FLOAT
15899/*
15900 * "pow()" function
15901 */
15902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015903f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015904{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015905 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015906
15907 rettv->v_type = VAR_FLOAT;
15908 if (get_float_arg(argvars, &fx) == OK
15909 && get_float_arg(&argvars[1], &fy) == OK)
15910 rettv->vval.v_float = pow(fx, fy);
15911 else
15912 rettv->vval.v_float = 0.0;
15913}
15914#endif
15915
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015916/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917 * "prevnonblank()" function
15918 */
15919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015920f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015921{
15922 linenr_T lnum;
15923
15924 lnum = get_tv_lnum(argvars);
15925 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15926 lnum = 0;
15927 else
15928 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15929 --lnum;
15930 rettv->vval.v_number = lnum;
15931}
15932
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015933/* This dummy va_list is here because:
15934 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15935 * - locally in the function results in a "used before set" warning
15936 * - using va_start() to initialize it gives "function with fixed args" error */
15937static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015938
Bram Moolenaar8c711452005-01-14 21:53:12 +000015939/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015940 * "printf()" function
15941 */
15942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015944{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015945 char_u buf[NUMBUFLEN];
15946 int len;
15947 char_u *s;
15948 int saved_did_emsg = did_emsg;
15949 char *fmt;
15950
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015951 rettv->v_type = VAR_STRING;
15952 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015953
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015954 /* Get the required length, allocate the buffer and do it for real. */
15955 did_emsg = FALSE;
15956 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15957 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15958 if (!did_emsg)
15959 {
15960 s = alloc(len + 1);
15961 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015962 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015963 rettv->vval.v_string = s;
15964 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015965 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015966 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015967 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015968}
15969
15970/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015971 * "pumvisible()" function
15972 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015975{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015976#ifdef FEAT_INS_EXPAND
15977 if (pum_visible())
15978 rettv->vval.v_number = 1;
15979#endif
15980}
15981
Bram Moolenaardb913952012-06-29 12:54:53 +020015982#ifdef FEAT_PYTHON3
15983/*
15984 * "py3eval()" function
15985 */
15986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015987f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015988{
15989 char_u *str;
15990 char_u buf[NUMBUFLEN];
15991
15992 str = get_tv_string_buf(&argvars[0], buf);
15993 do_py3eval(str, rettv);
15994}
15995#endif
15996
15997#ifdef FEAT_PYTHON
15998/*
15999 * "pyeval()" function
16000 */
16001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016002f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016003{
16004 char_u *str;
16005 char_u buf[NUMBUFLEN];
16006
16007 str = get_tv_string_buf(&argvars[0], buf);
16008 do_pyeval(str, rettv);
16009}
16010#endif
16011
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016013 * "range()" function
16014 */
16015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016016f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016017{
16018 long start;
16019 long end;
16020 long stride = 1;
16021 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016024 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016025 if (argvars[1].v_type == VAR_UNKNOWN)
16026 {
16027 end = start - 1;
16028 start = 0;
16029 }
16030 else
16031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016033 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016035 }
16036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016037 if (error)
16038 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016039 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016040 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016041 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016042 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016043 else
16044 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016046 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016047 if (list_append_number(rettv->vval.v_list,
16048 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016049 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016050 }
16051}
16052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016053/*
16054 * "readfile()" function
16055 */
16056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016057f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016058{
16059 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016060 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016061 char_u *fname;
16062 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016063 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16064 int io_size = sizeof(buf);
16065 int readlen; /* size of last fread() */
16066 char_u *prev = NULL; /* previously read bytes, if any */
16067 long prevlen = 0; /* length of data in prev */
16068 long prevsize = 0; /* size of prev buffer */
16069 long maxline = MAXLNUM;
16070 long cnt = 0;
16071 char_u *p; /* position in buf */
16072 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016073
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016074 if (argvars[1].v_type != VAR_UNKNOWN)
16075 {
16076 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16077 binary = TRUE;
16078 if (argvars[2].v_type != VAR_UNKNOWN)
16079 maxline = get_tv_number(&argvars[2]);
16080 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016081
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016082 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016083 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084
16085 /* Always open the file in binary mode, library functions have a mind of
16086 * their own about CR-LF conversion. */
16087 fname = get_tv_string(&argvars[0]);
16088 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16089 {
16090 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16091 return;
16092 }
16093
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016094 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016095 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016096 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016097
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016098 /* This for loop processes what was read, but is also entered at end
16099 * of file so that either:
16100 * - an incomplete line gets written
16101 * - a "binary" file gets an empty line at the end if it ends in a
16102 * newline. */
16103 for (p = buf, start = buf;
16104 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16105 ++p)
16106 {
16107 if (*p == '\n' || readlen <= 0)
16108 {
16109 listitem_T *li;
16110 char_u *s = NULL;
16111 long_u len = p - start;
16112
16113 /* Finished a line. Remove CRs before NL. */
16114 if (readlen > 0 && !binary)
16115 {
16116 while (len > 0 && start[len - 1] == '\r')
16117 --len;
16118 /* removal may cross back to the "prev" string */
16119 if (len == 0)
16120 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16121 --prevlen;
16122 }
16123 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016124 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016125 else
16126 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016127 /* Change "prev" buffer to be the right size. This way
16128 * the bytes are only copied once, and very long lines are
16129 * allocated only once. */
16130 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016132 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016133 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016134 prev = NULL; /* the list will own the string */
16135 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016136 }
16137 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016138 if (s == NULL)
16139 {
16140 do_outofmem_msg((long_u) prevlen + len + 1);
16141 failed = TRUE;
16142 break;
16143 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016145 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016146 {
16147 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016148 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016149 break;
16150 }
16151 li->li_tv.v_type = VAR_STRING;
16152 li->li_tv.v_lock = 0;
16153 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016154 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016155
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016156 start = p + 1; /* step over newline */
16157 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158 break;
16159 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016160 else if (*p == NUL)
16161 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016162#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016163 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16164 * when finding the BF and check the previous two bytes. */
16165 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016166 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016167 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16168 * + 1, these may be in the "prev" string. */
16169 char_u back1 = p >= buf + 1 ? p[-1]
16170 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16171 char_u back2 = p >= buf + 2 ? p[-2]
16172 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16173 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016174
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016175 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016176 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016177 char_u *dest = p - 2;
16178
16179 /* Usually a BOM is at the beginning of a file, and so at
16180 * the beginning of a line; then we can just step over it.
16181 */
16182 if (start == dest)
16183 start = p + 1;
16184 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016185 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016186 /* have to shuffle buf to close gap */
16187 int adjust_prevlen = 0;
16188
16189 if (dest < buf)
16190 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016191 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016192 dest = buf;
16193 }
16194 if (readlen > p - buf + 1)
16195 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16196 readlen -= 3 - adjust_prevlen;
16197 prevlen -= adjust_prevlen;
16198 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016199 }
16200 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016201 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016202#endif
16203 } /* for */
16204
16205 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16206 break;
16207 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016208 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016209 /* There's part of a line in buf, store it in "prev". */
16210 if (p - start + prevlen >= prevsize)
16211 {
16212 /* need bigger "prev" buffer */
16213 char_u *newprev;
16214
16215 /* A common use case is ordinary text files and "prev" gets a
16216 * fragment of a line, so the first allocation is made
16217 * small, to avoid repeatedly 'allocing' large and
16218 * 'reallocing' small. */
16219 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016220 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016221 else
16222 {
16223 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016224 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016225 prevsize = grow50pc > growmin ? grow50pc : growmin;
16226 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016227 newprev = prev == NULL ? alloc(prevsize)
16228 : vim_realloc(prev, prevsize);
16229 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016230 {
16231 do_outofmem_msg((long_u)prevsize);
16232 failed = TRUE;
16233 break;
16234 }
16235 prev = newprev;
16236 }
16237 /* Add the line part to end of "prev". */
16238 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016239 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016240 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016241 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016242
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016243 /*
16244 * For a negative line count use only the lines at the end of the file,
16245 * free the rest.
16246 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016247 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016248 while (cnt > -maxline)
16249 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016250 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016251 --cnt;
16252 }
16253
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016254 if (failed)
16255 {
16256 list_free(rettv->vval.v_list, TRUE);
16257 /* readfile doc says an empty list is returned on error */
16258 rettv->vval.v_list = list_alloc();
16259 }
16260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016261 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016262 fclose(fd);
16263}
16264
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016265#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016266static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016267
16268/*
16269 * Convert a List to proftime_T.
16270 * Return FAIL when there is something wrong.
16271 */
16272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016273list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016274{
16275 long n1, n2;
16276 int error = FALSE;
16277
16278 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16279 || arg->vval.v_list->lv_len != 2)
16280 return FAIL;
16281 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16282 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16283# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016284 tm->HighPart = n1;
16285 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016286# else
16287 tm->tv_sec = n1;
16288 tm->tv_usec = n2;
16289# endif
16290 return error ? FAIL : OK;
16291}
16292#endif /* FEAT_RELTIME */
16293
16294/*
16295 * "reltime()" function
16296 */
16297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016298f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016299{
16300#ifdef FEAT_RELTIME
16301 proftime_T res;
16302 proftime_T start;
16303
16304 if (argvars[0].v_type == VAR_UNKNOWN)
16305 {
16306 /* No arguments: get current time. */
16307 profile_start(&res);
16308 }
16309 else if (argvars[1].v_type == VAR_UNKNOWN)
16310 {
16311 if (list2proftime(&argvars[0], &res) == FAIL)
16312 return;
16313 profile_end(&res);
16314 }
16315 else
16316 {
16317 /* Two arguments: compute the difference. */
16318 if (list2proftime(&argvars[0], &start) == FAIL
16319 || list2proftime(&argvars[1], &res) == FAIL)
16320 return;
16321 profile_sub(&res, &start);
16322 }
16323
16324 if (rettv_list_alloc(rettv) == OK)
16325 {
16326 long n1, n2;
16327
16328# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016329 n1 = res.HighPart;
16330 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016331# else
16332 n1 = res.tv_sec;
16333 n2 = res.tv_usec;
16334# endif
16335 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16336 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16337 }
16338#endif
16339}
16340
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016341#ifdef FEAT_FLOAT
16342/*
16343 * "reltimefloat()" function
16344 */
16345 static void
16346f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16347{
16348# ifdef FEAT_RELTIME
16349 proftime_T tm;
16350# endif
16351
16352 rettv->v_type = VAR_FLOAT;
16353 rettv->vval.v_float = 0;
16354# ifdef FEAT_RELTIME
16355 if (list2proftime(&argvars[0], &tm) == OK)
16356 rettv->vval.v_float = profile_float(&tm);
16357# endif
16358}
16359#endif
16360
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016361/*
16362 * "reltimestr()" function
16363 */
16364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016365f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016366{
16367#ifdef FEAT_RELTIME
16368 proftime_T tm;
16369#endif
16370
16371 rettv->v_type = VAR_STRING;
16372 rettv->vval.v_string = NULL;
16373#ifdef FEAT_RELTIME
16374 if (list2proftime(&argvars[0], &tm) == OK)
16375 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16376#endif
16377}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016378
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016380static void make_connection(void);
16381static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382
16383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016384make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385{
16386 if (X_DISPLAY == NULL
16387# ifdef FEAT_GUI
16388 && !gui.in_use
16389# endif
16390 )
16391 {
16392 x_force_connect = TRUE;
16393 setup_term_clip();
16394 x_force_connect = FALSE;
16395 }
16396}
16397
16398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016399check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400{
16401 make_connection();
16402 if (X_DISPLAY == NULL)
16403 {
16404 EMSG(_("E240: No connection to Vim server"));
16405 return FAIL;
16406 }
16407 return OK;
16408}
16409#endif
16410
16411#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016412static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016413
16414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016415remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416{
16417 char_u *server_name;
16418 char_u *keys;
16419 char_u *r = NULL;
16420 char_u buf[NUMBUFLEN];
16421# ifdef WIN32
16422 HWND w;
16423# else
16424 Window w;
16425# endif
16426
16427 if (check_restricted() || check_secure())
16428 return;
16429
16430# ifdef FEAT_X11
16431 if (check_connection() == FAIL)
16432 return;
16433# endif
16434
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 server_name = get_tv_string_chk(&argvars[0]);
16436 if (server_name == NULL)
16437 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016438 keys = get_tv_string_buf(&argvars[1], buf);
16439# ifdef WIN32
16440 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16441# else
16442 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16443 < 0)
16444# endif
16445 {
16446 if (r != NULL)
16447 EMSG(r); /* sending worked but evaluation failed */
16448 else
16449 EMSG2(_("E241: Unable to send to %s"), server_name);
16450 return;
16451 }
16452
16453 rettv->vval.v_string = r;
16454
16455 if (argvars[2].v_type != VAR_UNKNOWN)
16456 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016457 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016458 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016460
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016461 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 v.di_tv.v_type = VAR_STRING;
16463 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016464 idvar = get_tv_string_chk(&argvars[2]);
16465 if (idvar != NULL)
16466 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016467 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 }
16469}
16470#endif
16471
16472/*
16473 * "remote_expr()" function
16474 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016476f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477{
16478 rettv->v_type = VAR_STRING;
16479 rettv->vval.v_string = NULL;
16480#ifdef FEAT_CLIENTSERVER
16481 remote_common(argvars, rettv, TRUE);
16482#endif
16483}
16484
16485/*
16486 * "remote_foreground()" function
16487 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016489f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491#ifdef FEAT_CLIENTSERVER
16492# ifdef WIN32
16493 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016494 {
16495 char_u *server_name = get_tv_string_chk(&argvars[0]);
16496
16497 if (server_name != NULL)
16498 serverForeground(server_name);
16499 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500# else
16501 /* Send a foreground() expression to the server. */
16502 argvars[1].v_type = VAR_STRING;
16503 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16504 argvars[2].v_type = VAR_UNKNOWN;
16505 remote_common(argvars, rettv, TRUE);
16506 vim_free(argvars[1].vval.v_string);
16507# endif
16508#endif
16509}
16510
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016512f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513{
16514#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016516 char_u *s = NULL;
16517# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016518 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521
16522 if (check_restricted() || check_secure())
16523 {
16524 rettv->vval.v_number = -1;
16525 return;
16526 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016527 serverid = get_tv_string_chk(&argvars[0]);
16528 if (serverid == NULL)
16529 {
16530 rettv->vval.v_number = -1;
16531 return; /* type error; errmsg already given */
16532 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016534 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016535 if (n == 0)
16536 rettv->vval.v_number = -1;
16537 else
16538 {
16539 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16540 rettv->vval.v_number = (s != NULL);
16541 }
16542# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543 if (check_connection() == FAIL)
16544 return;
16545
16546 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016547 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548# endif
16549
16550 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 char_u *retvar;
16553
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 v.di_tv.v_type = VAR_STRING;
16555 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 retvar = get_tv_string_chk(&argvars[1]);
16557 if (retvar != NULL)
16558 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016559 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016560 }
16561#else
16562 rettv->vval.v_number = -1;
16563#endif
16564}
16565
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568{
16569 char_u *r = NULL;
16570
16571#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016572 char_u *serverid = get_tv_string_chk(&argvars[0]);
16573
16574 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575 {
16576# ifdef WIN32
16577 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016578 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016580 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016581 if (n != 0)
16582 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16583 if (r == NULL)
16584# else
16585 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587# endif
16588 EMSG(_("E277: Unable to read a server reply"));
16589 }
16590#endif
16591 rettv->v_type = VAR_STRING;
16592 rettv->vval.v_string = r;
16593}
16594
16595/*
16596 * "remote_send()" function
16597 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016599f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600{
16601 rettv->v_type = VAR_STRING;
16602 rettv->vval.v_string = NULL;
16603#ifdef FEAT_CLIENTSERVER
16604 remote_common(argvars, rettv, FALSE);
16605#endif
16606}
16607
16608/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016609 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016610 */
16611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016612f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016613{
Bram Moolenaar33570922005-01-25 22:26:29 +000016614 list_T *l;
16615 listitem_T *item, *item2;
16616 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016617 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016618 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016619 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016620 dict_T *d;
16621 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016622 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016623
Bram Moolenaar8c711452005-01-14 21:53:12 +000016624 if (argvars[0].v_type == VAR_DICT)
16625 {
16626 if (argvars[2].v_type != VAR_UNKNOWN)
16627 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016628 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016629 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016631 key = get_tv_string_chk(&argvars[1]);
16632 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016634 di = dict_find(d, key, -1);
16635 if (di == NULL)
16636 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016637 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16638 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016639 {
16640 *rettv = di->di_tv;
16641 init_tv(&di->di_tv);
16642 dictitem_remove(d, di);
16643 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016645 }
16646 }
16647 else if (argvars[0].v_type != VAR_LIST)
16648 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016649 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016650 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016652 int error = FALSE;
16653
16654 idx = get_tv_number_chk(&argvars[1], &error);
16655 if (error)
16656 ; /* type error: do nothing, errmsg already given */
16657 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016658 EMSGN(_(e_listidx), idx);
16659 else
16660 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016661 if (argvars[2].v_type == VAR_UNKNOWN)
16662 {
16663 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016664 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016665 *rettv = item->li_tv;
16666 vim_free(item);
16667 }
16668 else
16669 {
16670 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016671 end = get_tv_number_chk(&argvars[2], &error);
16672 if (error)
16673 ; /* type error: do nothing */
16674 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016675 EMSGN(_(e_listidx), end);
16676 else
16677 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016678 int cnt = 0;
16679
16680 for (li = item; li != NULL; li = li->li_next)
16681 {
16682 ++cnt;
16683 if (li == item2)
16684 break;
16685 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016686 if (li == NULL) /* didn't find "item2" after "item" */
16687 EMSG(_(e_invrange));
16688 else
16689 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016690 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016691 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016692 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016693 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016694 l->lv_first = item;
16695 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016696 item->li_prev = NULL;
16697 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016698 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016699 }
16700 }
16701 }
16702 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016703 }
16704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705}
16706
16707/*
16708 * "rename({from}, {to})" function
16709 */
16710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016711f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712{
16713 char_u buf[NUMBUFLEN];
16714
16715 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16719 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720}
16721
16722/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016723 * "repeat()" function
16724 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016726f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016727{
16728 char_u *p;
16729 int n;
16730 int slen;
16731 int len;
16732 char_u *r;
16733 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016734
16735 n = get_tv_number(&argvars[1]);
16736 if (argvars[0].v_type == VAR_LIST)
16737 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016738 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016739 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016740 if (list_extend(rettv->vval.v_list,
16741 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016742 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016743 }
16744 else
16745 {
16746 p = get_tv_string(&argvars[0]);
16747 rettv->v_type = VAR_STRING;
16748 rettv->vval.v_string = NULL;
16749
16750 slen = (int)STRLEN(p);
16751 len = slen * n;
16752 if (len <= 0)
16753 return;
16754
16755 r = alloc(len + 1);
16756 if (r != NULL)
16757 {
16758 for (i = 0; i < n; i++)
16759 mch_memmove(r + i * slen, p, (size_t)slen);
16760 r[len] = NUL;
16761 }
16762
16763 rettv->vval.v_string = r;
16764 }
16765}
16766
16767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016768 * "resolve()" function
16769 */
16770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772{
16773 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016774#ifdef HAVE_READLINK
16775 char_u *buf = NULL;
16776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016778 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779#ifdef FEAT_SHORTCUT
16780 {
16781 char_u *v = NULL;
16782
16783 v = mch_resolve_shortcut(p);
16784 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 }
16789#else
16790# ifdef HAVE_READLINK
16791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 char_u *cpy;
16793 int len;
16794 char_u *remain = NULL;
16795 char_u *q;
16796 int is_relative_to_current = FALSE;
16797 int has_trailing_pathsep = FALSE;
16798 int limit = 100;
16799
16800 p = vim_strsave(p);
16801
16802 if (p[0] == '.' && (vim_ispathsep(p[1])
16803 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16804 is_relative_to_current = TRUE;
16805
16806 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016807 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016810 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
16813 q = getnextcomp(p);
16814 if (*q != NUL)
16815 {
16816 /* Separate the first path component in "p", and keep the
16817 * remainder (beginning with the path separator). */
16818 remain = vim_strsave(q - 1);
16819 q[-1] = NUL;
16820 }
16821
Bram Moolenaard9462e32011-04-11 21:35:11 +020016822 buf = alloc(MAXPATHL + 1);
16823 if (buf == NULL)
16824 goto fail;
16825
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 for (;;)
16827 {
16828 for (;;)
16829 {
16830 len = readlink((char *)p, (char *)buf, MAXPATHL);
16831 if (len <= 0)
16832 break;
16833 buf[len] = NUL;
16834
16835 if (limit-- == 0)
16836 {
16837 vim_free(p);
16838 vim_free(remain);
16839 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 goto fail;
16842 }
16843
16844 /* Ensure that the result will have a trailing path separator
16845 * if the argument has one. */
16846 if (remain == NULL && has_trailing_pathsep)
16847 add_pathsep(buf);
16848
16849 /* Separate the first path component in the link value and
16850 * concatenate the remainders. */
16851 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16852 if (*q != NUL)
16853 {
16854 if (remain == NULL)
16855 remain = vim_strsave(q - 1);
16856 else
16857 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016858 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 if (cpy != NULL)
16860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861 vim_free(remain);
16862 remain = cpy;
16863 }
16864 }
16865 q[-1] = NUL;
16866 }
16867
16868 q = gettail(p);
16869 if (q > p && *q == NUL)
16870 {
16871 /* Ignore trailing path separator. */
16872 q[-1] = NUL;
16873 q = gettail(p);
16874 }
16875 if (q > p && !mch_isFullName(buf))
16876 {
16877 /* symlink is relative to directory of argument */
16878 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16879 if (cpy != NULL)
16880 {
16881 STRCPY(cpy, p);
16882 STRCPY(gettail(cpy), buf);
16883 vim_free(p);
16884 p = cpy;
16885 }
16886 }
16887 else
16888 {
16889 vim_free(p);
16890 p = vim_strsave(buf);
16891 }
16892 }
16893
16894 if (remain == NULL)
16895 break;
16896
16897 /* Append the first path component of "remain" to "p". */
16898 q = getnextcomp(remain + 1);
16899 len = q - remain - (*q != NUL);
16900 cpy = vim_strnsave(p, STRLEN(p) + len);
16901 if (cpy != NULL)
16902 {
16903 STRNCAT(cpy, remain, len);
16904 vim_free(p);
16905 p = cpy;
16906 }
16907 /* Shorten "remain". */
16908 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016909 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910 else
16911 {
16912 vim_free(remain);
16913 remain = NULL;
16914 }
16915 }
16916
16917 /* If the result is a relative path name, make it explicitly relative to
16918 * the current directory if and only if the argument had this form. */
16919 if (!vim_ispathsep(*p))
16920 {
16921 if (is_relative_to_current
16922 && *p != NUL
16923 && !(p[0] == '.'
16924 && (p[1] == NUL
16925 || vim_ispathsep(p[1])
16926 || (p[1] == '.'
16927 && (p[2] == NUL
16928 || vim_ispathsep(p[2]))))))
16929 {
16930 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016931 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 if (cpy != NULL)
16933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 vim_free(p);
16935 p = cpy;
16936 }
16937 }
16938 else if (!is_relative_to_current)
16939 {
16940 /* Strip leading "./". */
16941 q = p;
16942 while (q[0] == '.' && vim_ispathsep(q[1]))
16943 q += 2;
16944 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016945 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016946 }
16947 }
16948
16949 /* Ensure that the result will have no trailing path separator
16950 * if the argument had none. But keep "/" or "//". */
16951 if (!has_trailing_pathsep)
16952 {
16953 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016954 if (after_pathsep(p, q))
16955 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 }
16957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016958 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 }
16960# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016961 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016962# endif
16963#endif
16964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016965 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966
16967#ifdef HAVE_READLINK
16968fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016969 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972}
16973
16974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 */
16977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016978f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979{
Bram Moolenaar33570922005-01-25 22:26:29 +000016980 list_T *l;
16981 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 if (argvars[0].v_type != VAR_LIST)
16984 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016985 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016986 && !tv_check_lock(l->lv_lock,
16987 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 {
16989 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016990 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016991 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 while (li != NULL)
16993 {
16994 ni = li->li_prev;
16995 list_append(l, li);
16996 li = ni;
16997 }
16998 rettv->vval.v_list = l;
16999 rettv->v_type = VAR_LIST;
17000 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017001 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003}
17004
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017005#define SP_NOMOVE 0x01 /* don't move cursor */
17006#define SP_REPEAT 0x02 /* repeat to find outer pair */
17007#define SP_RETCOUNT 0x04 /* return matchcount */
17008#define SP_SETPCMARK 0x08 /* set previous context mark */
17009#define SP_START 0x10 /* accept match at start position */
17010#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17011#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017012#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017013
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017014static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015
17016/*
17017 * Get flags for a search function.
17018 * Possibly sets "p_ws".
17019 * Returns BACKWARD, FORWARD or zero (for an error).
17020 */
17021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017022get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023{
17024 int dir = FORWARD;
17025 char_u *flags;
17026 char_u nbuf[NUMBUFLEN];
17027 int mask;
17028
17029 if (varp->v_type != VAR_UNKNOWN)
17030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017031 flags = get_tv_string_buf_chk(varp, nbuf);
17032 if (flags == NULL)
17033 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 while (*flags != NUL)
17035 {
17036 switch (*flags)
17037 {
17038 case 'b': dir = BACKWARD; break;
17039 case 'w': p_ws = TRUE; break;
17040 case 'W': p_ws = FALSE; break;
17041 default: mask = 0;
17042 if (flagsp != NULL)
17043 switch (*flags)
17044 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017045 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017046 case 'e': mask = SP_END; break;
17047 case 'm': mask = SP_RETCOUNT; break;
17048 case 'n': mask = SP_NOMOVE; break;
17049 case 'p': mask = SP_SUBPAT; break;
17050 case 'r': mask = SP_REPEAT; break;
17051 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017052 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053 }
17054 if (mask == 0)
17055 {
17056 EMSG2(_(e_invarg2), flags);
17057 dir = 0;
17058 }
17059 else
17060 *flagsp |= mask;
17061 }
17062 if (dir == 0)
17063 break;
17064 ++flags;
17065 }
17066 }
17067 return dir;
17068}
17069
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017071 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017074search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017076 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 char_u *pat;
17078 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017079 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 int save_p_ws = p_ws;
17081 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017082 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017083 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017084 proftime_T tm;
17085#ifdef FEAT_RELTIME
17086 long time_limit = 0;
17087#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017088 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017089 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017091 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017092 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017093 if (dir == 0)
17094 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017095 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017096 if (flags & SP_START)
17097 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017098 if (flags & SP_END)
17099 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017100 if (flags & SP_COLUMN)
17101 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017102
Bram Moolenaar76929292008-01-06 19:07:36 +000017103 /* Optional arguments: line number to stop searching and timeout. */
17104 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017105 {
17106 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17107 if (lnum_stop < 0)
17108 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017109#ifdef FEAT_RELTIME
17110 if (argvars[3].v_type != VAR_UNKNOWN)
17111 {
17112 time_limit = get_tv_number_chk(&argvars[3], NULL);
17113 if (time_limit < 0)
17114 goto theend;
17115 }
17116#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017117 }
17118
Bram Moolenaar76929292008-01-06 19:07:36 +000017119#ifdef FEAT_RELTIME
17120 /* Set the time limit, if there is one. */
17121 profile_setlimit(time_limit, &tm);
17122#endif
17123
Bram Moolenaar231334e2005-07-25 20:46:57 +000017124 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017125 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017126 * Check to make sure only those flags are set.
17127 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17128 * flags cannot be set. Check for that condition also.
17129 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017130 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017131 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017134 goto theend;
17135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017137 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017138 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017139 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017140 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017142 if (flags & SP_SUBPAT)
17143 retval = subpatnum;
17144 else
17145 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017146 if (flags & SP_SETPCMARK)
17147 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017149 if (match_pos != NULL)
17150 {
17151 /* Store the match cursor position */
17152 match_pos->lnum = pos.lnum;
17153 match_pos->col = pos.col + 1;
17154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 /* "/$" will put the cursor after the end of the line, may need to
17156 * correct that here */
17157 check_cursor();
17158 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017159
17160 /* If 'n' flag is used: restore cursor position. */
17161 if (flags & SP_NOMOVE)
17162 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017163 else
17164 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017165theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017167
17168 return retval;
17169}
17170
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017171#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017172
17173/*
17174 * round() is not in C90, use ceil() or floor() instead.
17175 */
17176 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017177vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017178{
17179 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17180}
17181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017182/*
17183 * "round({float})" function
17184 */
17185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017187{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017188 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017189
17190 rettv->v_type = VAR_FLOAT;
17191 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017192 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017193 else
17194 rettv->vval.v_float = 0.0;
17195}
17196#endif
17197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017198/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017199 * "screenattr()" function
17200 */
17201 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017202f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017203{
17204 int row;
17205 int col;
17206 int c;
17207
17208 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17209 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17210 if (row < 0 || row >= screen_Rows
17211 || col < 0 || col >= screen_Columns)
17212 c = -1;
17213 else
17214 c = ScreenAttrs[LineOffset[row] + col];
17215 rettv->vval.v_number = c;
17216}
17217
17218/*
17219 * "screenchar()" function
17220 */
17221 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017222f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017223{
17224 int row;
17225 int col;
17226 int off;
17227 int c;
17228
17229 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17230 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17231 if (row < 0 || row >= screen_Rows
17232 || col < 0 || col >= screen_Columns)
17233 c = -1;
17234 else
17235 {
17236 off = LineOffset[row] + col;
17237#ifdef FEAT_MBYTE
17238 if (enc_utf8 && ScreenLinesUC[off] != 0)
17239 c = ScreenLinesUC[off];
17240 else
17241#endif
17242 c = ScreenLines[off];
17243 }
17244 rettv->vval.v_number = c;
17245}
17246
17247/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017248 * "screencol()" function
17249 *
17250 * First column is 1 to be consistent with virtcol().
17251 */
17252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017253f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017254{
17255 rettv->vval.v_number = screen_screencol() + 1;
17256}
17257
17258/*
17259 * "screenrow()" function
17260 */
17261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017262f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017263{
17264 rettv->vval.v_number = screen_screenrow() + 1;
17265}
17266
17267/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017268 * "search()" function
17269 */
17270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017271f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017273 int flags = 0;
17274
17275 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276}
17277
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017279 * "searchdecl()" function
17280 */
17281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017282f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017283{
17284 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017285 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017286 int error = FALSE;
17287 char_u *name;
17288
17289 rettv->vval.v_number = 1; /* default: FAIL */
17290
17291 name = get_tv_string_chk(&argvars[0]);
17292 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017293 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017294 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017295 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17296 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17297 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017298 if (!error && name != NULL)
17299 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017300 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017301}
17302
17303/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017304 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017307searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308{
17309 char_u *spat, *mpat, *epat;
17310 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 int dir;
17313 int flags = 0;
17314 char_u nbuf1[NUMBUFLEN];
17315 char_u nbuf2[NUMBUFLEN];
17316 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017317 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017318 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017319 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017322 spat = get_tv_string_chk(&argvars[0]);
17323 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17324 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17325 if (spat == NULL || mpat == NULL || epat == NULL)
17326 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 /* Handle the optional fourth argument: flags */
17329 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017330 if (dir == 0)
17331 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017332
17333 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017334 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17335 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017336 if ((flags & (SP_END | SP_SUBPAT)) != 0
17337 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017338 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017339 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017340 goto theend;
17341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017343 /* Using 'r' implies 'W', otherwise it doesn't work. */
17344 if (flags & SP_REPEAT)
17345 p_ws = FALSE;
17346
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017347 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017348 if (argvars[3].v_type == VAR_UNKNOWN
17349 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 skip = (char_u *)"";
17351 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017354 if (argvars[5].v_type != VAR_UNKNOWN)
17355 {
17356 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17357 if (lnum_stop < 0)
17358 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017359#ifdef FEAT_RELTIME
17360 if (argvars[6].v_type != VAR_UNKNOWN)
17361 {
17362 time_limit = get_tv_number_chk(&argvars[6], NULL);
17363 if (time_limit < 0)
17364 goto theend;
17365 }
17366#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017367 }
17368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 if (skip == NULL)
17370 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017372 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017373 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017374
17375theend:
17376 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017377
17378 return retval;
17379}
17380
17381/*
17382 * "searchpair()" function
17383 */
17384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017385f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017386{
17387 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17388}
17389
17390/*
17391 * "searchpairpos()" function
17392 */
17393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017394f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017395{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017396 pos_T match_pos;
17397 int lnum = 0;
17398 int col = 0;
17399
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017400 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017401 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017402
17403 if (searchpair_cmn(argvars, &match_pos) > 0)
17404 {
17405 lnum = match_pos.lnum;
17406 col = match_pos.col;
17407 }
17408
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17410 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017411}
17412
17413/*
17414 * Search for a start/middle/end thing.
17415 * Used by searchpair(), see its documentation for the details.
17416 * Returns 0 or -1 for no match,
17417 */
17418 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017419do_searchpair(
17420 char_u *spat, /* start pattern */
17421 char_u *mpat, /* middle pattern */
17422 char_u *epat, /* end pattern */
17423 int dir, /* BACKWARD or FORWARD */
17424 char_u *skip, /* skip expression */
17425 int flags, /* SP_SETPCMARK and other SP_ values */
17426 pos_T *match_pos,
17427 linenr_T lnum_stop, /* stop at this line if not zero */
17428 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017429{
17430 char_u *save_cpo;
17431 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17432 long retval = 0;
17433 pos_T pos;
17434 pos_T firstpos;
17435 pos_T foundpos;
17436 pos_T save_cursor;
17437 pos_T save_pos;
17438 int n;
17439 int r;
17440 int nest = 1;
17441 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017442 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017443 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017444
17445 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17446 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017447 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017448
Bram Moolenaar76929292008-01-06 19:07:36 +000017449#ifdef FEAT_RELTIME
17450 /* Set the time limit, if there is one. */
17451 profile_setlimit(time_limit, &tm);
17452#endif
17453
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017454 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17455 * start/middle/end (pat3, for the top pair). */
17456 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17457 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17458 if (pat2 == NULL || pat3 == NULL)
17459 goto theend;
17460 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17461 if (*mpat == NUL)
17462 STRCPY(pat3, pat2);
17463 else
17464 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17465 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017466 if (flags & SP_START)
17467 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017468
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 save_cursor = curwin->w_cursor;
17470 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017471 clearpos(&firstpos);
17472 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 pat = pat3;
17474 for (;;)
17475 {
17476 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017477 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17479 /* didn't find it or found the first match again: FAIL */
17480 break;
17481
17482 if (firstpos.lnum == 0)
17483 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017484 if (equalpos(pos, foundpos))
17485 {
17486 /* Found the same position again. Can happen with a pattern that
17487 * has "\zs" at the end and searching backwards. Advance one
17488 * character and try again. */
17489 if (dir == BACKWARD)
17490 decl(&pos);
17491 else
17492 incl(&pos);
17493 }
17494 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017496 /* clear the start flag to avoid getting stuck here */
17497 options &= ~SEARCH_START;
17498
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499 /* If the skip pattern matches, ignore this match. */
17500 if (*skip != NUL)
17501 {
17502 save_pos = curwin->w_cursor;
17503 curwin->w_cursor = pos;
17504 r = eval_to_bool(skip, &err, NULL, FALSE);
17505 curwin->w_cursor = save_pos;
17506 if (err)
17507 {
17508 /* Evaluating {skip} caused an error, break here. */
17509 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017510 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 break;
17512 }
17513 if (r)
17514 continue;
17515 }
17516
17517 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17518 {
17519 /* Found end when searching backwards or start when searching
17520 * forward: nested pair. */
17521 ++nest;
17522 pat = pat2; /* nested, don't search for middle */
17523 }
17524 else
17525 {
17526 /* Found end when searching forward or start when searching
17527 * backward: end of (nested) pair; or found middle in outer pair. */
17528 if (--nest == 1)
17529 pat = pat3; /* outer level, search for middle */
17530 }
17531
17532 if (nest == 0)
17533 {
17534 /* Found the match: return matchcount or line number. */
17535 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017536 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017538 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017539 if (flags & SP_SETPCMARK)
17540 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 curwin->w_cursor = pos;
17542 if (!(flags & SP_REPEAT))
17543 break;
17544 nest = 1; /* search for next unmatched */
17545 }
17546 }
17547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017548 if (match_pos != NULL)
17549 {
17550 /* Store the match cursor position */
17551 match_pos->lnum = curwin->w_cursor.lnum;
17552 match_pos->col = curwin->w_cursor.col + 1;
17553 }
17554
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017556 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 curwin->w_cursor = save_cursor;
17558
17559theend:
17560 vim_free(pat2);
17561 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017562 if (p_cpo == empty_option)
17563 p_cpo = save_cpo;
17564 else
17565 /* Darn, evaluating the {skip} expression changed the value. */
17566 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017567
17568 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569}
17570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017571/*
17572 * "searchpos()" function
17573 */
17574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017575f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017576{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017577 pos_T match_pos;
17578 int lnum = 0;
17579 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017580 int n;
17581 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017582
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017583 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017584 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017585
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017586 n = search_cmn(argvars, &match_pos, &flags);
17587 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017588 {
17589 lnum = match_pos.lnum;
17590 col = match_pos.col;
17591 }
17592
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017593 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17594 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017595 if (flags & SP_SUBPAT)
17596 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017597}
17598
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017602#ifdef FEAT_CLIENTSERVER
17603 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 char_u *server = get_tv_string_chk(&argvars[0]);
17605 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017606
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 if (server == NULL || reply == NULL)
17609 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610 if (check_restricted() || check_secure())
17611 return;
17612# ifdef FEAT_X11
17613 if (check_connection() == FAIL)
17614 return;
17615# endif
17616
17617 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017619 EMSG(_("E258: Unable to send to client"));
17620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622 rettv->vval.v_number = 0;
17623#else
17624 rettv->vval.v_number = -1;
17625#endif
17626}
17627
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017629f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630{
17631 char_u *r = NULL;
17632
17633#ifdef FEAT_CLIENTSERVER
17634# ifdef WIN32
17635 r = serverGetVimNames();
17636# else
17637 make_connection();
17638 if (X_DISPLAY != NULL)
17639 r = serverGetVimNames(X_DISPLAY);
17640# endif
17641#endif
17642 rettv->v_type = VAR_STRING;
17643 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644}
17645
17646/*
17647 * "setbufvar()" function
17648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017650f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651{
17652 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 char_u nbuf[NUMBUFLEN];
17657
17658 if (check_restricted() || check_secure())
17659 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017660 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17661 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017662 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 varp = &argvars[2];
17664
17665 if (buf != NULL && varname != NULL && varp != NULL)
17666 {
17667 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669
17670 if (*varname == '&')
17671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672 long numval;
17673 char_u *strval;
17674 int error = FALSE;
17675
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017677 numval = get_tv_number_chk(varp, &error);
17678 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017679 if (!error && strval != NULL)
17680 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 }
17682 else
17683 {
17684 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17685 if (bufvarname != NULL)
17686 {
17687 STRCPY(bufvarname, "b:");
17688 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017689 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 vim_free(bufvarname);
17691 }
17692 }
17693
17694 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697}
17698
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017700f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017701{
17702 dict_T *d;
17703 dictitem_T *di;
17704 char_u *csearch;
17705
17706 if (argvars[0].v_type != VAR_DICT)
17707 {
17708 EMSG(_(e_dictreq));
17709 return;
17710 }
17711
17712 if ((d = argvars[0].vval.v_dict) != NULL)
17713 {
17714 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17715 if (csearch != NULL)
17716 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017717#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017718 if (enc_utf8)
17719 {
17720 int pcc[MAX_MCO];
17721 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017722
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017723 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17724 }
17725 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017726#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017727 set_last_csearch(PTR2CHAR(csearch),
17728 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017729 }
17730
17731 di = dict_find(d, (char_u *)"forward", -1);
17732 if (di != NULL)
17733 set_csearch_direction(get_tv_number(&di->di_tv)
17734 ? FORWARD : BACKWARD);
17735
17736 di = dict_find(d, (char_u *)"until", -1);
17737 if (di != NULL)
17738 set_csearch_until(!!get_tv_number(&di->di_tv));
17739 }
17740}
17741
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742/*
17743 * "setcmdpos()" function
17744 */
17745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017746f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017748 int pos = (int)get_tv_number(&argvars[0]) - 1;
17749
17750 if (pos >= 0)
17751 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752}
17753
17754/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017755 * "setfperm({fname}, {mode})" function
17756 */
17757 static void
17758f_setfperm(typval_T *argvars, typval_T *rettv)
17759{
17760 char_u *fname;
17761 char_u modebuf[NUMBUFLEN];
17762 char_u *mode_str;
17763 int i;
17764 int mask;
17765 int mode = 0;
17766
17767 rettv->vval.v_number = 0;
17768 fname = get_tv_string_chk(&argvars[0]);
17769 if (fname == NULL)
17770 return;
17771 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17772 if (mode_str == NULL)
17773 return;
17774 if (STRLEN(mode_str) != 9)
17775 {
17776 EMSG2(_(e_invarg2), mode_str);
17777 return;
17778 }
17779
17780 mask = 1;
17781 for (i = 8; i >= 0; --i)
17782 {
17783 if (mode_str[i] != '-')
17784 mode |= mask;
17785 mask = mask << 1;
17786 }
17787 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17788}
17789
17790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 * "setline()" function
17792 */
17793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017794f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795{
17796 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017797 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017798 list_T *l = NULL;
17799 listitem_T *li = NULL;
17800 long added = 0;
17801 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017803 lnum = get_tv_lnum(&argvars[0]);
17804 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017806 l = argvars[1].vval.v_list;
17807 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017809 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017810 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017811
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017812 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017813 for (;;)
17814 {
17815 if (l != NULL)
17816 {
17817 /* list argument, get next string */
17818 if (li == NULL)
17819 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017820 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017821 li = li->li_next;
17822 }
17823
17824 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017825 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017826 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017827
17828 /* When coming here from Insert mode, sync undo, so that this can be
17829 * undone separately from what was previously inserted. */
17830 if (u_sync_once == 2)
17831 {
17832 u_sync_once = 1; /* notify that u_sync() was called */
17833 u_sync(TRUE);
17834 }
17835
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017836 if (lnum <= curbuf->b_ml.ml_line_count)
17837 {
17838 /* existing line, replace it */
17839 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17840 {
17841 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017842 if (lnum == curwin->w_cursor.lnum)
17843 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017844 rettv->vval.v_number = 0; /* OK */
17845 }
17846 }
17847 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17848 {
17849 /* lnum is one past the last line, append the line */
17850 ++added;
17851 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17852 rettv->vval.v_number = 0; /* OK */
17853 }
17854
17855 if (l == NULL) /* only one string argument */
17856 break;
17857 ++lnum;
17858 }
17859
17860 if (added > 0)
17861 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862}
17863
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017864static 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 +000017865
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017867 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017868 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017870set_qf_ll_list(
17871 win_T *wp UNUSED,
17872 typval_T *list_arg UNUSED,
17873 typval_T *action_arg UNUSED,
17874 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017875{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017876#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017877 char_u *act;
17878 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017879#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017880
Bram Moolenaar2641f772005-03-25 21:58:17 +000017881 rettv->vval.v_number = -1;
17882
17883#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017884 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017885 EMSG(_(e_listreq));
17886 else
17887 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017888 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017889
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017890 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017891 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017892 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017893 if (act == NULL)
17894 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017895 if (*act == 'a' || *act == 'r')
17896 action = *act;
17897 }
17898
Bram Moolenaar81484f42012-12-05 15:16:47 +010017899 if (l != NULL && set_errorlist(wp, l, action,
17900 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017901 rettv->vval.v_number = 0;
17902 }
17903#endif
17904}
17905
17906/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017907 * "setloclist()" function
17908 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017910f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017911{
17912 win_T *win;
17913
17914 rettv->vval.v_number = -1;
17915
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017916 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017917 if (win != NULL)
17918 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17919}
17920
17921/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017922 * "setmatches()" function
17923 */
17924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017925f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017926{
17927#ifdef FEAT_SEARCH_EXTRA
17928 list_T *l;
17929 listitem_T *li;
17930 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017931 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017932
17933 rettv->vval.v_number = -1;
17934 if (argvars[0].v_type != VAR_LIST)
17935 {
17936 EMSG(_(e_listreq));
17937 return;
17938 }
17939 if ((l = argvars[0].vval.v_list) != NULL)
17940 {
17941
17942 /* To some extent make sure that we are dealing with a list from
17943 * "getmatches()". */
17944 li = l->lv_first;
17945 while (li != NULL)
17946 {
17947 if (li->li_tv.v_type != VAR_DICT
17948 || (d = li->li_tv.vval.v_dict) == NULL)
17949 {
17950 EMSG(_(e_invarg));
17951 return;
17952 }
17953 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017954 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17955 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017956 && dict_find(d, (char_u *)"priority", -1) != NULL
17957 && dict_find(d, (char_u *)"id", -1) != NULL))
17958 {
17959 EMSG(_(e_invarg));
17960 return;
17961 }
17962 li = li->li_next;
17963 }
17964
17965 clear_matches(curwin);
17966 li = l->lv_first;
17967 while (li != NULL)
17968 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017969 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017970 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017971 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017972 char_u *group;
17973 int priority;
17974 int id;
17975 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017976
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017977 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017978 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17979 {
17980 if (s == NULL)
17981 {
17982 s = list_alloc();
17983 if (s == NULL)
17984 return;
17985 }
17986
17987 /* match from matchaddpos() */
17988 for (i = 1; i < 9; i++)
17989 {
17990 sprintf((char *)buf, (char *)"pos%d", i);
17991 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17992 {
17993 if (di->di_tv.v_type != VAR_LIST)
17994 return;
17995
17996 list_append_tv(s, &di->di_tv);
17997 s->lv_refcount++;
17998 }
17999 else
18000 break;
18001 }
18002 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018003
18004 group = get_dict_string(d, (char_u *)"group", FALSE);
18005 priority = (int)get_dict_number(d, (char_u *)"priority");
18006 id = (int)get_dict_number(d, (char_u *)"id");
18007 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18008 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18009 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018010 if (i == 0)
18011 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018012 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018013 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018014 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018015 }
18016 else
18017 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018018 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018019 list_unref(s);
18020 s = NULL;
18021 }
18022
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018023 li = li->li_next;
18024 }
18025 rettv->vval.v_number = 0;
18026 }
18027#endif
18028}
18029
18030/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018031 * "setpos()" function
18032 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018034f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035{
18036 pos_T pos;
18037 int fnum;
18038 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018039 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018040
Bram Moolenaar08250432008-02-13 11:42:46 +000018041 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018042 name = get_tv_string_chk(argvars);
18043 if (name != NULL)
18044 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018045 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018046 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018047 if (--pos.col < 0)
18048 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018049 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018050 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018051 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018052 if (fnum == curbuf->b_fnum)
18053 {
18054 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018055 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018056 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018057 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018058 curwin->w_set_curswant = FALSE;
18059 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018061 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018062 }
18063 else
18064 EMSG(_(e_invarg));
18065 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018066 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18067 {
18068 /* set mark */
18069 if (setmark_pos(name[1], &pos, fnum) == OK)
18070 rettv->vval.v_number = 0;
18071 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018072 else
18073 EMSG(_(e_invarg));
18074 }
18075 }
18076}
18077
18078/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018079 * "setqflist()" function
18080 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018082f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018083{
18084 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18085}
18086
18087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 * "setreg()" function
18089 */
18090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018091f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092{
18093 int regname;
18094 char_u *strregname;
18095 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 int append;
18098 char_u yank_type;
18099 long block_len;
18100
18101 block_len = -1;
18102 yank_type = MAUTO;
18103 append = FALSE;
18104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018105 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018106 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018108 if (strregname == NULL)
18109 return; /* type error; errmsg already given */
18110 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111 if (regname == 0 || regname == '@')
18112 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018116 stropt = get_tv_string_chk(&argvars[2]);
18117 if (stropt == NULL)
18118 return; /* type error */
18119 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 switch (*stropt)
18121 {
18122 case 'a': case 'A': /* append */
18123 append = TRUE;
18124 break;
18125 case 'v': case 'c': /* character-wise selection */
18126 yank_type = MCHAR;
18127 break;
18128 case 'V': case 'l': /* line-wise selection */
18129 yank_type = MLINE;
18130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 case 'b': case Ctrl_V: /* block-wise selection */
18132 yank_type = MBLOCK;
18133 if (VIM_ISDIGIT(stropt[1]))
18134 {
18135 ++stropt;
18136 block_len = getdigits(&stropt) - 1;
18137 --stropt;
18138 }
18139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 }
18141 }
18142
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018143 if (argvars[1].v_type == VAR_LIST)
18144 {
18145 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018146 char_u **allocval;
18147 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018148 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018149 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018150 int len = argvars[1].vval.v_list->lv_len;
18151 listitem_T *li;
18152
Bram Moolenaar7d647822014-04-05 21:28:56 +020018153 /* First half: use for pointers to result lines; second half: use for
18154 * pointers to allocated copies. */
18155 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018156 if (lstval == NULL)
18157 return;
18158 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018159 allocval = lstval + len + 2;
18160 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018161
18162 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18163 li = li->li_next)
18164 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018165 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018166 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018167 goto free_lstval;
18168 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018169 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018170 /* Need to make a copy, next get_tv_string_buf_chk() will
18171 * overwrite the string. */
18172 strval = vim_strsave(buf);
18173 if (strval == NULL)
18174 goto free_lstval;
18175 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018176 }
18177 *curval++ = strval;
18178 }
18179 *curval++ = NULL;
18180
18181 write_reg_contents_lst(regname, lstval, -1,
18182 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018183free_lstval:
18184 while (curallocval > allocval)
18185 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018186 vim_free(lstval);
18187 }
18188 else
18189 {
18190 strval = get_tv_string_chk(&argvars[1]);
18191 if (strval == NULL)
18192 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018193 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018195 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018196 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197}
18198
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018199/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018200 * "settabvar()" function
18201 */
18202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018203f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018204{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018205#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018206 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018207 tabpage_T *tp;
18208#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018209 char_u *varname, *tabvarname;
18210 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018211
18212 rettv->vval.v_number = 0;
18213
18214 if (check_restricted() || check_secure())
18215 return;
18216
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018217#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018218 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018219#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018220 varname = get_tv_string_chk(&argvars[1]);
18221 varp = &argvars[2];
18222
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018223 if (varname != NULL && varp != NULL
18224#ifdef FEAT_WINDOWS
18225 && tp != NULL
18226#endif
18227 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018228 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018229#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018230 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018231 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018232#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018233
18234 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18235 if (tabvarname != NULL)
18236 {
18237 STRCPY(tabvarname, "t:");
18238 STRCPY(tabvarname + 2, varname);
18239 set_var(tabvarname, varp, TRUE);
18240 vim_free(tabvarname);
18241 }
18242
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018243#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018244 /* Restore current tabpage */
18245 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018246 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018247#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018248 }
18249}
18250
18251/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018252 * "settabwinvar()" function
18253 */
18254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018255f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018256{
18257 setwinvar(argvars, rettv, 1);
18258}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259
18260/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018261 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018266 setwinvar(argvars, rettv, 0);
18267}
18268
18269/*
18270 * "setwinvar()" and "settabwinvar()" functions
18271 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018272
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018274setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018275{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 win_T *win;
18277#ifdef FEAT_WINDOWS
18278 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018279 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018280 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281#endif
18282 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018283 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018285 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
18287 if (check_restricted() || check_secure())
18288 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018289
18290#ifdef FEAT_WINDOWS
18291 if (off == 1)
18292 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18293 else
18294 tp = curtab;
18295#endif
18296 win = find_win_by_nr(&argvars[off], tp);
18297 varname = get_tv_string_chk(&argvars[off + 1]);
18298 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299
18300 if (win != NULL && varname != NULL && varp != NULL)
18301 {
18302#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018303 need_switch_win = !(tp == curtab && win == curwin);
18304 if (!need_switch_win
18305 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018308 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018310 long numval;
18311 char_u *strval;
18312 int error = FALSE;
18313
18314 ++varname;
18315 numval = get_tv_number_chk(varp, &error);
18316 strval = get_tv_string_buf_chk(varp, nbuf);
18317 if (!error && strval != NULL)
18318 set_option_value(varname, numval, strval, OPT_LOCAL);
18319 }
18320 else
18321 {
18322 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18323 if (winvarname != NULL)
18324 {
18325 STRCPY(winvarname, "w:");
18326 STRCPY(winvarname + 2, varname);
18327 set_var(winvarname, varp, TRUE);
18328 vim_free(winvarname);
18329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 }
18331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018333 if (need_switch_win)
18334 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335#endif
18336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337}
18338
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018339#ifdef FEAT_CRYPT
18340/*
18341 * "sha256({string})" function
18342 */
18343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018344f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018345{
18346 char_u *p;
18347
18348 p = get_tv_string(&argvars[0]);
18349 rettv->vval.v_string = vim_strsave(
18350 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18351 rettv->v_type = VAR_STRING;
18352}
18353#endif /* FEAT_CRYPT */
18354
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018356 * "shellescape({string})" function
18357 */
18358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018359f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018360{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018361 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018362 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018363 rettv->v_type = VAR_STRING;
18364}
18365
18366/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018367 * shiftwidth() function
18368 */
18369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018370f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018371{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018372 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018373}
18374
18375/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018376 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 */
18378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018379f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382
Bram Moolenaar0d660222005-01-07 21:51:51 +000018383 p = get_tv_string(&argvars[0]);
18384 rettv->vval.v_string = vim_strsave(p);
18385 simplify_filename(rettv->vval.v_string); /* simplify in place */
18386 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387}
18388
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018389#ifdef FEAT_FLOAT
18390/*
18391 * "sin()" function
18392 */
18393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018394f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018395{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018396 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018397
18398 rettv->v_type = VAR_FLOAT;
18399 if (get_float_arg(argvars, &f) == OK)
18400 rettv->vval.v_float = sin(f);
18401 else
18402 rettv->vval.v_float = 0.0;
18403}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018404
18405/*
18406 * "sinh()" function
18407 */
18408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018409f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018410{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018411 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018412
18413 rettv->v_type = VAR_FLOAT;
18414 if (get_float_arg(argvars, &f) == OK)
18415 rettv->vval.v_float = sinh(f);
18416 else
18417 rettv->vval.v_float = 0.0;
18418}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018419#endif
18420
Bram Moolenaar0d660222005-01-07 21:51:51 +000018421static int
18422#ifdef __BORLANDC__
18423 _RTLENTRYF
18424#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018425 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426static int
18427#ifdef __BORLANDC__
18428 _RTLENTRYF
18429#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018430 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018431
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018432/* struct used in the array that's given to qsort() */
18433typedef struct
18434{
18435 listitem_T *item;
18436 int idx;
18437} sortItem_T;
18438
Bram Moolenaar0b962472016-02-22 22:51:33 +010018439/* struct storing information about current sort */
18440typedef struct
18441{
18442 int item_compare_ic;
18443 int item_compare_numeric;
18444 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018445#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018446 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018447#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018448 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018449 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018450 dict_T *item_compare_selfdict;
18451 int item_compare_func_err;
18452 int item_compare_keep_zero;
18453} sortinfo_T;
18454static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018455static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456#define ITEM_COMPARE_FAIL 999
18457
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018459 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461 static int
18462#ifdef __BORLANDC__
18463_RTLENTRYF
18464#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018465item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018467 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018468 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018470 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018471 int res;
18472 char_u numbuf1[NUMBUFLEN];
18473 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018475 si1 = (sortItem_T *)s1;
18476 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018477 tv1 = &si1->item->li_tv;
18478 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018479
Bram Moolenaar0b962472016-02-22 22:51:33 +010018480 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018481 {
18482 long v1 = get_tv_number(tv1);
18483 long v2 = get_tv_number(tv2);
18484
18485 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18486 }
18487
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018488#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018489 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018490 {
18491 float_T v1 = get_tv_float(tv1);
18492 float_T v2 = get_tv_float(tv2);
18493
18494 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18495 }
18496#endif
18497
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018498 /* tv2string() puts quotes around a string and allocates memory. Don't do
18499 * that for string variables. Use a single quote when comparing with a
18500 * non-string to do what the docs promise. */
18501 if (tv1->v_type == VAR_STRING)
18502 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018503 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018504 p1 = (char_u *)"'";
18505 else
18506 p1 = tv1->vval.v_string;
18507 }
18508 else
18509 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18510 if (tv2->v_type == VAR_STRING)
18511 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018512 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018513 p2 = (char_u *)"'";
18514 else
18515 p2 = tv2->vval.v_string;
18516 }
18517 else
18518 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018519 if (p1 == NULL)
18520 p1 = (char_u *)"";
18521 if (p2 == NULL)
18522 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018523 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018524 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018525 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018526 res = STRICMP(p1, p2);
18527 else
18528 res = STRCMP(p1, p2);
18529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018531 {
18532 double n1, n2;
18533 n1 = strtod((char *)p1, (char **)&p1);
18534 n2 = strtod((char *)p2, (char **)&p2);
18535 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18536 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018537
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018538 /* When the result would be zero, compare the item indexes. Makes the
18539 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018540 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018541 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018542
Bram Moolenaar0d660222005-01-07 21:51:51 +000018543 vim_free(tofree1);
18544 vim_free(tofree2);
18545 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546}
18547
18548 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018549#ifdef __BORLANDC__
18550_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018552item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018553{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018554 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018555 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018556 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018557 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018558 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018559 char_u *func_name;
18560 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018563 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018564 return 0;
18565
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018566 si1 = (sortItem_T *)s1;
18567 si2 = (sortItem_T *)s2;
18568
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018569 if (partial == NULL)
18570 func_name = sortinfo->item_compare_func;
18571 else
18572 func_name = partial->pt_name;
18573
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018574 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018575 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018576 copy_tv(&si1->item->li_tv, &argv[0]);
18577 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018578
18579 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018580 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018581 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018582 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018583 clear_tv(&argv[0]);
18584 clear_tv(&argv[1]);
18585
18586 if (res == FAIL)
18587 res = ITEM_COMPARE_FAIL;
18588 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018589 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18590 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018591 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018592 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018593
18594 /* When the result would be zero, compare the pointers themselves. Makes
18595 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018596 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018597 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018598
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 return res;
18600}
18601
18602/*
18603 * "sort({list})" function
18604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018606do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607{
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 list_T *l;
18609 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018610 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018611 sortinfo_T *old_sortinfo;
18612 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 long len;
18614 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615
Bram Moolenaar0b962472016-02-22 22:51:33 +010018616 /* Pointer to current info struct used in compare function. Save and
18617 * restore the current one for nested calls. */
18618 old_sortinfo = sortinfo;
18619 sortinfo = &info;
18620
Bram Moolenaar0d660222005-01-07 21:51:51 +000018621 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018622 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 else
18624 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018626 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018627 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18628 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018629 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018630 rettv->vval.v_list = l;
18631 rettv->v_type = VAR_LIST;
18632 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 len = list_len(l);
18635 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018636 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
Bram Moolenaar0b962472016-02-22 22:51:33 +010018638 info.item_compare_ic = FALSE;
18639 info.item_compare_numeric = FALSE;
18640 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018641#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018642 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018643#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018644 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018645 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018646 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018647 if (argvars[1].v_type != VAR_UNKNOWN)
18648 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018649 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018651 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018652 else if (argvars[1].v_type == VAR_PARTIAL)
18653 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 else
18655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018656 int error = FALSE;
18657
18658 i = get_tv_number_chk(&argvars[1], &error);
18659 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018660 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018662 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018663 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018664 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018665 else if (i != 0)
18666 {
18667 EMSG(_(e_invarg));
18668 goto theend;
18669 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018670 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018671 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018672 if (*info.item_compare_func == NUL)
18673 {
18674 /* empty string means default sort */
18675 info.item_compare_func = NULL;
18676 }
18677 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018678 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018679 info.item_compare_func = NULL;
18680 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018681 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018682 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018683 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018684 info.item_compare_func = NULL;
18685 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018686 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018687#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018688 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018689 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018690 info.item_compare_func = NULL;
18691 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018692 }
18693#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018694 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018695 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018696 info.item_compare_func = NULL;
18697 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018698 }
18699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018700 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018701
18702 if (argvars[2].v_type != VAR_UNKNOWN)
18703 {
18704 /* optional third argument: {dict} */
18705 if (argvars[2].v_type != VAR_DICT)
18706 {
18707 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018708 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018709 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018710 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018711 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018715 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018716 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018717 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
Bram Moolenaar327aa022014-03-25 18:24:23 +010018719 i = 0;
18720 if (sort)
18721 {
18722 /* sort(): ptrs will be the list to sort */
18723 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018724 {
18725 ptrs[i].item = li;
18726 ptrs[i].idx = i;
18727 ++i;
18728 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018729
Bram Moolenaar0b962472016-02-22 22:51:33 +010018730 info.item_compare_func_err = FALSE;
18731 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018733 if ((info.item_compare_func != NULL
18734 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018735 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018736 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018737 EMSG(_("E702: Sort compare function failed"));
18738 else
18739 {
18740 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018741 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018742 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018743 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018744 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018745
Bram Moolenaar0b962472016-02-22 22:51:33 +010018746 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018747 {
18748 /* Clear the List and append the items in sorted order. */
18749 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18750 l->lv_len = 0;
18751 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018752 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018753 }
18754 }
18755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018758 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018759
18760 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018761 info.item_compare_func_err = FALSE;
18762 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018763 item_compare_func_ptr = info.item_compare_func != NULL
18764 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018765 ? item_compare2 : item_compare;
18766
18767 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18768 li = li->li_next)
18769 {
18770 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18771 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018772 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018773 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018774 {
18775 EMSG(_("E882: Uniq compare function failed"));
18776 break;
18777 }
18778 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018779
Bram Moolenaar0b962472016-02-22 22:51:33 +010018780 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018781 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018782 while (--i >= 0)
18783 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018784 li = ptrs[i].item->li_next;
18785 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018787 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018788 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018789 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018790 list_fix_watch(l, li);
18791 listitem_free(li);
18792 l->lv_len--;
18793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018794 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018795 }
18796
18797 vim_free(ptrs);
18798 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018799theend:
18800 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018801}
18802
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018803/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018804 * "sort({list})" function
18805 */
18806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018807f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018808{
18809 do_sort_uniq(argvars, rettv, TRUE);
18810}
18811
18812/*
18813 * "uniq({list})" function
18814 */
18815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018816f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018817{
18818 do_sort_uniq(argvars, rettv, FALSE);
18819}
18820
18821/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018822 * "soundfold({word})" function
18823 */
18824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018825f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018826{
18827 char_u *s;
18828
18829 rettv->v_type = VAR_STRING;
18830 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018831#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018832 rettv->vval.v_string = eval_soundfold(s);
18833#else
18834 rettv->vval.v_string = vim_strsave(s);
18835#endif
18836}
18837
18838/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018839 * "spellbadword()" function
18840 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018842f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018843{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018844 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018845 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018846 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018848 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018849 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018850
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018851#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018852 if (argvars[0].v_type == VAR_UNKNOWN)
18853 {
18854 /* Find the start and length of the badly spelled word. */
18855 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18856 if (len != 0)
18857 word = ml_get_cursor();
18858 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018859 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018860 {
18861 char_u *str = get_tv_string_chk(&argvars[0]);
18862 int capcol = -1;
18863
18864 if (str != NULL)
18865 {
18866 /* Check the argument for spelling. */
18867 while (*str != NUL)
18868 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018869 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018870 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018871 {
18872 word = str;
18873 break;
18874 }
18875 str += len;
18876 }
18877 }
18878 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018879#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018880
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018881 list_append_string(rettv->vval.v_list, word, len);
18882 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018883 attr == HLF_SPB ? "bad" :
18884 attr == HLF_SPR ? "rare" :
18885 attr == HLF_SPL ? "local" :
18886 attr == HLF_SPC ? "caps" :
18887 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018888}
18889
18890/*
18891 * "spellsuggest()" function
18892 */
18893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018894f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018895{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018896#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018897 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018898 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018899 int maxcount;
18900 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018901 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018902 listitem_T *li;
18903 int need_capital = FALSE;
18904#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018905
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018906 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018907 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018908
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018909#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018910 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911 {
18912 str = get_tv_string(&argvars[0]);
18913 if (argvars[1].v_type != VAR_UNKNOWN)
18914 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018915 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018916 if (maxcount <= 0)
18917 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018918 if (argvars[2].v_type != VAR_UNKNOWN)
18919 {
18920 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18921 if (typeerr)
18922 return;
18923 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018924 }
18925 else
18926 maxcount = 25;
18927
Bram Moolenaar4770d092006-01-12 23:22:24 +000018928 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018929
18930 for (i = 0; i < ga.ga_len; ++i)
18931 {
18932 str = ((char_u **)ga.ga_data)[i];
18933
18934 li = listitem_alloc();
18935 if (li == NULL)
18936 vim_free(str);
18937 else
18938 {
18939 li->li_tv.v_type = VAR_STRING;
18940 li->li_tv.v_lock = 0;
18941 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018942 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018943 }
18944 }
18945 ga_clear(&ga);
18946 }
18947#endif
18948}
18949
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018951f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952{
18953 char_u *str;
18954 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018955 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 regmatch_T regmatch;
18957 char_u patbuf[NUMBUFLEN];
18958 char_u *save_cpo;
18959 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018961 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018962 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963
18964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18965 save_cpo = p_cpo;
18966 p_cpo = (char_u *)"";
18967
18968 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018969 if (argvars[1].v_type != VAR_UNKNOWN)
18970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018971 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18972 if (pat == NULL)
18973 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018975 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018976 }
18977 if (pat == NULL || *pat == NUL)
18978 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018980 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018982 if (typeerr)
18983 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18986 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018989 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018990 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018991 if (*str == NUL)
18992 match = FALSE; /* empty item at the end */
18993 else
18994 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995 if (match)
18996 end = regmatch.startp[0];
18997 else
18998 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018999 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19000 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019001 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019002 if (list_append_string(rettv->vval.v_list, str,
19003 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 }
19006 if (!match)
19007 break;
19008 /* Advance to just after the match. */
19009 if (regmatch.endp[0] > str)
19010 col = 0;
19011 else
19012 {
19013 /* Don't get stuck at the same match. */
19014#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019015 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016#else
19017 col = 1;
19018#endif
19019 }
19020 str = regmatch.endp[0];
19021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022
Bram Moolenaar473de612013-06-08 18:19:48 +020019023 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027}
19028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019029#ifdef FEAT_FLOAT
19030/*
19031 * "sqrt()" function
19032 */
19033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019034f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019035{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019036 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019037
19038 rettv->v_type = VAR_FLOAT;
19039 if (get_float_arg(argvars, &f) == OK)
19040 rettv->vval.v_float = sqrt(f);
19041 else
19042 rettv->vval.v_float = 0.0;
19043}
19044
19045/*
19046 * "str2float()" function
19047 */
19048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019049f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019050{
19051 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19052
19053 if (*p == '+')
19054 p = skipwhite(p + 1);
19055 (void)string2float(p, &rettv->vval.v_float);
19056 rettv->v_type = VAR_FLOAT;
19057}
19058#endif
19059
Bram Moolenaar2c932302006-03-18 21:42:09 +000019060/*
19061 * "str2nr()" function
19062 */
19063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019064f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019065{
19066 int base = 10;
19067 char_u *p;
19068 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019069 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019070
19071 if (argvars[1].v_type != VAR_UNKNOWN)
19072 {
19073 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019074 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019075 {
19076 EMSG(_(e_invarg));
19077 return;
19078 }
19079 }
19080
19081 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019082 if (*p == '+')
19083 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019084 switch (base)
19085 {
19086 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19087 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19088 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19089 default: what = 0;
19090 }
19091 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019092 rettv->vval.v_number = n;
19093}
19094
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095#ifdef HAVE_STRFTIME
19096/*
19097 * "strftime({format}[, {time}])" function
19098 */
19099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019100f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101{
19102 char_u result_buf[256];
19103 struct tm *curtime;
19104 time_t seconds;
19105 char_u *p;
19106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019109 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019110 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 seconds = time(NULL);
19112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 curtime = localtime(&seconds);
19115 /* MSVC returns NULL for an invalid value of seconds. */
19116 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 else
19119 {
19120# ifdef FEAT_MBYTE
19121 vimconv_T conv;
19122 char_u *enc;
19123
19124 conv.vc_type = CONV_NONE;
19125 enc = enc_locale();
19126 convert_setup(&conv, p_enc, enc);
19127 if (conv.vc_type != CONV_NONE)
19128 p = string_convert(&conv, p, NULL);
19129# endif
19130 if (p != NULL)
19131 (void)strftime((char *)result_buf, sizeof(result_buf),
19132 (char *)p, curtime);
19133 else
19134 result_buf[0] = NUL;
19135
19136# ifdef FEAT_MBYTE
19137 if (conv.vc_type != CONV_NONE)
19138 vim_free(p);
19139 convert_setup(&conv, enc, p_enc);
19140 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019141 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 else
19143# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145
19146# ifdef FEAT_MBYTE
19147 /* Release conversion descriptors */
19148 convert_setup(&conv, NULL, NULL);
19149 vim_free(enc);
19150# endif
19151 }
19152}
19153#endif
19154
19155/*
19156 * "stridx()" function
19157 */
19158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019159f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160{
19161 char_u buf[NUMBUFLEN];
19162 char_u *needle;
19163 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019166 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019168 needle = get_tv_string_chk(&argvars[1]);
19169 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019171 if (needle == NULL || haystack == NULL)
19172 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173
Bram Moolenaar33570922005-01-25 22:26:29 +000019174 if (argvars[2].v_type != VAR_UNKNOWN)
19175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019176 int error = FALSE;
19177
19178 start_idx = get_tv_number_chk(&argvars[2], &error);
19179 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019180 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019181 if (start_idx >= 0)
19182 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 }
19184
19185 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19186 if (pos != NULL)
19187 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188}
19189
19190/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019191 * "string()" function
19192 */
19193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019194f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195{
19196 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019197 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019199 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019200 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019201 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019202 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204}
19205
19206/*
19207 * "strlen()" function
19208 */
19209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019210f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019212 rettv->vval.v_number = (varnumber_T)(STRLEN(
19213 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019217 * "strchars()" function
19218 */
19219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019221{
19222 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019223 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019224#ifdef FEAT_MBYTE
19225 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019226 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019227#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019228
19229 if (argvars[1].v_type != VAR_UNKNOWN)
19230 skipcc = get_tv_number_chk(&argvars[1], NULL);
19231 if (skipcc < 0 || skipcc > 1)
19232 EMSG(_(e_invarg));
19233 else
19234 {
19235#ifdef FEAT_MBYTE
19236 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19237 while (*s != NUL)
19238 {
19239 func_mb_ptr2char_adv(&s);
19240 ++len;
19241 }
19242 rettv->vval.v_number = len;
19243#else
19244 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19245#endif
19246 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019247}
19248
19249/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019250 * "strdisplaywidth()" function
19251 */
19252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019253f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019254{
19255 char_u *s = get_tv_string(&argvars[0]);
19256 int col = 0;
19257
19258 if (argvars[1].v_type != VAR_UNKNOWN)
19259 col = get_tv_number(&argvars[1]);
19260
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019261 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019262}
19263
19264/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019265 * "strwidth()" function
19266 */
19267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019268f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019269{
19270 char_u *s = get_tv_string(&argvars[0]);
19271
19272 rettv->vval.v_number = (varnumber_T)(
19273#ifdef FEAT_MBYTE
19274 mb_string2cells(s, -1)
19275#else
19276 STRLEN(s)
19277#endif
19278 );
19279}
19280
19281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 * "strpart()" function
19283 */
19284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019285f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286{
19287 char_u *p;
19288 int n;
19289 int len;
19290 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019293 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 slen = (int)STRLEN(p);
19295
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296 n = get_tv_number_chk(&argvars[1], &error);
19297 if (error)
19298 len = 0;
19299 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019300 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 else
19302 len = slen - n; /* default len: all bytes that are available. */
19303
19304 /*
19305 * Only return the overlap between the specified part and the actual
19306 * string.
19307 */
19308 if (n < 0)
19309 {
19310 len += n;
19311 n = 0;
19312 }
19313 else if (n > slen)
19314 n = slen;
19315 if (len < 0)
19316 len = 0;
19317 else if (n + len > slen)
19318 len = slen - n;
19319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019320 rettv->v_type = VAR_STRING;
19321 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322}
19323
19324/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 * "strridx()" function
19326 */
19327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019328f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019329{
19330 char_u buf[NUMBUFLEN];
19331 char_u *needle;
19332 char_u *haystack;
19333 char_u *rest;
19334 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019335 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019336
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019337 needle = get_tv_string_chk(&argvars[1]);
19338 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019339
19340 rettv->vval.v_number = -1;
19341 if (needle == NULL || haystack == NULL)
19342 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019343
19344 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019345 if (argvars[2].v_type != VAR_UNKNOWN)
19346 {
19347 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019348 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019349 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019350 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019351 }
19352 else
19353 end_idx = haystack_len;
19354
Bram Moolenaar0d660222005-01-07 21:51:51 +000019355 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019356 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019358 lastmatch = haystack + end_idx;
19359 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019360 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019361 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362 for (rest = haystack; *rest != '\0'; ++rest)
19363 {
19364 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019365 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019366 break;
19367 lastmatch = rest;
19368 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019369 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370
19371 if (lastmatch == NULL)
19372 rettv->vval.v_number = -1;
19373 else
19374 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19375}
19376
19377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 * "strtrans()" function
19379 */
19380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019381f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383 rettv->v_type = VAR_STRING;
19384 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385}
19386
19387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388 * "submatch()" function
19389 */
19390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019391f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019392{
Bram Moolenaar41571762014-04-02 19:00:58 +020019393 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019394 int no;
19395 int retList = 0;
19396
19397 no = (int)get_tv_number_chk(&argvars[0], &error);
19398 if (error)
19399 return;
19400 error = FALSE;
19401 if (argvars[1].v_type != VAR_UNKNOWN)
19402 retList = get_tv_number_chk(&argvars[1], &error);
19403 if (error)
19404 return;
19405
19406 if (retList == 0)
19407 {
19408 rettv->v_type = VAR_STRING;
19409 rettv->vval.v_string = reg_submatch(no);
19410 }
19411 else
19412 {
19413 rettv->v_type = VAR_LIST;
19414 rettv->vval.v_list = reg_submatch_list(no);
19415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416}
19417
19418/*
19419 * "substitute()" function
19420 */
19421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019422f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019423{
19424 char_u patbuf[NUMBUFLEN];
19425 char_u subbuf[NUMBUFLEN];
19426 char_u flagsbuf[NUMBUFLEN];
19427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019428 char_u *str = get_tv_string_chk(&argvars[0]);
19429 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19430 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19431 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19432
Bram Moolenaar0d660222005-01-07 21:51:51 +000019433 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019434 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19435 rettv->vval.v_string = NULL;
19436 else
19437 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019438}
19439
19440/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019441 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019444f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445{
19446 int id = 0;
19447#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019448 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 long col;
19450 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019451 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019453 lnum = get_tv_lnum(argvars); /* -1 on type error */
19454 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19455 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019458 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019459 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460#endif
19461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019462 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463}
19464
19465/*
19466 * "synIDattr(id, what [, mode])" function
19467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019469f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470{
19471 char_u *p = NULL;
19472#ifdef FEAT_SYN_HL
19473 int id;
19474 char_u *what;
19475 char_u *mode;
19476 char_u modebuf[NUMBUFLEN];
19477 int modec;
19478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 id = get_tv_number(&argvars[0]);
19480 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019481 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019485 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 modec = 0; /* replace invalid with current */
19487 }
19488 else
19489 {
19490#ifdef FEAT_GUI
19491 if (gui.in_use)
19492 modec = 'g';
19493 else
19494#endif
19495 if (t_colors > 1)
19496 modec = 'c';
19497 else
19498 modec = 't';
19499 }
19500
19501
19502 switch (TOLOWER_ASC(what[0]))
19503 {
19504 case 'b':
19505 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19506 p = highlight_color(id, what, modec);
19507 else /* bold */
19508 p = highlight_has_attr(id, HL_BOLD, modec);
19509 break;
19510
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019511 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 p = highlight_color(id, what, modec);
19513 break;
19514
19515 case 'i':
19516 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19517 p = highlight_has_attr(id, HL_INVERSE, modec);
19518 else /* italic */
19519 p = highlight_has_attr(id, HL_ITALIC, modec);
19520 break;
19521
19522 case 'n': /* name */
19523 p = get_highlight_name(NULL, id - 1);
19524 break;
19525
19526 case 'r': /* reverse */
19527 p = highlight_has_attr(id, HL_INVERSE, modec);
19528 break;
19529
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019530 case 's':
19531 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19532 p = highlight_color(id, what, modec);
19533 else /* standout */
19534 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535 break;
19536
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019537 case 'u':
19538 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19539 /* underline */
19540 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19541 else
19542 /* undercurl */
19543 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 break;
19545 }
19546
19547 if (p != NULL)
19548 p = vim_strsave(p);
19549#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019550 rettv->v_type = VAR_STRING;
19551 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552}
19553
19554/*
19555 * "synIDtrans(id)" function
19556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019558f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559{
19560 int id;
19561
19562#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564
19565 if (id > 0)
19566 id = syn_get_final_id(id);
19567 else
19568#endif
19569 id = 0;
19570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
19574/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019575 * "synconcealed(lnum, col)" function
19576 */
19577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019578f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019579{
19580#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19581 long lnum;
19582 long col;
19583 int syntax_flags = 0;
19584 int cchar;
19585 int matchid = 0;
19586 char_u str[NUMBUFLEN];
19587#endif
19588
19589 rettv->v_type = VAR_LIST;
19590 rettv->vval.v_list = NULL;
19591
19592#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19593 lnum = get_tv_lnum(argvars); /* -1 on type error */
19594 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19595
19596 vim_memset(str, NUL, sizeof(str));
19597
19598 if (rettv_list_alloc(rettv) != FAIL)
19599 {
19600 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19601 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19602 && curwin->w_p_cole > 0)
19603 {
19604 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19605 syntax_flags = get_syntax_info(&matchid);
19606
19607 /* get the conceal character */
19608 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19609 {
19610 cchar = syn_get_sub_char();
19611 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19612 cchar = lcs_conceal;
19613 if (cchar != NUL)
19614 {
19615# ifdef FEAT_MBYTE
19616 if (has_mbyte)
19617 (*mb_char2bytes)(cchar, str);
19618 else
19619# endif
19620 str[0] = cchar;
19621 }
19622 }
19623 }
19624
19625 list_append_number(rettv->vval.v_list,
19626 (syntax_flags & HL_CONCEAL) != 0);
19627 /* -1 to auto-determine strlen */
19628 list_append_string(rettv->vval.v_list, str, -1);
19629 list_append_number(rettv->vval.v_list, matchid);
19630 }
19631#endif
19632}
19633
19634/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019635 * "synstack(lnum, col)" function
19636 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019639{
19640#ifdef FEAT_SYN_HL
19641 long lnum;
19642 long col;
19643 int i;
19644 int id;
19645#endif
19646
19647 rettv->v_type = VAR_LIST;
19648 rettv->vval.v_list = NULL;
19649
19650#ifdef FEAT_SYN_HL
19651 lnum = get_tv_lnum(argvars); /* -1 on type error */
19652 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19653
19654 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019655 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019656 && rettv_list_alloc(rettv) != FAIL)
19657 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019658 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019659 for (i = 0; ; ++i)
19660 {
19661 id = syn_get_stack_item(i);
19662 if (id < 0)
19663 break;
19664 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19665 break;
19666 }
19667 }
19668#endif
19669}
19670
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019672get_cmd_output_as_rettv(
19673 typval_T *argvars,
19674 typval_T *rettv,
19675 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019677 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019679 char_u *infile = NULL;
19680 char_u buf[NUMBUFLEN];
19681 int err = FALSE;
19682 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019683 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019684 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019686 rettv->v_type = VAR_STRING;
19687 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019688 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019689 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019692 {
19693 /*
19694 * Write the string to a temp file, to be used for input of the shell
19695 * command.
19696 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019697 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019698 {
19699 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019700 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019701 }
19702
19703 fd = mch_fopen((char *)infile, WRITEBIN);
19704 if (fd == NULL)
19705 {
19706 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019707 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019708 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019709 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019710 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019711 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19712 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019713 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019714 else
19715 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019716 size_t len;
19717
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019718 p = get_tv_string_buf_chk(&argvars[1], buf);
19719 if (p == NULL)
19720 {
19721 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019722 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019723 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019724 len = STRLEN(p);
19725 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019726 err = TRUE;
19727 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019728 if (fclose(fd) != 0)
19729 err = TRUE;
19730 if (err)
19731 {
19732 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019733 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019734 }
19735 }
19736
Bram Moolenaar52a72462014-08-29 15:53:52 +020019737 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19738 * echoes typeahead, that messes up the display. */
19739 if (!msg_silent)
19740 flags += SHELL_COOKED;
19741
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019744 int len;
19745 listitem_T *li;
19746 char_u *s = NULL;
19747 char_u *start;
19748 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019749 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750
Bram Moolenaar52a72462014-08-29 15:53:52 +020019751 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019752 if (res == NULL)
19753 goto errret;
19754
19755 list = list_alloc();
19756 if (list == NULL)
19757 goto errret;
19758
19759 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019761 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019762 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019763 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019764 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019766 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019767 if (s == NULL)
19768 goto errret;
19769
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019770 for (p = s; start < end; ++p, ++start)
19771 *p = *start == NUL ? NL : *start;
19772 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019773
19774 li = listitem_alloc();
19775 if (li == NULL)
19776 {
19777 vim_free(s);
19778 goto errret;
19779 }
19780 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019781 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019782 li->li_tv.vval.v_string = s;
19783 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019785
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019786 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019787 rettv->v_type = VAR_LIST;
19788 rettv->vval.v_list = list;
19789 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 else
19792 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019793 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019794#ifdef USE_CR
19795 /* translate <CR> into <NL> */
19796 if (res != NULL)
19797 {
19798 char_u *s;
19799
19800 for (s = res; *s; ++s)
19801 {
19802 if (*s == CAR)
19803 *s = NL;
19804 }
19805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806#else
19807# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019808 /* translate <CR><NL> into <NL> */
19809 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019811 char_u *s, *d;
19812
19813 d = res;
19814 for (s = res; *s; ++s)
19815 {
19816 if (s[0] == CAR && s[1] == NL)
19817 ++s;
19818 *d++ = *s;
19819 }
19820 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822# endif
19823#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019824 rettv->vval.v_string = res;
19825 res = NULL;
19826 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019827
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019828errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019829 if (infile != NULL)
19830 {
19831 mch_remove(infile);
19832 vim_free(infile);
19833 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019834 if (res != NULL)
19835 vim_free(res);
19836 if (list != NULL)
19837 list_free(list, TRUE);
19838}
19839
19840/*
19841 * "system()" function
19842 */
19843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019844f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019845{
19846 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19847}
19848
19849/*
19850 * "systemlist()" function
19851 */
19852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019853f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019854{
19855 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856}
19857
19858/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019859 * "tabpagebuflist()" function
19860 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019862f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019863{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019864#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019865 tabpage_T *tp;
19866 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019867
19868 if (argvars[0].v_type == VAR_UNKNOWN)
19869 wp = firstwin;
19870 else
19871 {
19872 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19873 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019874 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019875 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019876 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019877 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019878 for (; wp != NULL; wp = wp->w_next)
19879 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019880 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019881 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019882 }
19883#endif
19884}
19885
19886
19887/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019888 * "tabpagenr()" function
19889 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019891f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019892{
19893 int nr = 1;
19894#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019895 char_u *arg;
19896
19897 if (argvars[0].v_type != VAR_UNKNOWN)
19898 {
19899 arg = get_tv_string_chk(&argvars[0]);
19900 nr = 0;
19901 if (arg != NULL)
19902 {
19903 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019904 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019905 else
19906 EMSG2(_(e_invexpr2), arg);
19907 }
19908 }
19909 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019910 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019911#endif
19912 rettv->vval.v_number = nr;
19913}
19914
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019915
19916#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019917static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019918
19919/*
19920 * Common code for tabpagewinnr() and winnr().
19921 */
19922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019923get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019924{
19925 win_T *twin;
19926 int nr = 1;
19927 win_T *wp;
19928 char_u *arg;
19929
19930 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19931 if (argvar->v_type != VAR_UNKNOWN)
19932 {
19933 arg = get_tv_string_chk(argvar);
19934 if (arg == NULL)
19935 nr = 0; /* type error; errmsg already given */
19936 else if (STRCMP(arg, "$") == 0)
19937 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19938 else if (STRCMP(arg, "#") == 0)
19939 {
19940 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19941 if (twin == NULL)
19942 nr = 0;
19943 }
19944 else
19945 {
19946 EMSG2(_(e_invexpr2), arg);
19947 nr = 0;
19948 }
19949 }
19950
19951 if (nr > 0)
19952 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19953 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019954 {
19955 if (wp == NULL)
19956 {
19957 /* didn't find it in this tabpage */
19958 nr = 0;
19959 break;
19960 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019961 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019962 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019963 return nr;
19964}
19965#endif
19966
19967/*
19968 * "tabpagewinnr()" function
19969 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019971f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019972{
19973 int nr = 1;
19974#ifdef FEAT_WINDOWS
19975 tabpage_T *tp;
19976
19977 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19978 if (tp == NULL)
19979 nr = 0;
19980 else
19981 nr = get_winnr(tp, &argvars[1]);
19982#endif
19983 rettv->vval.v_number = nr;
19984}
19985
19986
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019987/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019988 * "tagfiles()" function
19989 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019991f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019992{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019993 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019994 tagname_T tn;
19995 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019996
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019997 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019998 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019999 fname = alloc(MAXPATHL);
20000 if (fname == NULL)
20001 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020002
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020003 for (first = TRUE; ; first = FALSE)
20004 if (get_tagfname(&tn, first, fname) == FAIL
20005 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020006 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020007 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020008 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020009}
20010
20011/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020012 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020013 */
20014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020015f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020016{
20017 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020018
20019 tag_pattern = get_tv_string(&argvars[0]);
20020
20021 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020022 if (*tag_pattern == NUL)
20023 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020025 if (rettv_list_alloc(rettv) == OK)
20026 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020027}
20028
20029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 * "tempname()" function
20031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020033f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034{
20035 static int x = 'A';
20036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020038 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020039
20040 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20041 * names. Skip 'I' and 'O', they are used for shell redirection. */
20042 do
20043 {
20044 if (x == 'Z')
20045 x = '0';
20046 else if (x == '9')
20047 x = 'A';
20048 else
20049 {
20050#ifdef EBCDIC
20051 if (x == 'I')
20052 x = 'J';
20053 else if (x == 'R')
20054 x = 'S';
20055 else
20056#endif
20057 ++x;
20058 }
20059 } while (x == 'I' || x == 'O');
20060}
20061
20062/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020063 * "test(list)" function: Just checking the walls...
20064 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020066f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020067{
20068 /* Used for unit testing. Change the code below to your liking. */
20069#if 0
20070 listitem_T *li;
20071 list_T *l;
20072 char_u *bad, *good;
20073
20074 if (argvars[0].v_type != VAR_LIST)
20075 return;
20076 l = argvars[0].vval.v_list;
20077 if (l == NULL)
20078 return;
20079 li = l->lv_first;
20080 if (li == NULL)
20081 return;
20082 bad = get_tv_string(&li->li_tv);
20083 li = li->li_next;
20084 if (li == NULL)
20085 return;
20086 good = get_tv_string(&li->li_tv);
20087 rettv->vval.v_number = test_edit_score(bad, good);
20088#endif
20089}
20090
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020091#ifdef FEAT_FLOAT
20092/*
20093 * "tan()" function
20094 */
20095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020096f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020097{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020098 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020099
20100 rettv->v_type = VAR_FLOAT;
20101 if (get_float_arg(argvars, &f) == OK)
20102 rettv->vval.v_float = tan(f);
20103 else
20104 rettv->vval.v_float = 0.0;
20105}
20106
20107/*
20108 * "tanh()" function
20109 */
20110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020111f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020112{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020113 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020114
20115 rettv->v_type = VAR_FLOAT;
20116 if (get_float_arg(argvars, &f) == OK)
20117 rettv->vval.v_float = tanh(f);
20118 else
20119 rettv->vval.v_float = 0.0;
20120}
20121#endif
20122
Bram Moolenaar975b5272016-03-15 23:10:59 +010020123#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20124/*
20125 * Get a callback from "arg". It can be a Funcref or a function name.
20126 * When "arg" is zero return an empty string.
20127 * Return NULL for an invalid argument.
20128 */
20129 char_u *
20130get_callback(typval_T *arg, partial_T **pp)
20131{
20132 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20133 {
20134 *pp = arg->vval.v_partial;
20135 return (*pp)->pt_name;
20136 }
20137 *pp = NULL;
20138 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20139 return arg->vval.v_string;
20140 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20141 return (char_u *)"";
20142 EMSG(_("E921: Invalid callback argument"));
20143 return NULL;
20144}
20145#endif
20146
20147#ifdef FEAT_TIMERS
20148/*
20149 * "timer_start(time, callback [, options])" function
20150 */
20151 static void
20152f_timer_start(typval_T *argvars, typval_T *rettv)
20153{
20154 long msec = get_tv_number(&argvars[0]);
20155 timer_T *timer;
20156 int repeat = 0;
20157 char_u *callback;
20158 dict_T *dict;
20159
20160 if (argvars[2].v_type != VAR_UNKNOWN)
20161 {
20162 if (argvars[2].v_type != VAR_DICT
20163 || (dict = argvars[2].vval.v_dict) == NULL)
20164 {
20165 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20166 return;
20167 }
20168 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20169 repeat = get_dict_number(dict, (char_u *)"repeat");
20170 }
20171
20172 timer = create_timer(msec, repeat);
20173 callback = get_callback(&argvars[1], &timer->tr_partial);
20174 if (callback == NULL)
20175 {
20176 stop_timer(timer);
20177 rettv->vval.v_number = -1;
20178 }
20179 else
20180 {
20181 timer->tr_callback = vim_strsave(callback);
20182 rettv->vval.v_number = timer->tr_id;
20183 }
20184}
20185
20186/*
20187 * "timer_stop(timer)" function
20188 */
20189 static void
20190f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20191{
20192 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20193
20194 if (timer != NULL)
20195 stop_timer(timer);
20196}
20197#endif
20198
Bram Moolenaard52d9742005-08-21 22:20:28 +000020199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 * "tolower(string)" function
20201 */
20202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020203f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204{
20205 char_u *p;
20206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020207 p = vim_strsave(get_tv_string(&argvars[0]));
20208 rettv->v_type = VAR_STRING;
20209 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210
20211 if (p != NULL)
20212 while (*p != NUL)
20213 {
20214#ifdef FEAT_MBYTE
20215 int l;
20216
20217 if (enc_utf8)
20218 {
20219 int c, lc;
20220
20221 c = utf_ptr2char(p);
20222 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020223 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 /* TODO: reallocate string when byte count changes. */
20225 if (utf_char2len(lc) == l)
20226 utf_char2bytes(lc, p);
20227 p += l;
20228 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020229 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 p += l; /* skip multi-byte character */
20231 else
20232#endif
20233 {
20234 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20235 ++p;
20236 }
20237 }
20238}
20239
20240/*
20241 * "toupper(string)" function
20242 */
20243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020244f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020246 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020247 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248}
20249
20250/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020251 * "tr(string, fromstr, tostr)" function
20252 */
20253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020254f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020255{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020256 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020257 char_u *fromstr;
20258 char_u *tostr;
20259 char_u *p;
20260#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020261 int inlen;
20262 int fromlen;
20263 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020264 int idx;
20265 char_u *cpstr;
20266 int cplen;
20267 int first = TRUE;
20268#endif
20269 char_u buf[NUMBUFLEN];
20270 char_u buf2[NUMBUFLEN];
20271 garray_T ga;
20272
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020273 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020274 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20275 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020276
20277 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020278 rettv->v_type = VAR_STRING;
20279 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020280 if (fromstr == NULL || tostr == NULL)
20281 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020282 ga_init2(&ga, (int)sizeof(char), 80);
20283
20284#ifdef FEAT_MBYTE
20285 if (!has_mbyte)
20286#endif
20287 /* not multi-byte: fromstr and tostr must be the same length */
20288 if (STRLEN(fromstr) != STRLEN(tostr))
20289 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020290#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020291error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020292#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020293 EMSG2(_(e_invarg2), fromstr);
20294 ga_clear(&ga);
20295 return;
20296 }
20297
20298 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020299 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020300 {
20301#ifdef FEAT_MBYTE
20302 if (has_mbyte)
20303 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020304 inlen = (*mb_ptr2len)(in_str);
20305 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020306 cplen = inlen;
20307 idx = 0;
20308 for (p = fromstr; *p != NUL; p += fromlen)
20309 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020310 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020311 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020312 {
20313 for (p = tostr; *p != NUL; p += tolen)
20314 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020315 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020316 if (idx-- == 0)
20317 {
20318 cplen = tolen;
20319 cpstr = p;
20320 break;
20321 }
20322 }
20323 if (*p == NUL) /* tostr is shorter than fromstr */
20324 goto error;
20325 break;
20326 }
20327 ++idx;
20328 }
20329
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020330 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020331 {
20332 /* Check that fromstr and tostr have the same number of
20333 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020334 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020335 first = FALSE;
20336 for (p = tostr; *p != NUL; p += tolen)
20337 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020338 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020339 --idx;
20340 }
20341 if (idx != 0)
20342 goto error;
20343 }
20344
Bram Moolenaarcde88542015-08-11 19:14:00 +020020345 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020346 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020347 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020348
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020349 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020350 }
20351 else
20352#endif
20353 {
20354 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020355 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020356 if (p != NULL)
20357 ga_append(&ga, tostr[p - fromstr]);
20358 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020359 ga_append(&ga, *in_str);
20360 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020361 }
20362 }
20363
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020364 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020365 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020366 ga_append(&ga, NUL);
20367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020368 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020369}
20370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020371#ifdef FEAT_FLOAT
20372/*
20373 * "trunc({float})" function
20374 */
20375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020376f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020377{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020378 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020379
20380 rettv->v_type = VAR_FLOAT;
20381 if (get_float_arg(argvars, &f) == OK)
20382 /* trunc() is not in C90, use floor() or ceil() instead. */
20383 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20384 else
20385 rettv->vval.v_float = 0.0;
20386}
20387#endif
20388
Bram Moolenaar8299df92004-07-10 09:47:34 +000020389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 * "type(expr)" function
20391 */
20392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020393f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020395 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020396
20397 switch (argvars[0].v_type)
20398 {
20399 case VAR_NUMBER: n = 0; break;
20400 case VAR_STRING: n = 1; break;
20401 case VAR_FUNC: n = 2; break;
20402 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020403 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020404 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020405 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020406 if (argvars[0].vval.v_number == VVAL_FALSE
20407 || argvars[0].vval.v_number == VVAL_TRUE)
20408 n = 6;
20409 else
20410 n = 7;
20411 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020412 case VAR_JOB: n = 8; break;
20413 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020414 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020415 case VAR_UNKNOWN:
20416 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20417 n = -1;
20418 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020419 }
20420 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421}
20422
20423/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020424 * "undofile(name)" function
20425 */
20426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020427f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020428{
20429 rettv->v_type = VAR_STRING;
20430#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020431 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020432 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020433
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020434 if (*fname == NUL)
20435 {
20436 /* If there is no file name there will be no undo file. */
20437 rettv->vval.v_string = NULL;
20438 }
20439 else
20440 {
20441 char_u *ffname = FullName_save(fname, FALSE);
20442
20443 if (ffname != NULL)
20444 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20445 vim_free(ffname);
20446 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020447 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020448#else
20449 rettv->vval.v_string = NULL;
20450#endif
20451}
20452
20453/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020454 * "undotree()" function
20455 */
20456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020457f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020458{
20459 if (rettv_dict_alloc(rettv) == OK)
20460 {
20461 dict_T *dict = rettv->vval.v_dict;
20462 list_T *list;
20463
Bram Moolenaar730cde92010-06-27 05:18:54 +020020464 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020465 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020466 dict_add_nr_str(dict, "save_last",
20467 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020468 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20469 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020470 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020471
20472 list = list_alloc();
20473 if (list != NULL)
20474 {
20475 u_eval_tree(curbuf->b_u_oldhead, list);
20476 dict_add_list(dict, "entries", list);
20477 }
20478 }
20479}
20480
20481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020482 * "values(dict)" function
20483 */
20484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020485f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020486{
20487 dict_list(argvars, rettv, 1);
20488}
20489
20490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 * "virtcol(string)" function
20492 */
20493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020494f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495{
20496 colnr_T vcol = 0;
20497 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020498 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020500 fp = var2fpos(&argvars[0], FALSE, &fnum);
20501 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20502 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 {
20504 getvvcol(curwin, fp, NULL, NULL, &vcol);
20505 ++vcol;
20506 }
20507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509}
20510
20511/*
20512 * "visualmode()" function
20513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020515f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517 char_u str[2];
20518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 str[0] = curbuf->b_visual_mode_eval;
20521 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020522 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523
20524 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020525 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527}
20528
20529/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020530 * "wildmenumode()" function
20531 */
20532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020533f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020534{
20535#ifdef FEAT_WILDMENU
20536 if (wild_menu_showing)
20537 rettv->vval.v_number = 1;
20538#endif
20539}
20540
20541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020542 * "winbufnr(nr)" function
20543 */
20544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546{
20547 win_T *wp;
20548
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020549 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020553 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554}
20555
20556/*
20557 * "wincol()" function
20558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020560f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020561{
20562 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020563 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564}
20565
20566/*
20567 * "winheight(nr)" function
20568 */
20569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020570f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571{
20572 win_T *wp;
20573
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020574 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020578 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579}
20580
20581/*
20582 * "winline()" function
20583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020585f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586{
20587 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020588 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589}
20590
20591/*
20592 * "winnr()" function
20593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020595f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596{
20597 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020598
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020600 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020602 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603}
20604
20605/*
20606 * "winrestcmd()" function
20607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020609f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610{
20611#ifdef FEAT_WINDOWS
20612 win_T *wp;
20613 int winnr = 1;
20614 garray_T ga;
20615 char_u buf[50];
20616
20617 ga_init2(&ga, (int)sizeof(char), 70);
20618 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20619 {
20620 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20621 ga_concat(&ga, buf);
20622# ifdef FEAT_VERTSPLIT
20623 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20624 ga_concat(&ga, buf);
20625# endif
20626 ++winnr;
20627 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020628 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020630 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635}
20636
20637/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020638 * "winrestview()" function
20639 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020641f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020642{
20643 dict_T *dict;
20644
20645 if (argvars[0].v_type != VAR_DICT
20646 || (dict = argvars[0].vval.v_dict) == NULL)
20647 EMSG(_(e_invarg));
20648 else
20649 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020650 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20651 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20652 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20653 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020654#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020655 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20656 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020657#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020658 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20659 {
20660 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20661 curwin->w_set_curswant = FALSE;
20662 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020663
Bram Moolenaar82c25852014-05-28 16:47:16 +020020664 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20665 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020666#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020667 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20668 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020669#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020670 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20671 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20672 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20673 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020674
20675 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020676 win_new_height(curwin, curwin->w_height);
20677# ifdef FEAT_VERTSPLIT
20678 win_new_width(curwin, W_WIDTH(curwin));
20679# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020680 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020681
Bram Moolenaarb851a962014-10-31 15:45:52 +010020682 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020683 curwin->w_topline = 1;
20684 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20685 curwin->w_topline = curbuf->b_ml.ml_line_count;
20686#ifdef FEAT_DIFF
20687 check_topfill(curwin, TRUE);
20688#endif
20689 }
20690}
20691
20692/*
20693 * "winsaveview()" function
20694 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020696f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020697{
20698 dict_T *dict;
20699
Bram Moolenaara800b422010-06-27 01:15:55 +020020700 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020701 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020702 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020703
20704 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20705 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20706#ifdef FEAT_VIRTUALEDIT
20707 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20708#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020709 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020710 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20711
20712 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20713#ifdef FEAT_DIFF
20714 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20715#endif
20716 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20717 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20718}
20719
20720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 * "winwidth(nr)" function
20722 */
20723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020724f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725{
20726 win_T *wp;
20727
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020728 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 else
20732#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020735 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736#endif
20737}
20738
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020740 * "wordcount()" function
20741 */
20742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020743f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020744{
20745 if (rettv_dict_alloc(rettv) == FAIL)
20746 return;
20747 cursor_pos_info(rettv->vval.v_dict);
20748}
20749
20750/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020751 * Write list of strings to file
20752 */
20753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020754write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020755{
20756 listitem_T *li;
20757 int c;
20758 int ret = OK;
20759 char_u *s;
20760
20761 for (li = list->lv_first; li != NULL; li = li->li_next)
20762 {
20763 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20764 {
20765 if (*s == '\n')
20766 c = putc(NUL, fd);
20767 else
20768 c = putc(*s, fd);
20769 if (c == EOF)
20770 {
20771 ret = FAIL;
20772 break;
20773 }
20774 }
20775 if (!binary || li->li_next != NULL)
20776 if (putc('\n', fd) == EOF)
20777 {
20778 ret = FAIL;
20779 break;
20780 }
20781 if (ret == FAIL)
20782 {
20783 EMSG(_(e_write));
20784 break;
20785 }
20786 }
20787 return ret;
20788}
20789
20790/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020791 * "writefile()" function
20792 */
20793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020794f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020795{
20796 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020797 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020798 char_u *fname;
20799 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020800 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020801
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020802 if (check_restricted() || check_secure())
20803 return;
20804
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020805 if (argvars[0].v_type != VAR_LIST)
20806 {
20807 EMSG2(_(e_listarg), "writefile()");
20808 return;
20809 }
20810 if (argvars[0].vval.v_list == NULL)
20811 return;
20812
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020813 if (argvars[2].v_type != VAR_UNKNOWN)
20814 {
20815 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20816 binary = TRUE;
20817 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20818 append = TRUE;
20819 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020820
20821 /* Always open the file in binary mode, library functions have a mind of
20822 * their own about CR-LF conversion. */
20823 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020824 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20825 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020826 {
20827 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20828 ret = -1;
20829 }
20830 else
20831 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020832 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20833 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020834 fclose(fd);
20835 }
20836
20837 rettv->vval.v_number = ret;
20838}
20839
20840/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020841 * "xor(expr, expr)" function
20842 */
20843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020844f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020845{
20846 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20847 ^ get_tv_number_chk(&argvars[1], NULL);
20848}
20849
20850
20851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020853 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854 */
20855 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020856var2fpos(
20857 typval_T *varp,
20858 int dollar_lnum, /* TRUE when $ is last line */
20859 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020861 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020863 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864
Bram Moolenaara5525202006-03-02 22:52:09 +000020865 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020866 if (varp->v_type == VAR_LIST)
20867 {
20868 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020869 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020870 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020871 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020872
20873 l = varp->vval.v_list;
20874 if (l == NULL)
20875 return NULL;
20876
20877 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020878 pos.lnum = list_find_nr(l, 0L, &error);
20879 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020880 return NULL; /* invalid line number */
20881
20882 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020883 pos.col = list_find_nr(l, 1L, &error);
20884 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020885 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020886 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020887
20888 /* We accept "$" for the column number: last column. */
20889 li = list_find(l, 1L);
20890 if (li != NULL && li->li_tv.v_type == VAR_STRING
20891 && li->li_tv.vval.v_string != NULL
20892 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20893 pos.col = len + 1;
20894
Bram Moolenaara5525202006-03-02 22:52:09 +000020895 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020896 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020897 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020898 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020899
Bram Moolenaara5525202006-03-02 22:52:09 +000020900#ifdef FEAT_VIRTUALEDIT
20901 /* Get the virtual offset. Defaults to zero. */
20902 pos.coladd = list_find_nr(l, 2L, &error);
20903 if (error)
20904 pos.coladd = 0;
20905#endif
20906
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020907 return &pos;
20908 }
20909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020910 name = get_tv_string_chk(varp);
20911 if (name == NULL)
20912 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020913 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020915 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20916 {
20917 if (VIsual_active)
20918 return &VIsual;
20919 return &curwin->w_cursor;
20920 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020921 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020923 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20925 return NULL;
20926 return pp;
20927 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020928
20929#ifdef FEAT_VIRTUALEDIT
20930 pos.coladd = 0;
20931#endif
20932
Bram Moolenaar477933c2007-07-17 14:32:23 +000020933 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020934 {
20935 pos.col = 0;
20936 if (name[1] == '0') /* "w0": first visible line */
20937 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020938 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020939 pos.lnum = curwin->w_topline;
20940 return &pos;
20941 }
20942 else if (name[1] == '$') /* "w$": last visible line */
20943 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020944 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020945 pos.lnum = curwin->w_botline - 1;
20946 return &pos;
20947 }
20948 }
20949 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020951 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952 {
20953 pos.lnum = curbuf->b_ml.ml_line_count;
20954 pos.col = 0;
20955 }
20956 else
20957 {
20958 pos.lnum = curwin->w_cursor.lnum;
20959 pos.col = (colnr_T)STRLEN(ml_get_curline());
20960 }
20961 return &pos;
20962 }
20963 return NULL;
20964}
20965
20966/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020967 * Convert list in "arg" into a position and optional file number.
20968 * When "fnump" is NULL there is no file number, only 3 items.
20969 * Note that the column is passed on as-is, the caller may want to decrement
20970 * it to use 1 for the first column.
20971 * Return FAIL when conversion is not possible, doesn't check the position for
20972 * validity.
20973 */
20974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020975list2fpos(
20976 typval_T *arg,
20977 pos_T *posp,
20978 int *fnump,
20979 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020980{
20981 list_T *l = arg->vval.v_list;
20982 long i = 0;
20983 long n;
20984
Bram Moolenaar493c1782014-05-28 14:34:46 +020020985 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20986 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020987 if (arg->v_type != VAR_LIST
20988 || l == NULL
20989 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020990 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020991 return FAIL;
20992
20993 if (fnump != NULL)
20994 {
20995 n = list_find_nr(l, i++, NULL); /* fnum */
20996 if (n < 0)
20997 return FAIL;
20998 if (n == 0)
20999 n = curbuf->b_fnum; /* current buffer */
21000 *fnump = n;
21001 }
21002
21003 n = list_find_nr(l, i++, NULL); /* lnum */
21004 if (n < 0)
21005 return FAIL;
21006 posp->lnum = n;
21007
21008 n = list_find_nr(l, i++, NULL); /* col */
21009 if (n < 0)
21010 return FAIL;
21011 posp->col = n;
21012
21013#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021014 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021015 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021016 posp->coladd = 0;
21017 else
21018 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021019#endif
21020
Bram Moolenaar493c1782014-05-28 14:34:46 +020021021 if (curswantp != NULL)
21022 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21023
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021024 return OK;
21025}
21026
21027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 * Get the length of an environment variable name.
21029 * Advance "arg" to the first character after the name.
21030 * Return 0 for error.
21031 */
21032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021033get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034{
21035 char_u *p;
21036 int len;
21037
21038 for (p = *arg; vim_isIDc(*p); ++p)
21039 ;
21040 if (p == *arg) /* no name found */
21041 return 0;
21042
21043 len = (int)(p - *arg);
21044 *arg = p;
21045 return len;
21046}
21047
21048/*
21049 * Get the length of the name of a function or internal variable.
21050 * "arg" is advanced to the first non-white character after the name.
21051 * Return 0 if something is wrong.
21052 */
21053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021054get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021055{
21056 char_u *p;
21057 int len;
21058
21059 /* Find the end of the name. */
21060 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021061 {
21062 if (*p == ':')
21063 {
21064 /* "s:" is start of "s:var", but "n:" is not and can be used in
21065 * slice "[n:]". Also "xx:" is not a namespace. */
21066 len = (int)(p - *arg);
21067 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21068 || len > 1)
21069 break;
21070 }
21071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 if (p == *arg) /* no name found */
21073 return 0;
21074
21075 len = (int)(p - *arg);
21076 *arg = skipwhite(p);
21077
21078 return len;
21079}
21080
21081/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021082 * Get the length of the name of a variable or function.
21083 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021085 * Return -1 if curly braces expansion failed.
21086 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * If the name contains 'magic' {}'s, expand them and return the
21088 * expanded name in an allocated string via 'alias' - caller must free.
21089 */
21090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091get_name_len(
21092 char_u **arg,
21093 char_u **alias,
21094 int evaluate,
21095 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096{
21097 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 char_u *p;
21099 char_u *expr_start;
21100 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101
21102 *alias = NULL; /* default to no alias */
21103
21104 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21105 && (*arg)[2] == (int)KE_SNR)
21106 {
21107 /* hard coded <SNR>, already translated */
21108 *arg += 3;
21109 return get_id_len(arg) + 3;
21110 }
21111 len = eval_fname_script(*arg);
21112 if (len > 0)
21113 {
21114 /* literal "<SID>", "s:" or "<SNR>" */
21115 *arg += len;
21116 }
21117
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021119 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021121 p = find_name_end(*arg, &expr_start, &expr_end,
21122 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 if (expr_start != NULL)
21124 {
21125 char_u *temp_string;
21126
21127 if (!evaluate)
21128 {
21129 len += (int)(p - *arg);
21130 *arg = skipwhite(p);
21131 return len;
21132 }
21133
21134 /*
21135 * Include any <SID> etc in the expanded string:
21136 * Thus the -len here.
21137 */
21138 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21139 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021140 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 *alias = temp_string;
21142 *arg = skipwhite(p);
21143 return (int)STRLEN(temp_string);
21144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145
21146 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021147 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 EMSG2(_(e_invexpr2), *arg);
21149
21150 return len;
21151}
21152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021153/*
21154 * Find the end of a variable or function name, taking care of magic braces.
21155 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21156 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021157 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 * Return a pointer to just after the name. Equal to "arg" if there is no
21159 * valid name.
21160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021162find_name_end(
21163 char_u *arg,
21164 char_u **expr_start,
21165 char_u **expr_end,
21166 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021168 int mb_nest = 0;
21169 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021171 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021173 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 *expr_start = NULL;
21176 *expr_end = NULL;
21177 }
21178
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021179 /* Quick check for valid starting character. */
21180 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21181 return arg;
21182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021183 for (p = arg; *p != NUL
21184 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021185 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021186 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021188 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021190 if (*p == '\'')
21191 {
21192 /* skip over 'string' to avoid counting [ and ] inside it. */
21193 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21194 ;
21195 if (*p == NUL)
21196 break;
21197 }
21198 else if (*p == '"')
21199 {
21200 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21201 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21202 if (*p == '\\' && p[1] != NUL)
21203 ++p;
21204 if (*p == NUL)
21205 break;
21206 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021207 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21208 {
21209 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021210 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021211 len = (int)(p - arg);
21212 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021213 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021214 break;
21215 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021219 if (*p == '[')
21220 ++br_nest;
21221 else if (*p == ']')
21222 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021224
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021225 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 if (*p == '{')
21228 {
21229 mb_nest++;
21230 if (expr_start != NULL && *expr_start == NULL)
21231 *expr_start = p;
21232 }
21233 else if (*p == '}')
21234 {
21235 mb_nest--;
21236 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21237 *expr_end = p;
21238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 }
21241
21242 return p;
21243}
21244
21245/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021246 * Expands out the 'magic' {}'s in a variable/function name.
21247 * Note that this can call itself recursively, to deal with
21248 * constructs like foo{bar}{baz}{bam}
21249 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21250 * "in_start" ^
21251 * "expr_start" ^
21252 * "expr_end" ^
21253 * "in_end" ^
21254 *
21255 * Returns a new allocated string, which the caller must free.
21256 * Returns NULL for failure.
21257 */
21258 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021259make_expanded_name(
21260 char_u *in_start,
21261 char_u *expr_start,
21262 char_u *expr_end,
21263 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021264{
21265 char_u c1;
21266 char_u *retval = NULL;
21267 char_u *temp_result;
21268 char_u *nextcmd = NULL;
21269
21270 if (expr_end == NULL || in_end == NULL)
21271 return NULL;
21272 *expr_start = NUL;
21273 *expr_end = NUL;
21274 c1 = *in_end;
21275 *in_end = NUL;
21276
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021277 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021278 if (temp_result != NULL && nextcmd == NULL)
21279 {
21280 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21281 + (in_end - expr_end) + 1));
21282 if (retval != NULL)
21283 {
21284 STRCPY(retval, in_start);
21285 STRCAT(retval, temp_result);
21286 STRCAT(retval, expr_end + 1);
21287 }
21288 }
21289 vim_free(temp_result);
21290
21291 *in_end = c1; /* put char back for error messages */
21292 *expr_start = '{';
21293 *expr_end = '}';
21294
21295 if (retval != NULL)
21296 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021297 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021298 if (expr_start != NULL)
21299 {
21300 /* Further expansion! */
21301 temp_result = make_expanded_name(retval, expr_start,
21302 expr_end, temp_result);
21303 vim_free(retval);
21304 retval = temp_result;
21305 }
21306 }
21307
21308 return retval;
21309}
21310
21311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021313 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 */
21315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021316eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021318 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21319}
21320
21321/*
21322 * Return TRUE if character "c" can be used as the first character in a
21323 * variable or function name (excluding '{' and '}').
21324 */
21325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021327{
21328 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329}
21330
21331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 * Set number v: variable to "val".
21333 */
21334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021335set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021337 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338}
21339
21340/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021341 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342 */
21343 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021344get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021346 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347}
21348
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021349/*
21350 * Get string v: variable value. Uses a static buffer, can only be used once.
21351 */
21352 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021353get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021354{
21355 return get_tv_string(&vimvars[idx].vv_tv);
21356}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021357
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021359 * Get List v: variable value. Caller must take care of reference count when
21360 * needed.
21361 */
21362 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021363get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021364{
21365 return vimvars[idx].vv_list;
21366}
21367
21368/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021369 * Set v:char to character "c".
21370 */
21371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021372set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021373{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021374 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021375
21376#ifdef FEAT_MBYTE
21377 if (has_mbyte)
21378 buf[(*mb_char2bytes)(c, buf)] = NUL;
21379 else
21380#endif
21381 {
21382 buf[0] = c;
21383 buf[1] = NUL;
21384 }
21385 set_vim_var_string(VV_CHAR, buf, -1);
21386}
21387
21388/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021389 * Set v:count to "count" and v:count1 to "count1".
21390 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 */
21392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393set_vcount(
21394 long count,
21395 long count1,
21396 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021398 if (set_prevcount)
21399 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021400 vimvars[VV_COUNT].vv_nr = count;
21401 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402}
21403
21404/*
21405 * Set string v: variable to a copy of "val".
21406 */
21407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021408set_vim_var_string(
21409 int idx,
21410 char_u *val,
21411 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412{
Bram Moolenaara542c682016-01-31 16:28:04 +010021413 clear_tv(&vimvars[idx].vv_di.di_tv);
21414 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021416 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021418 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021420 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421}
21422
21423/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021424 * Set List v: variable to "val".
21425 */
21426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021427set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021428{
Bram Moolenaara542c682016-01-31 16:28:04 +010021429 clear_tv(&vimvars[idx].vv_di.di_tv);
21430 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021431 vimvars[idx].vv_list = val;
21432 if (val != NULL)
21433 ++val->lv_refcount;
21434}
21435
21436/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021437 * Set Dictionary v: variable to "val".
21438 */
21439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021440set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021441{
21442 int todo;
21443 hashitem_T *hi;
21444
Bram Moolenaara542c682016-01-31 16:28:04 +010021445 clear_tv(&vimvars[idx].vv_di.di_tv);
21446 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021447 vimvars[idx].vv_dict = val;
21448 if (val != NULL)
21449 {
21450 ++val->dv_refcount;
21451
21452 /* Set readonly */
21453 todo = (int)val->dv_hashtab.ht_used;
21454 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21455 {
21456 if (HASHITEM_EMPTY(hi))
21457 continue;
21458 --todo;
21459 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21460 }
21461 }
21462}
21463
21464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 * Set v:register if needed.
21466 */
21467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021468set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469{
21470 char_u regname;
21471
21472 if (c == 0 || c == ' ')
21473 regname = '"';
21474 else
21475 regname = c;
21476 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021477 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 set_vim_var_string(VV_REG, &regname, 1);
21479}
21480
21481/*
21482 * Get or set v:exception. If "oldval" == NULL, return the current value.
21483 * Otherwise, restore the value to "oldval" and return NULL.
21484 * Must always be called in pairs to save and restore v:exception! Does not
21485 * take care of memory allocations.
21486 */
21487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021488v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489{
21490 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492
Bram Moolenaare9a41262005-01-15 22:18:47 +000021493 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 return NULL;
21495}
21496
21497/*
21498 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21499 * Otherwise, restore the value to "oldval" and return NULL.
21500 * Must always be called in pairs to save and restore v:throwpoint! Does not
21501 * take care of memory allocations.
21502 */
21503 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021504v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505{
21506 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021507 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508
Bram Moolenaare9a41262005-01-15 22:18:47 +000021509 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 return NULL;
21511}
21512
21513#if defined(FEAT_AUTOCMD) || defined(PROTO)
21514/*
21515 * Set v:cmdarg.
21516 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21517 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21518 * Must always be called in pairs!
21519 */
21520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021521set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522{
21523 char_u *oldval;
21524 char_u *newval;
21525 unsigned len;
21526
Bram Moolenaare9a41262005-01-15 22:18:47 +000021527 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021528 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021530 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021531 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021532 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 }
21534
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021535 if (eap->force_bin == FORCE_BIN)
21536 len = 6;
21537 else if (eap->force_bin == FORCE_NOBIN)
21538 len = 8;
21539 else
21540 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021541
21542 if (eap->read_edit)
21543 len += 7;
21544
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021545 if (eap->force_ff != 0)
21546 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21547# ifdef FEAT_MBYTE
21548 if (eap->force_enc != 0)
21549 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021550 if (eap->bad_char != 0)
21551 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021552# endif
21553
21554 newval = alloc(len + 1);
21555 if (newval == NULL)
21556 return NULL;
21557
21558 if (eap->force_bin == FORCE_BIN)
21559 sprintf((char *)newval, " ++bin");
21560 else if (eap->force_bin == FORCE_NOBIN)
21561 sprintf((char *)newval, " ++nobin");
21562 else
21563 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021564
21565 if (eap->read_edit)
21566 STRCAT(newval, " ++edit");
21567
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021568 if (eap->force_ff != 0)
21569 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21570 eap->cmd + eap->force_ff);
21571# ifdef FEAT_MBYTE
21572 if (eap->force_enc != 0)
21573 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21574 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021575 if (eap->bad_char == BAD_KEEP)
21576 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21577 else if (eap->bad_char == BAD_DROP)
21578 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21579 else if (eap->bad_char != 0)
21580 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021581# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021582 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021583 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584}
21585#endif
21586
21587/*
21588 * Get the value of internal variable "name".
21589 * Return OK or FAIL.
21590 */
21591 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021592get_var_tv(
21593 char_u *name,
21594 int len, /* length of "name" */
21595 typval_T *rettv, /* NULL when only checking existence */
21596 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21597 int verbose, /* may give error message */
21598 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599{
21600 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021601 typval_T *tv = NULL;
21602 typval_T atv;
21603 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605
21606 /* truncate the name, so that we can use strcmp() */
21607 cc = name[len];
21608 name[len] = NUL;
21609
21610 /*
21611 * Check for "b:changedtick".
21612 */
21613 if (STRCMP(name, "b:changedtick") == 0)
21614 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021615 atv.v_type = VAR_NUMBER;
21616 atv.vval.v_number = curbuf->b_changedtick;
21617 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 }
21619
21620 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 * Check for user-defined variables.
21622 */
21623 else
21624 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021625 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021627 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021628 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021629 if (dip != NULL)
21630 *dip = v;
21631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 }
21633
Bram Moolenaare9a41262005-01-15 22:18:47 +000021634 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021636 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021637 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 ret = FAIL;
21639 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021641 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642
21643 name[len] = cc;
21644
21645 return ret;
21646}
21647
21648/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021649 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21650 * Also handle function call with Funcref variable: func(expr)
21651 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21652 */
21653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021654handle_subscript(
21655 char_u **arg,
21656 typval_T *rettv,
21657 int evaluate, /* do more than finding the end */
21658 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021659{
21660 int ret = OK;
21661 dict_T *selfdict = NULL;
21662 char_u *s;
21663 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021664 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021665
21666 while (ret == OK
21667 && (**arg == '['
21668 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021669 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21670 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021671 && !vim_iswhite(*(*arg - 1)))
21672 {
21673 if (**arg == '(')
21674 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021675 partial_T *pt = NULL;
21676
Bram Moolenaard9fba312005-06-26 22:34:35 +000021677 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021678 if (evaluate)
21679 {
21680 functv = *rettv;
21681 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021682
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021683 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021684 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021685 {
21686 pt = functv.vval.v_partial;
21687 s = pt->pt_name;
21688 }
21689 else
21690 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021691 }
21692 else
21693 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021694 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021695 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021696 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021697
21698 /* Clear the funcref afterwards, so that deleting it while
21699 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021700 if (evaluate)
21701 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021702
21703 /* Stop the expression evaluation when immediately aborting on
21704 * error, or when an interrupt occurred or an exception was thrown
21705 * but not caught. */
21706 if (aborting())
21707 {
21708 if (ret == OK)
21709 clear_tv(rettv);
21710 ret = FAIL;
21711 }
21712 dict_unref(selfdict);
21713 selfdict = NULL;
21714 }
21715 else /* **arg == '[' || **arg == '.' */
21716 {
21717 dict_unref(selfdict);
21718 if (rettv->v_type == VAR_DICT)
21719 {
21720 selfdict = rettv->vval.v_dict;
21721 if (selfdict != NULL)
21722 ++selfdict->dv_refcount;
21723 }
21724 else
21725 selfdict = NULL;
21726 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21727 {
21728 clear_tv(rettv);
21729 ret = FAIL;
21730 }
21731 }
21732 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021733
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021734 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21735 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021736 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021737 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21738 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021739 char_u *tofree = NULL;
21740 ufunc_T *fp;
21741 char_u fname_buf[FLEN_FIXED + 1];
21742 int error;
21743
21744 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021745 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021746 fp = find_func(fname);
21747 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021748
21749 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021750 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021751 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021752 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21753
21754 if (pt != NULL)
21755 {
21756 pt->pt_refcount = 1;
21757 pt->pt_dict = selfdict;
21758 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021759 if (rettv->v_type == VAR_FUNC)
21760 {
21761 /* just a function: use selfdict */
21762 pt->pt_name = rettv->vval.v_string;
21763 }
21764 else
21765 {
21766 partial_T *ret_pt = rettv->vval.v_partial;
21767 int i;
21768
21769 /* partial: use selfdict and copy args */
21770 pt->pt_name = vim_strsave(ret_pt->pt_name);
21771 if (ret_pt->pt_argc > 0)
21772 {
21773 pt->pt_argv = (typval_T *)alloc(
21774 sizeof(typval_T) * ret_pt->pt_argc);
21775 if (pt->pt_argv == NULL)
21776 /* out of memory: drop the arguments */
21777 pt->pt_argc = 0;
21778 else
21779 {
21780 pt->pt_argc = ret_pt->pt_argc;
21781 for (i = 0; i < pt->pt_argc; i++)
21782 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21783 }
21784 }
21785 partial_unref(ret_pt);
21786 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021787 func_ref(pt->pt_name);
21788 rettv->v_type = VAR_PARTIAL;
21789 rettv->vval.v_partial = pt;
21790 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021791 }
21792 }
21793
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021794 dict_unref(selfdict);
21795 return ret;
21796}
21797
21798/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021799 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021800 * value).
21801 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021802 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021803alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021804{
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021806}
21807
21808/*
21809 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 * The string "s" must have been allocated, it is consumed.
21811 * Return NULL for out of memory, the variable otherwise.
21812 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021814alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021815{
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021818 rettv = alloc_tv();
21819 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021821 rettv->v_type = VAR_STRING;
21822 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 }
21824 else
21825 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021826 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827}
21828
21829/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021830 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021833free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834{
21835 if (varp != NULL)
21836 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021837 switch (varp->v_type)
21838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021839 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021840 func_unref(varp->vval.v_string);
21841 /*FALLTHROUGH*/
21842 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021843 vim_free(varp->vval.v_string);
21844 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021845 case VAR_PARTIAL:
21846 partial_unref(varp->vval.v_partial);
21847 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021848 case VAR_LIST:
21849 list_unref(varp->vval.v_list);
21850 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021851 case VAR_DICT:
21852 dict_unref(varp->vval.v_dict);
21853 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021854 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021855#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021856 job_unref(varp->vval.v_job);
21857 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021858#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021859 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021860#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021861 channel_unref(varp->vval.v_channel);
21862 break;
21863#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021864 case VAR_NUMBER:
21865 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021866 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021867 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021868 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 vim_free(varp);
21871 }
21872}
21873
21874/*
21875 * Free the memory for a variable value and set the value to NULL or 0.
21876 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021878clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879{
21880 if (varp != NULL)
21881 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021884 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021885 func_unref(varp->vval.v_string);
21886 /*FALLTHROUGH*/
21887 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 vim_free(varp->vval.v_string);
21889 varp->vval.v_string = NULL;
21890 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021891 case VAR_PARTIAL:
21892 partial_unref(varp->vval.v_partial);
21893 varp->vval.v_partial = NULL;
21894 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021895 case VAR_LIST:
21896 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021897 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021898 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021899 case VAR_DICT:
21900 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021901 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021902 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021903 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021904 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 varp->vval.v_number = 0;
21906 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021907 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021908#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021909 varp->vval.v_float = 0.0;
21910 break;
21911#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021912 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021913#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021914 job_unref(varp->vval.v_job);
21915 varp->vval.v_job = NULL;
21916#endif
21917 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021918 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021920 channel_unref(varp->vval.v_channel);
21921 varp->vval.v_channel = NULL;
21922#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021923 case VAR_UNKNOWN:
21924 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021926 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 }
21928}
21929
21930/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021931 * Set the value of a variable to NULL without freeing items.
21932 */
21933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021934init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935{
21936 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021938}
21939
21940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 * Get the number value of a variable.
21942 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021943 * For incompatible types, return 0.
21944 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21945 * caller of incompatible types: it sets *denote to TRUE if "denote"
21946 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021948 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021949get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021951 int error = FALSE;
21952
21953 return get_tv_number_chk(varp, &error); /* return 0L on error */
21954}
21955
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021956 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021957get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021958{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021959 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021961 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021963 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021964 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021965 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021966#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021967 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021968 break;
21969#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021970 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021971 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021972 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973 break;
21974 case VAR_STRING:
21975 if (varp->vval.v_string != NULL)
21976 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021977 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021978 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021979 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021980 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021981 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021982 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021983 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021984 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021985 case VAR_SPECIAL:
21986 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21987 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021988 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021989#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021990 EMSG(_("E910: Using a Job as a Number"));
21991 break;
21992#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021993 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021994#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021995 EMSG(_("E913: Using a Channel as a Number"));
21996 break;
21997#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021998 case VAR_UNKNOWN:
21999 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022002 if (denote == NULL) /* useful for values that must be unsigned */
22003 n = -1;
22004 else
22005 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007}
22008
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022009#ifdef FEAT_FLOAT
22010 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022011get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022012{
22013 switch (varp->v_type)
22014 {
22015 case VAR_NUMBER:
22016 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022017 case VAR_FLOAT:
22018 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022019 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022020 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022021 EMSG(_("E891: Using a Funcref as a Float"));
22022 break;
22023 case VAR_STRING:
22024 EMSG(_("E892: Using a String as a Float"));
22025 break;
22026 case VAR_LIST:
22027 EMSG(_("E893: Using a List as a Float"));
22028 break;
22029 case VAR_DICT:
22030 EMSG(_("E894: Using a Dictionary as a Float"));
22031 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022032 case VAR_SPECIAL:
22033 EMSG(_("E907: Using a special value as a Float"));
22034 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022035 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022036# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022037 EMSG(_("E911: Using a Job as a Float"));
22038 break;
22039# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022040 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022041# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022042 EMSG(_("E914: Using a Channel as a Float"));
22043 break;
22044# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022045 case VAR_UNKNOWN:
22046 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022047 break;
22048 }
22049 return 0;
22050}
22051#endif
22052
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022054 * Get the lnum from the first argument.
22055 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022056 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 */
22058 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022059get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060{
Bram Moolenaar33570922005-01-25 22:26:29 +000022061 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 linenr_T lnum;
22063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022064 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 if (lnum == 0) /* no valid number, try using line() */
22066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 rettv.v_type = VAR_NUMBER;
22068 f_line(argvars, &rettv);
22069 lnum = rettv.vval.v_number;
22070 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 }
22072 return lnum;
22073}
22074
22075/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022076 * Get the lnum from the first argument.
22077 * Also accepts "$", then "buf" is used.
22078 * Returns 0 on error.
22079 */
22080 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022081get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022082{
22083 if (argvars[0].v_type == VAR_STRING
22084 && argvars[0].vval.v_string != NULL
22085 && argvars[0].vval.v_string[0] == '$'
22086 && buf != NULL)
22087 return buf->b_ml.ml_line_count;
22088 return get_tv_number_chk(&argvars[0], NULL);
22089}
22090
22091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 * Get the string value of a variable.
22093 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022094 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22095 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 * If the String variable has never been set, return an empty string.
22097 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022098 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22099 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
22104 static char_u mybuf[NUMBUFLEN];
22105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022106 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107}
22108
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022109 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022110get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022112 char_u *res = get_tv_string_buf_chk(varp, buf);
22113
22114 return res != NULL ? res : (char_u *)"";
22115}
22116
Bram Moolenaar7d647822014-04-05 21:28:56 +020022117/*
22118 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22119 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022120 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022121get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022122{
22123 static char_u mybuf[NUMBUFLEN];
22124
22125 return get_tv_string_buf_chk(varp, mybuf);
22126}
22127
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022128 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022129get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022130{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022131 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 case VAR_NUMBER:
22134 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22135 return buf;
22136 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022137 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022138 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022139 break;
22140 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022141 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022142 break;
22143 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022144 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022147#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022148 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022149 break;
22150#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151 case VAR_STRING:
22152 if (varp->vval.v_string != NULL)
22153 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022154 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022155 case VAR_SPECIAL:
22156 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22157 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022158 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022159#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022160 {
22161 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022162 char *status;
22163
22164 if (job == NULL)
22165 return (char_u *)"no process";
22166 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022167 : job->jv_status == JOB_ENDED ? "dead"
22168 : "run";
22169# ifdef UNIX
22170 vim_snprintf((char *)buf, NUMBUFLEN,
22171 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022172# elif defined(WIN32)
22173 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022174 "process %ld %s",
22175 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022176 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022177# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022178 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022179 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22180# endif
22181 return buf;
22182 }
22183#endif
22184 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022185 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022186#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022187 {
22188 channel_T *channel = varp->vval.v_channel;
22189 char *status = channel_status(channel);
22190
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022191 if (channel == NULL)
22192 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22193 else
22194 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022195 "channel %d %s", channel->ch_id, status);
22196 return buf;
22197 }
22198#endif
22199 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022200 case VAR_UNKNOWN:
22201 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022202 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022204 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205}
22206
22207/*
22208 * Find variable "name" in the list of variables.
22209 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022210 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022211 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022212 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022218 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219
Bram Moolenaara7043832005-01-21 11:56:39 +000022220 ht = find_var_ht(name, &varname);
22221 if (htp != NULL)
22222 *htp = ht;
22223 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022225 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226}
22227
22228/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022229 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022230 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022231 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022232 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233find_var_in_ht(
22234 hashtab_T *ht,
22235 int htname,
22236 char_u *varname,
22237 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022238{
Bram Moolenaar33570922005-01-25 22:26:29 +000022239 hashitem_T *hi;
22240
22241 if (*varname == NUL)
22242 {
22243 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022244 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022246 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 case 'g': return &globvars_var;
22248 case 'v': return &vimvars_var;
22249 case 'b': return &curbuf->b_bufvar;
22250 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022251#ifdef FEAT_WINDOWS
22252 case 't': return &curtab->tp_winvar;
22253#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022254 case 'l': return current_funccal == NULL
22255 ? NULL : &current_funccal->l_vars_var;
22256 case 'a': return current_funccal == NULL
22257 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022258 }
22259 return NULL;
22260 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022261
22262 hi = hash_find(ht, varname);
22263 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022264 {
22265 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022266 * worked find the variable again. Don't auto-load a script if it was
22267 * loaded already, otherwise it would be loaded every time when
22268 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022269 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022270 {
22271 /* Note: script_autoload() may make "hi" invalid. It must either
22272 * be obtained again or not used. */
22273 if (!script_autoload(varname, FALSE) || aborting())
22274 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022275 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022276 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022277 if (HASHITEM_EMPTY(hi))
22278 return NULL;
22279 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022280 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022281}
22282
22283/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022284 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022285 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022286 * Set "varname" to the start of name without ':'.
22287 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022289find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022291 hashitem_T *hi;
22292
Bram Moolenaar73627d02015-08-11 15:46:09 +020022293 if (name[0] == NUL)
22294 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 if (name[1] != ':')
22296 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022297 /* The name must not start with a colon or #. */
22298 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 return NULL;
22300 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022301
22302 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022303 hi = hash_find(&compat_hashtab, name);
22304 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022305 return &compat_hashtab;
22306
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022309 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 }
22311 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022312 if (*name == 'g') /* global variable */
22313 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022314 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22315 */
22316 if (vim_strchr(name + 2, ':') != NULL
22317 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022320 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022322 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022323#ifdef FEAT_WINDOWS
22324 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022325 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022326#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 if (*name == 'v') /* v: variable */
22328 return &vimvarht;
22329 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022330 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022332 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 if (*name == 's' /* script variable */
22334 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22335 return &SCRIPT_VARS(current_SID);
22336 return NULL;
22337}
22338
22339/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022340 * Get function call environment based on bactrace debug level
22341 */
22342 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022343get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022344{
22345 int i;
22346 funccall_T *funccal;
22347 funccall_T *temp_funccal;
22348
22349 funccal = current_funccal;
22350 if (debug_backtrace_level > 0)
22351 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022352 for (i = 0; i < debug_backtrace_level; i++)
22353 {
22354 temp_funccal = funccal->caller;
22355 if (temp_funccal)
22356 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022357 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022358 /* backtrace level overflow. reset to max */
22359 debug_backtrace_level = i;
22360 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022361 }
22362 return funccal;
22363}
22364
22365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022367 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 * Returns NULL when it doesn't exist.
22369 */
22370 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022371get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372{
Bram Moolenaar33570922005-01-25 22:26:29 +000022373 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022375 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 if (v == NULL)
22377 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379}
22380
22381/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022382 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 * sourcing this script and when executing functions defined in the script.
22384 */
22385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022386new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387{
Bram Moolenaara7043832005-01-21 11:56:39 +000022388 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 hashtab_T *ht;
22390 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022391
Bram Moolenaar071d4272004-06-13 20:20:40 +000022392 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22393 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022394 /* Re-allocating ga_data means that an ht_array pointing to
22395 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022396 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022397 for (i = 1; i <= ga_scripts.ga_len; ++i)
22398 {
22399 ht = &SCRIPT_VARS(i);
22400 if (ht->ht_mask == HT_INIT_SIZE - 1)
22401 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022402 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022404 }
22405
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 while (ga_scripts.ga_len < id)
22407 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022408 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022409 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022410 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 }
22413 }
22414}
22415
22416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022417 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22418 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 */
22420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022421init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422{
Bram Moolenaar33570922005-01-25 22:26:29 +000022423 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022424 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022425 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022426 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022427 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 dict_var->di_tv.vval.v_dict = dict;
22429 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022430 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022431 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22432 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022433}
22434
22435/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022436 * Unreference a dictionary initialized by init_var_dict().
22437 */
22438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022439unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022440{
22441 /* Now the dict needs to be freed if no one else is using it, go back to
22442 * normal reference counting. */
22443 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22444 dict_unref(dict);
22445}
22446
22447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 * Frees all allocated variables and the value they contain.
22450 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 */
22452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022453vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022454{
22455 vars_clear_ext(ht, TRUE);
22456}
22457
22458/*
22459 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22460 */
22461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022462vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463{
Bram Moolenaara7043832005-01-21 11:56:39 +000022464 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 hashitem_T *hi;
22466 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467
Bram Moolenaar33570922005-01-25 22:26:29 +000022468 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022469 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022470 for (hi = ht->ht_array; todo > 0; ++hi)
22471 {
22472 if (!HASHITEM_EMPTY(hi))
22473 {
22474 --todo;
22475
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022477 * ht_array might change then. hash_clear() takes care of it
22478 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022479 v = HI2DI(hi);
22480 if (free_val)
22481 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022482 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022484 }
22485 }
22486 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022487 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488}
22489
Bram Moolenaara7043832005-01-21 11:56:39 +000022490/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 * Delete a variable from hashtab "ht" at item "hi".
22492 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022495delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496{
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022498
22499 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 clear_tv(&di->di_tv);
22501 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502}
22503
22504/*
22505 * List the value of one internal variable.
22506 */
22507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022508list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510 char_u *tofree;
22511 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022512 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022513
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022514 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022516 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022517 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518}
22519
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022521list_one_var_a(
22522 char_u *prefix,
22523 char_u *name,
22524 int type,
22525 char_u *string,
22526 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527{
Bram Moolenaar31859182007-08-14 20:41:13 +000022528 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22529 msg_start();
22530 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 if (name != NULL) /* "a:" vars don't have a name stored */
22532 msg_puts(name);
22533 msg_putchar(' ');
22534 msg_advance(22);
22535 if (type == VAR_NUMBER)
22536 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022537 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 msg_putchar('*');
22539 else if (type == VAR_LIST)
22540 {
22541 msg_putchar('[');
22542 if (*string == '[')
22543 ++string;
22544 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022545 else if (type == VAR_DICT)
22546 {
22547 msg_putchar('{');
22548 if (*string == '{')
22549 ++string;
22550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 else
22552 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022555
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022556 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022557 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022558 if (*first)
22559 {
22560 msg_clr_eos();
22561 *first = FALSE;
22562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563}
22564
22565/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022566 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567 * If the variable already exists, the value is updated.
22568 * Otherwise the variable is created.
22569 */
22570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022571set_var(
22572 char_u *name,
22573 typval_T *tv,
22574 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575{
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022580 ht = find_var_ht(name, &varname);
22581 if (ht == NULL || *varname == NUL)
22582 {
22583 EMSG2(_(e_illvar), name);
22584 return;
22585 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022586 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022587
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022588 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22589 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022590 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022591
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022595 if (var_check_ro(v->di_flags, name, FALSE)
22596 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022597 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022598
22599 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022600 * Handle setting internal v: variables separately where needed to
22601 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 */
22603 if (ht == &vimvarht)
22604 {
22605 if (v->di_tv.v_type == VAR_STRING)
22606 {
22607 vim_free(v->di_tv.vval.v_string);
22608 if (copy || tv->v_type != VAR_STRING)
22609 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22610 else
22611 {
22612 /* Take over the string to avoid an extra alloc/free. */
22613 v->di_tv.vval.v_string = tv->vval.v_string;
22614 tv->vval.v_string = NULL;
22615 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022616 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022617 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022618 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022619 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022621 if (STRCMP(varname, "searchforward") == 0)
22622 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022623#ifdef FEAT_SEARCH_EXTRA
22624 else if (STRCMP(varname, "hlsearch") == 0)
22625 {
22626 no_hlsearch = !v->di_tv.vval.v_number;
22627 redraw_all_later(SOME_VALID);
22628 }
22629#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022630 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022631 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022632 else if (v->di_tv.v_type != tv->v_type)
22633 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 }
22635
22636 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
22638 else /* add a new variable */
22639 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022640 /* Can't add "v:" variable. */
22641 if (ht == &vimvarht)
22642 {
22643 EMSG2(_(e_illvar), name);
22644 return;
22645 }
22646
Bram Moolenaar92124a32005-06-17 22:03:40 +000022647 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022648 if (!valid_varname(varname))
22649 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022650
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022651 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22652 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022653 if (v == NULL)
22654 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022658 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 return;
22660 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022661 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022663
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022664 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022666 else
22667 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022668 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022669 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022670 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672}
22673
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022674/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022675 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 * Also give an error message.
22677 */
22678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022679var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022680{
22681 if (flags & DI_FLAGS_RO)
22682 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022683 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022684 return TRUE;
22685 }
22686 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22687 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022688 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 return TRUE;
22690 }
22691 return FALSE;
22692}
22693
22694/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022695 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22696 * Also give an error message.
22697 */
22698 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022699var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022700{
22701 if (flags & DI_FLAGS_FIX)
22702 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022703 EMSG2(_("E795: Cannot delete variable %s"),
22704 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022705 return TRUE;
22706 }
22707 return FALSE;
22708}
22709
22710/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022711 * Check if a funcref is assigned to a valid variable name.
22712 * Return TRUE and give an error if not.
22713 */
22714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022715var_check_func_name(
22716 char_u *name, /* points to start of variable name */
22717 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022718{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022719 /* Allow for w: b: s: and t:. */
22720 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022721 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22722 ? name[2] : name[0]))
22723 {
22724 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22725 name);
22726 return TRUE;
22727 }
22728 /* Don't allow hiding a function. When "v" is not NULL we might be
22729 * assigning another function to the same var, the type is checked
22730 * below. */
22731 if (new_var && function_exists(name))
22732 {
22733 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22734 name);
22735 return TRUE;
22736 }
22737 return FALSE;
22738}
22739
22740/*
22741 * Check if a variable name is valid.
22742 * Return FALSE and give an error if not.
22743 */
22744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022745valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022746{
22747 char_u *p;
22748
22749 for (p = varname; *p != NUL; ++p)
22750 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22751 && *p != AUTOLOAD_CHAR)
22752 {
22753 EMSG2(_(e_illvar), varname);
22754 return FALSE;
22755 }
22756 return TRUE;
22757}
22758
22759/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022760 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022761 * Also give an error message, using "name" or _("name") when use_gettext is
22762 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022763 */
22764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022765tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022766{
22767 if (lock & VAR_LOCKED)
22768 {
22769 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022770 name == NULL ? (char_u *)_("Unknown")
22771 : use_gettext ? (char_u *)_(name)
22772 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022773 return TRUE;
22774 }
22775 if (lock & VAR_FIXED)
22776 {
22777 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022778 name == NULL ? (char_u *)_("Unknown")
22779 : use_gettext ? (char_u *)_(name)
22780 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022781 return TRUE;
22782 }
22783 return FALSE;
22784}
22785
22786/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022789 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022790 * It is OK for "from" and "to" to point to the same item. This is used to
22791 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022792 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022794copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022796 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022797 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022798 switch (from->v_type)
22799 {
22800 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022801 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022802 to->vval.v_number = from->vval.v_number;
22803 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022804 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022805#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022806 to->vval.v_float = from->vval.v_float;
22807 break;
22808#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022809 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022810#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022811 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022812 if (to->vval.v_job != NULL)
22813 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022814 break;
22815#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022816 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022817#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022818 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022819 if (to->vval.v_channel != NULL)
22820 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022821 break;
22822#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022823 case VAR_STRING:
22824 case VAR_FUNC:
22825 if (from->vval.v_string == NULL)
22826 to->vval.v_string = NULL;
22827 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022828 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022829 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022830 if (from->v_type == VAR_FUNC)
22831 func_ref(to->vval.v_string);
22832 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022833 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022834 case VAR_PARTIAL:
22835 if (from->vval.v_partial == NULL)
22836 to->vval.v_partial = NULL;
22837 else
22838 {
22839 to->vval.v_partial = from->vval.v_partial;
22840 ++to->vval.v_partial->pt_refcount;
22841 }
22842 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022843 case VAR_LIST:
22844 if (from->vval.v_list == NULL)
22845 to->vval.v_list = NULL;
22846 else
22847 {
22848 to->vval.v_list = from->vval.v_list;
22849 ++to->vval.v_list->lv_refcount;
22850 }
22851 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022852 case VAR_DICT:
22853 if (from->vval.v_dict == NULL)
22854 to->vval.v_dict = NULL;
22855 else
22856 {
22857 to->vval.v_dict = from->vval.v_dict;
22858 ++to->vval.v_dict->dv_refcount;
22859 }
22860 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022861 case VAR_UNKNOWN:
22862 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022863 break;
22864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865}
22866
22867/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022868 * Make a copy of an item.
22869 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022870 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22871 * reference to an already copied list/dict can be used.
22872 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022873 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022875item_copy(
22876 typval_T *from,
22877 typval_T *to,
22878 int deep,
22879 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022880{
22881 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022882 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022883
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022885 {
22886 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022887 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022888 }
22889 ++recurse;
22890
22891 switch (from->v_type)
22892 {
22893 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022894 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022895 case VAR_STRING:
22896 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022897 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022898 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022899 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022900 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022901 copy_tv(from, to);
22902 break;
22903 case VAR_LIST:
22904 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022905 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022906 if (from->vval.v_list == NULL)
22907 to->vval.v_list = NULL;
22908 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22909 {
22910 /* use the copy made earlier */
22911 to->vval.v_list = from->vval.v_list->lv_copylist;
22912 ++to->vval.v_list->lv_refcount;
22913 }
22914 else
22915 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22916 if (to->vval.v_list == NULL)
22917 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022918 break;
22919 case VAR_DICT:
22920 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022921 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022922 if (from->vval.v_dict == NULL)
22923 to->vval.v_dict = NULL;
22924 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22925 {
22926 /* use the copy made earlier */
22927 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22928 ++to->vval.v_dict->dv_refcount;
22929 }
22930 else
22931 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22932 if (to->vval.v_dict == NULL)
22933 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022934 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022935 case VAR_UNKNOWN:
22936 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022937 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022938 }
22939 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022940 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022941}
22942
22943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 * ":echo expr1 ..." print each argument separated with a space, add a
22945 * newline at the end.
22946 * ":echon expr1 ..." print each argument plain.
22947 */
22948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022949ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950{
22951 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022953 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 char_u *p;
22955 int needclr = TRUE;
22956 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022957 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958
22959 if (eap->skip)
22960 ++emsg_skip;
22961 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22962 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022963 /* If eval1() causes an error message the text from the command may
22964 * still need to be cleared. E.g., "echo 22,44". */
22965 need_clr_eos = needclr;
22966
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022968 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022969 {
22970 /*
22971 * Report the invalid expression unless the expression evaluation
22972 * has been cancelled due to an aborting error, an interrupt, or an
22973 * exception.
22974 */
22975 if (!aborting())
22976 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022977 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 break;
22979 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022980 need_clr_eos = FALSE;
22981
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 if (!eap->skip)
22983 {
22984 if (atstart)
22985 {
22986 atstart = FALSE;
22987 /* Call msg_start() after eval1(), evaluating the expression
22988 * may cause a message to appear. */
22989 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022990 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022991 /* Mark the saved text as finishing the line, so that what
22992 * follows is displayed on a new line when scrolling back
22993 * at the more prompt. */
22994 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 }
22998 else if (eap->cmdidx == CMD_echo)
22999 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023000 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 if (p != NULL)
23002 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023004 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023006 if (*p != TAB && needclr)
23007 {
23008 /* remove any text still there from the command */
23009 msg_clr_eos();
23010 needclr = FALSE;
23011 }
23012 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 }
23014 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023015 {
23016#ifdef FEAT_MBYTE
23017 if (has_mbyte)
23018 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023019 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023020
23021 (void)msg_outtrans_len_attr(p, i, echo_attr);
23022 p += i - 1;
23023 }
23024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023026 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023029 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023031 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 arg = skipwhite(arg);
23033 }
23034 eap->nextcmd = check_nextcmd(arg);
23035
23036 if (eap->skip)
23037 --emsg_skip;
23038 else
23039 {
23040 /* remove text that may still be there from the command */
23041 if (needclr)
23042 msg_clr_eos();
23043 if (eap->cmdidx == CMD_echo)
23044 msg_end();
23045 }
23046}
23047
23048/*
23049 * ":echohl {name}".
23050 */
23051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023052ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053{
23054 int id;
23055
23056 id = syn_name2id(eap->arg);
23057 if (id == 0)
23058 echo_attr = 0;
23059 else
23060 echo_attr = syn_id2attr(id);
23061}
23062
23063/*
23064 * ":execute expr1 ..." execute the result of an expression.
23065 * ":echomsg expr1 ..." Print a message
23066 * ":echoerr expr1 ..." Print an error
23067 * Each gets spaces around each argument and a newline at the end for
23068 * echo commands
23069 */
23070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023071ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072{
23073 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 int ret = OK;
23076 char_u *p;
23077 garray_T ga;
23078 int len;
23079 int save_did_emsg;
23080
23081 ga_init2(&ga, 1, 80);
23082
23083 if (eap->skip)
23084 ++emsg_skip;
23085 while (*arg != NUL && *arg != '|' && *arg != '\n')
23086 {
23087 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023088 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 {
23090 /*
23091 * Report the invalid expression unless the expression evaluation
23092 * has been cancelled due to an aborting error, an interrupt, or an
23093 * exception.
23094 */
23095 if (!aborting())
23096 EMSG2(_(e_invexpr2), p);
23097 ret = FAIL;
23098 break;
23099 }
23100
23101 if (!eap->skip)
23102 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023103 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 len = (int)STRLEN(p);
23105 if (ga_grow(&ga, len + 2) == FAIL)
23106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023107 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 ret = FAIL;
23109 break;
23110 }
23111 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 ga.ga_len += len;
23115 }
23116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023117 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 arg = skipwhite(arg);
23119 }
23120
23121 if (ret != FAIL && ga.ga_data != NULL)
23122 {
23123 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023126 out_flush();
23127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 else if (eap->cmdidx == CMD_echoerr)
23129 {
23130 /* We don't want to abort following commands, restore did_emsg. */
23131 save_did_emsg = did_emsg;
23132 EMSG((char_u *)ga.ga_data);
23133 if (!force_abort)
23134 did_emsg = save_did_emsg;
23135 }
23136 else if (eap->cmdidx == CMD_execute)
23137 do_cmdline((char_u *)ga.ga_data,
23138 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23139 }
23140
23141 ga_clear(&ga);
23142
23143 if (eap->skip)
23144 --emsg_skip;
23145
23146 eap->nextcmd = check_nextcmd(arg);
23147}
23148
23149/*
23150 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23151 * "arg" points to the "&" or '+' when called, to "option" when returning.
23152 * Returns NULL when no option name found. Otherwise pointer to the char
23153 * after the option name.
23154 */
23155 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023156find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157{
23158 char_u *p = *arg;
23159
23160 ++p;
23161 if (*p == 'g' && p[1] == ':')
23162 {
23163 *opt_flags = OPT_GLOBAL;
23164 p += 2;
23165 }
23166 else if (*p == 'l' && p[1] == ':')
23167 {
23168 *opt_flags = OPT_LOCAL;
23169 p += 2;
23170 }
23171 else
23172 *opt_flags = 0;
23173
23174 if (!ASCII_ISALPHA(*p))
23175 return NULL;
23176 *arg = p;
23177
23178 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23179 p += 4; /* termcap option */
23180 else
23181 while (ASCII_ISALPHA(*p))
23182 ++p;
23183 return p;
23184}
23185
23186/*
23187 * ":function"
23188 */
23189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023190ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191{
23192 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023193 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 int j;
23195 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023197 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 char_u *name = NULL;
23199 char_u *p;
23200 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023201 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 garray_T newargs;
23203 garray_T newlines;
23204 int varargs = FALSE;
23205 int mustend = FALSE;
23206 int flags = 0;
23207 ufunc_T *fp;
23208 int indent;
23209 int nesting;
23210 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 dictitem_T *v;
23212 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023213 static int func_nr = 0; /* number for nameless function */
23214 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023215 hashtab_T *ht;
23216 int todo;
23217 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023218 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219
23220 /*
23221 * ":function" without argument: list functions.
23222 */
23223 if (ends_excmd(*eap->arg))
23224 {
23225 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023226 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023227 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023228 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023229 {
23230 if (!HASHITEM_EMPTY(hi))
23231 {
23232 --todo;
23233 fp = HI2UF(hi);
23234 if (!isdigit(*fp->uf_name))
23235 list_func_head(fp, FALSE);
23236 }
23237 }
23238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 eap->nextcmd = check_nextcmd(eap->arg);
23240 return;
23241 }
23242
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023243 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023244 * ":function /pat": list functions matching pattern.
23245 */
23246 if (*eap->arg == '/')
23247 {
23248 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23249 if (!eap->skip)
23250 {
23251 regmatch_T regmatch;
23252
23253 c = *p;
23254 *p = NUL;
23255 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23256 *p = c;
23257 if (regmatch.regprog != NULL)
23258 {
23259 regmatch.rm_ic = p_ic;
23260
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023261 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023262 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23263 {
23264 if (!HASHITEM_EMPTY(hi))
23265 {
23266 --todo;
23267 fp = HI2UF(hi);
23268 if (!isdigit(*fp->uf_name)
23269 && vim_regexec(&regmatch, fp->uf_name, 0))
23270 list_func_head(fp, FALSE);
23271 }
23272 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023273 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023274 }
23275 }
23276 if (*p == '/')
23277 ++p;
23278 eap->nextcmd = check_nextcmd(p);
23279 return;
23280 }
23281
23282 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023283 * Get the function name. There are these situations:
23284 * func normal function name
23285 * "name" == func, "fudi.fd_dict" == NULL
23286 * dict.func new dictionary entry
23287 * "name" == NULL, "fudi.fd_dict" set,
23288 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23289 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023290 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023291 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23292 * dict.func existing dict entry that's not a Funcref
23293 * "name" == NULL, "fudi.fd_dict" set,
23294 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023295 * s:func script-local function name
23296 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023299 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023300 paren = (vim_strchr(p, '(') != NULL);
23301 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 {
23303 /*
23304 * Return on an invalid expression in braces, unless the expression
23305 * evaluation has been cancelled due to an aborting error, an
23306 * interrupt, or an exception.
23307 */
23308 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023310 if (!eap->skip && fudi.fd_newkey != NULL)
23311 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023312 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 else
23316 eap->skip = TRUE;
23317 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023318
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 /* An error in a function call during evaluation of an expression in magic
23320 * braces should not cause the function not to be defined. */
23321 saved_did_emsg = did_emsg;
23322 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323
23324 /*
23325 * ":function func" with only function name: list function.
23326 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023327 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 {
23329 if (!ends_excmd(*skipwhite(p)))
23330 {
23331 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023332 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 }
23334 eap->nextcmd = check_nextcmd(p);
23335 if (eap->nextcmd != NULL)
23336 *p = NUL;
23337 if (!eap->skip && !got_int)
23338 {
23339 fp = find_func(name);
23340 if (fp != NULL)
23341 {
23342 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023343 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023345 if (FUNCLINE(fp, j) == NULL)
23346 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 msg_putchar('\n');
23348 msg_outnum((long)(j + 1));
23349 if (j < 9)
23350 msg_putchar(' ');
23351 if (j < 99)
23352 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023353 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 out_flush(); /* show a line at a time */
23355 ui_breakcheck();
23356 }
23357 if (!got_int)
23358 {
23359 msg_putchar('\n');
23360 msg_puts((char_u *)" endfunction");
23361 }
23362 }
23363 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023364 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023366 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 }
23368
23369 /*
23370 * ":function name(arg1, arg2)" Define function.
23371 */
23372 p = skipwhite(p);
23373 if (*p != '(')
23374 {
23375 if (!eap->skip)
23376 {
23377 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023378 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 }
23380 /* attempt to continue by skipping some text */
23381 if (vim_strchr(p, '(') != NULL)
23382 p = vim_strchr(p, '(');
23383 }
23384 p = skipwhite(p + 1);
23385
23386 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23387 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23388
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023389 if (!eap->skip)
23390 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023391 /* Check the name of the function. Unless it's a dictionary function
23392 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023393 if (name != NULL)
23394 arg = name;
23395 else
23396 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023397 if (arg != NULL && (fudi.fd_di == NULL
23398 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023399 {
23400 if (*arg == K_SPECIAL)
23401 j = 3;
23402 else
23403 j = 0;
23404 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23405 : eval_isnamec(arg[j])))
23406 ++j;
23407 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023408 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023409 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023410 /* Disallow using the g: dict. */
23411 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23412 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023413 }
23414
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 /*
23416 * Isolate the arguments: "arg1, arg2, ...)"
23417 */
23418 while (*p != ')')
23419 {
23420 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23421 {
23422 varargs = TRUE;
23423 p += 3;
23424 mustend = TRUE;
23425 }
23426 else
23427 {
23428 arg = p;
23429 while (ASCII_ISALNUM(*p) || *p == '_')
23430 ++p;
23431 if (arg == p || isdigit(*arg)
23432 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23433 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23434 {
23435 if (!eap->skip)
23436 EMSG2(_("E125: Illegal argument: %s"), arg);
23437 break;
23438 }
23439 if (ga_grow(&newargs, 1) == FAIL)
23440 goto erret;
23441 c = *p;
23442 *p = NUL;
23443 arg = vim_strsave(arg);
23444 if (arg == NULL)
23445 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023446
23447 /* Check for duplicate argument name. */
23448 for (i = 0; i < newargs.ga_len; ++i)
23449 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23450 {
23451 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023452 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023453 goto erret;
23454 }
23455
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23457 *p = c;
23458 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 if (*p == ',')
23460 ++p;
23461 else
23462 mustend = TRUE;
23463 }
23464 p = skipwhite(p);
23465 if (mustend && *p != ')')
23466 {
23467 if (!eap->skip)
23468 EMSG2(_(e_invarg2), eap->arg);
23469 break;
23470 }
23471 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023472 if (*p != ')')
23473 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 ++p; /* skip the ')' */
23475
Bram Moolenaare9a41262005-01-15 22:18:47 +000023476 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 for (;;)
23478 {
23479 p = skipwhite(p);
23480 if (STRNCMP(p, "range", 5) == 0)
23481 {
23482 flags |= FC_RANGE;
23483 p += 5;
23484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023485 else if (STRNCMP(p, "dict", 4) == 0)
23486 {
23487 flags |= FC_DICT;
23488 p += 4;
23489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 else if (STRNCMP(p, "abort", 5) == 0)
23491 {
23492 flags |= FC_ABORT;
23493 p += 5;
23494 }
23495 else
23496 break;
23497 }
23498
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023499 /* When there is a line break use what follows for the function body.
23500 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23501 if (*p == '\n')
23502 line_arg = p + 1;
23503 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 EMSG(_(e_trailing));
23505
23506 /*
23507 * Read the body of the function, until ":endfunction" is found.
23508 */
23509 if (KeyTyped)
23510 {
23511 /* Check if the function already exists, don't let the user type the
23512 * whole function before telling him it doesn't work! For a script we
23513 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023514 if (!eap->skip && !eap->forceit)
23515 {
23516 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23517 EMSG(_(e_funcdict));
23518 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023519 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023522 if (!eap->skip && did_emsg)
23523 goto erret;
23524
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 msg_putchar('\n'); /* don't overwrite the function name */
23526 cmdline_row = msg_row;
23527 }
23528
23529 indent = 2;
23530 nesting = 0;
23531 for (;;)
23532 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023533 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023534 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023535 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023536 saved_wait_return = FALSE;
23537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023539 sourcing_lnum_off = sourcing_lnum;
23540
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023541 if (line_arg != NULL)
23542 {
23543 /* Use eap->arg, split up in parts by line breaks. */
23544 theline = line_arg;
23545 p = vim_strchr(theline, '\n');
23546 if (p == NULL)
23547 line_arg += STRLEN(line_arg);
23548 else
23549 {
23550 *p = NUL;
23551 line_arg = p + 1;
23552 }
23553 }
23554 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 theline = getcmdline(':', 0L, indent);
23556 else
23557 theline = eap->getline(':', eap->cookie, indent);
23558 if (KeyTyped)
23559 lines_left = Rows - 1;
23560 if (theline == NULL)
23561 {
23562 EMSG(_("E126: Missing :endfunction"));
23563 goto erret;
23564 }
23565
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023566 /* Detect line continuation: sourcing_lnum increased more than one. */
23567 if (sourcing_lnum > sourcing_lnum_off + 1)
23568 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23569 else
23570 sourcing_lnum_off = 0;
23571
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 if (skip_until != NULL)
23573 {
23574 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23575 * don't check for ":endfunc". */
23576 if (STRCMP(theline, skip_until) == 0)
23577 {
23578 vim_free(skip_until);
23579 skip_until = NULL;
23580 }
23581 }
23582 else
23583 {
23584 /* skip ':' and blanks*/
23585 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23586 ;
23587
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023588 /* Check for "endfunction". */
23589 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023591 if (line_arg == NULL)
23592 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 break;
23594 }
23595
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023596 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 * at "end". */
23598 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23599 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023600 else if (STRNCMP(p, "if", 2) == 0
23601 || STRNCMP(p, "wh", 2) == 0
23602 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 || STRNCMP(p, "try", 3) == 0)
23604 indent += 2;
23605
23606 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023607 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023609 if (*p == '!')
23610 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023612 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023613 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023615 ++nesting;
23616 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 }
23618 }
23619
23620 /* Check for ":append" or ":insert". */
23621 p = skip_range(p, NULL);
23622 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23623 || (p[0] == 'i'
23624 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23625 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23626 skip_until = vim_strsave((char_u *)".");
23627
23628 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23629 arg = skipwhite(skiptowhite(p));
23630 if (arg[0] == '<' && arg[1] =='<'
23631 && ((p[0] == 'p' && p[1] == 'y'
23632 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23633 || (p[0] == 'p' && p[1] == 'e'
23634 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23635 || (p[0] == 't' && p[1] == 'c'
23636 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023637 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23638 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23640 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023641 || (p[0] == 'm' && p[1] == 'z'
23642 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 ))
23644 {
23645 /* ":python <<" continues until a dot, like ":append" */
23646 p = skipwhite(arg + 2);
23647 if (*p == NUL)
23648 skip_until = vim_strsave((char_u *)".");
23649 else
23650 skip_until = vim_strsave(p);
23651 }
23652 }
23653
23654 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023655 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023656 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023657 if (line_arg == NULL)
23658 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023660 }
23661
23662 /* Copy the line to newly allocated memory. get_one_sourceline()
23663 * allocates 250 bytes per line, this saves 80% on average. The cost
23664 * is an extra alloc/free. */
23665 p = vim_strsave(theline);
23666 if (p != NULL)
23667 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023668 if (line_arg == NULL)
23669 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023670 theline = p;
23671 }
23672
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023673 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23674
23675 /* Add NULL lines for continuation lines, so that the line count is
23676 * equal to the index in the growarray. */
23677 while (sourcing_lnum_off-- > 0)
23678 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023679
23680 /* Check for end of eap->arg. */
23681 if (line_arg != NULL && *line_arg == NUL)
23682 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 }
23684
23685 /* Don't define the function when skipping commands or when an error was
23686 * detected. */
23687 if (eap->skip || did_emsg)
23688 goto erret;
23689
23690 /*
23691 * If there are no errors, add the function
23692 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023693 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023694 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023695 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023696 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023697 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023698 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023699 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700 goto erret;
23701 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023702
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023703 fp = find_func(name);
23704 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023706 if (!eap->forceit)
23707 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023708 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023709 goto erret;
23710 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023711 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023712 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023713 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 name);
23715 goto erret;
23716 }
23717 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023718 ga_clear_strings(&(fp->uf_args));
23719 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023720 vim_free(name);
23721 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 }
23724 else
23725 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023726 char numbuf[20];
23727
23728 fp = NULL;
23729 if (fudi.fd_newkey == NULL && !eap->forceit)
23730 {
23731 EMSG(_(e_funcdict));
23732 goto erret;
23733 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023734 if (fudi.fd_di == NULL)
23735 {
23736 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023737 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023738 goto erret;
23739 }
23740 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023741 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023742 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023743
23744 /* Give the function a sequential number. Can only be used with a
23745 * Funcref! */
23746 vim_free(name);
23747 sprintf(numbuf, "%d", ++func_nr);
23748 name = vim_strsave((char_u *)numbuf);
23749 if (name == NULL)
23750 goto erret;
23751 }
23752
23753 if (fp == NULL)
23754 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023755 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 {
23757 int slen, plen;
23758 char_u *scriptname;
23759
23760 /* Check that the autoload name matches the script name. */
23761 j = FAIL;
23762 if (sourcing_name != NULL)
23763 {
23764 scriptname = autoload_name(name);
23765 if (scriptname != NULL)
23766 {
23767 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023768 plen = (int)STRLEN(p);
23769 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023770 if (slen > plen && fnamecmp(p,
23771 sourcing_name + slen - plen) == 0)
23772 j = OK;
23773 vim_free(scriptname);
23774 }
23775 }
23776 if (j == FAIL)
23777 {
23778 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23779 goto erret;
23780 }
23781 }
23782
23783 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 if (fp == NULL)
23785 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023786
23787 if (fudi.fd_dict != NULL)
23788 {
23789 if (fudi.fd_di == NULL)
23790 {
23791 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023792 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023793 if (fudi.fd_di == NULL)
23794 {
23795 vim_free(fp);
23796 goto erret;
23797 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023798 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23799 {
23800 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023801 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023802 goto erret;
23803 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023804 }
23805 else
23806 /* overwrite existing dict entry */
23807 clear_tv(&fudi.fd_di->di_tv);
23808 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023809 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023810 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023811 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023812
23813 /* behave like "dict" was used */
23814 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023815 }
23816
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023819 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23820 {
23821 vim_free(fp);
23822 goto erret;
23823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023825 fp->uf_args = newargs;
23826 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023827#ifdef FEAT_PROFILE
23828 fp->uf_tml_count = NULL;
23829 fp->uf_tml_total = NULL;
23830 fp->uf_tml_self = NULL;
23831 fp->uf_profiling = FALSE;
23832 if (prof_def_func())
23833 func_do_profile(fp);
23834#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023835 fp->uf_varargs = varargs;
23836 fp->uf_flags = flags;
23837 fp->uf_calls = 0;
23838 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840
23841erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 ga_clear_strings(&newargs);
23843 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023844ret_free:
23845 vim_free(skip_until);
23846 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023849 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850}
23851
23852/*
23853 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023854 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023857 * TFN_INT: internal function name OK
23858 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023859 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 * Advances "pp" to just after the function name (if no error).
23861 */
23862 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023863trans_function_name(
23864 char_u **pp,
23865 int skip, /* only find the end, don't evaluate */
23866 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023867 funcdict_T *fdp, /* return: info about dictionary used */
23868 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023870 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 char_u *start;
23872 char_u *end;
23873 int lead;
23874 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023876 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877
23878 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023879 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023881
23882 /* Check for hard coded <SNR>: already translated function ID (from a user
23883 * command). */
23884 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23885 && (*pp)[2] == (int)KE_SNR)
23886 {
23887 *pp += 3;
23888 len = get_id_len(pp) + 3;
23889 return vim_strnsave(start, len);
23890 }
23891
23892 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23893 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023895 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023897
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023898 /* Note that TFN_ flags use the same values as GLV_ flags. */
23899 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023900 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023901 if (end == start)
23902 {
23903 if (!skip)
23904 EMSG(_("E129: Function name required"));
23905 goto theend;
23906 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023907 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023908 {
23909 /*
23910 * Report an invalid expression in braces, unless the expression
23911 * evaluation has been cancelled due to an aborting error, an
23912 * interrupt, or an exception.
23913 */
23914 if (!aborting())
23915 {
23916 if (end != NULL)
23917 EMSG2(_(e_invarg2), start);
23918 }
23919 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023920 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023921 goto theend;
23922 }
23923
23924 if (lv.ll_tv != NULL)
23925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023926 if (fdp != NULL)
23927 {
23928 fdp->fd_dict = lv.ll_dict;
23929 fdp->fd_newkey = lv.ll_newkey;
23930 lv.ll_newkey = NULL;
23931 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023933 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23934 {
23935 name = vim_strsave(lv.ll_tv->vval.v_string);
23936 *pp = end;
23937 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023938 else if (lv.ll_tv->v_type == VAR_PARTIAL
23939 && lv.ll_tv->vval.v_partial != NULL)
23940 {
23941 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23942 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010023943 if (partial != NULL)
23944 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010023945 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023946 else
23947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023948 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23949 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023950 EMSG(_(e_funcref));
23951 else
23952 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023953 name = NULL;
23954 }
23955 goto theend;
23956 }
23957
23958 if (lv.ll_name == NULL)
23959 {
23960 /* Error found, but continue after the function name. */
23961 *pp = end;
23962 goto theend;
23963 }
23964
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023965 /* Check if the name is a Funcref. If so, use the value. */
23966 if (lv.ll_exp_name != NULL)
23967 {
23968 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023969 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023970 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023971 if (name == lv.ll_exp_name)
23972 name = NULL;
23973 }
23974 else
23975 {
23976 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023977 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023978 if (name == *pp)
23979 name = NULL;
23980 }
23981 if (name != NULL)
23982 {
23983 name = vim_strsave(name);
23984 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023985 if (STRNCMP(name, "<SNR>", 5) == 0)
23986 {
23987 /* Change "<SNR>" to the byte sequence. */
23988 name[0] = K_SPECIAL;
23989 name[1] = KS_EXTRA;
23990 name[2] = (int)KE_SNR;
23991 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23992 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023993 goto theend;
23994 }
23995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023996 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023997 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023998 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023999 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24000 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24001 {
24002 /* When there was "s:" already or the name expanded to get a
24003 * leading "s:" then remove it. */
24004 lv.ll_name += 2;
24005 len -= 2;
24006 lead = 2;
24007 }
24008 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024009 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024010 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024011 /* skip over "s:" and "g:" */
24012 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024013 lv.ll_name += 2;
24014 len = (int)(end - lv.ll_name);
24015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024016
24017 /*
24018 * Copy the function name to allocated memory.
24019 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24020 * Accept <SNR>123_name() outside a script.
24021 */
24022 if (skip)
24023 lead = 0; /* do nothing */
24024 else if (lead > 0)
24025 {
24026 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024027 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24028 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024029 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024030 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024031 if (current_SID <= 0)
24032 {
24033 EMSG(_(e_usingsid));
24034 goto theend;
24035 }
24036 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24037 lead += (int)STRLEN(sid_buf);
24038 }
24039 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024040 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024041 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024042 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024043 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024044 goto theend;
24045 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024046 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024047 {
24048 char_u *cp = vim_strchr(lv.ll_name, ':');
24049
24050 if (cp != NULL && cp < end)
24051 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024052 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024053 goto theend;
24054 }
24055 }
24056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024057 name = alloc((unsigned)(len + lead + 1));
24058 if (name != NULL)
24059 {
24060 if (lead > 0)
24061 {
24062 name[0] = K_SPECIAL;
24063 name[1] = KS_EXTRA;
24064 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024065 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024066 STRCPY(name + 3, sid_buf);
24067 }
24068 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024069 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024070 }
24071 *pp = end;
24072
24073theend:
24074 clear_lval(&lv);
24075 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076}
24077
24078/*
24079 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24080 * Return 2 if "p" starts with "s:".
24081 * Return 0 otherwise.
24082 */
24083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024084eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024086 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24087 * the standard library function. */
24088 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24089 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 return 5;
24091 if (p[0] == 's' && p[1] == ':')
24092 return 2;
24093 return 0;
24094}
24095
24096/*
24097 * Return TRUE if "p" starts with "<SID>" or "s:".
24098 * Only works if eval_fname_script() returned non-zero for "p"!
24099 */
24100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024101eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102{
24103 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24104}
24105
24106/*
24107 * List the head of the function: "name(arg1, arg2)".
24108 */
24109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024110list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111{
24112 int j;
24113
24114 msg_start();
24115 if (indent)
24116 MSG_PUTS(" ");
24117 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 {
24120 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024121 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 }
24123 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024124 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024126 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 {
24128 if (j)
24129 MSG_PUTS(", ");
24130 msg_puts(FUNCARG(fp, j));
24131 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 {
24134 if (j)
24135 MSG_PUTS(", ");
24136 MSG_PUTS("...");
24137 }
24138 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024139 if (fp->uf_flags & FC_ABORT)
24140 MSG_PUTS(" abort");
24141 if (fp->uf_flags & FC_RANGE)
24142 MSG_PUTS(" range");
24143 if (fp->uf_flags & FC_DICT)
24144 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024145 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024146 if (p_verbose > 0)
24147 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148}
24149
24150/*
24151 * Find a function by name, return pointer to it in ufuncs.
24152 * Return NULL for unknown function.
24153 */
24154 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024155find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024157 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024159 hi = hash_find(&func_hashtab, name);
24160 if (!HASHITEM_EMPTY(hi))
24161 return HI2UF(hi);
24162 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163}
24164
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024165#if defined(EXITFREE) || defined(PROTO)
24166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024167free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024168{
24169 hashitem_T *hi;
24170
24171 /* Need to start all over every time, because func_free() may change the
24172 * hash table. */
24173 while (func_hashtab.ht_used > 0)
24174 for (hi = func_hashtab.ht_array; ; ++hi)
24175 if (!HASHITEM_EMPTY(hi))
24176 {
24177 func_free(HI2UF(hi));
24178 break;
24179 }
24180}
24181#endif
24182
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024184translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024185{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024186 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024187 return find_internal_func(name) >= 0;
24188 return find_func(name) != NULL;
24189}
24190
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024191/*
24192 * Return TRUE if a function "name" exists.
24193 */
24194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024195function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024196{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024197 char_u *nm = name;
24198 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024199 int n = FALSE;
24200
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024201 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024202 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024203 nm = skipwhite(nm);
24204
24205 /* Only accept "funcname", "funcname ", "funcname (..." and
24206 * "funcname(...", not "funcname!...". */
24207 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024208 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024209 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024210 return n;
24211}
24212
Bram Moolenaara1544c02013-05-30 12:35:52 +020024213 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024215{
24216 char_u *nm = name;
24217 char_u *p;
24218
Bram Moolenaar65639032016-03-16 21:40:30 +010024219 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024220
24221 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024222 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024223 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024224
Bram Moolenaara1544c02013-05-30 12:35:52 +020024225 vim_free(p);
24226 return NULL;
24227}
24228
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024229/*
24230 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024231 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24232 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024233 */
24234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024235builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024236{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024237 char_u *p;
24238
24239 if (!ASCII_ISLOWER(name[0]))
24240 return FALSE;
24241 p = vim_strchr(name, AUTOLOAD_CHAR);
24242 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024243}
24244
Bram Moolenaar05159a02005-02-26 23:04:13 +000024245#if defined(FEAT_PROFILE) || defined(PROTO)
24246/*
24247 * Start profiling function "fp".
24248 */
24249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024250func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024251{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024252 int len = fp->uf_lines.ga_len;
24253
24254 if (len == 0)
24255 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024256 fp->uf_tm_count = 0;
24257 profile_zero(&fp->uf_tm_self);
24258 profile_zero(&fp->uf_tm_total);
24259 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024260 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024261 if (fp->uf_tml_total == NULL)
24262 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024263 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024264 if (fp->uf_tml_self == NULL)
24265 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024266 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024267 fp->uf_tml_idx = -1;
24268 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24269 || fp->uf_tml_self == NULL)
24270 return; /* out of memory */
24271
24272 fp->uf_profiling = TRUE;
24273}
24274
24275/*
24276 * Dump the profiling results for all functions in file "fd".
24277 */
24278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024279func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024280{
24281 hashitem_T *hi;
24282 int todo;
24283 ufunc_T *fp;
24284 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024285 ufunc_T **sorttab;
24286 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024288 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024289 if (todo == 0)
24290 return; /* nothing to dump */
24291
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024292 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024293
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24295 {
24296 if (!HASHITEM_EMPTY(hi))
24297 {
24298 --todo;
24299 fp = HI2UF(hi);
24300 if (fp->uf_profiling)
24301 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024302 if (sorttab != NULL)
24303 sorttab[st_len++] = fp;
24304
Bram Moolenaar05159a02005-02-26 23:04:13 +000024305 if (fp->uf_name[0] == K_SPECIAL)
24306 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24307 else
24308 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24309 if (fp->uf_tm_count == 1)
24310 fprintf(fd, "Called 1 time\n");
24311 else
24312 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24313 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24314 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24315 fprintf(fd, "\n");
24316 fprintf(fd, "count total (s) self (s)\n");
24317
24318 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24319 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024320 if (FUNCLINE(fp, i) == NULL)
24321 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024322 prof_func_line(fd, fp->uf_tml_count[i],
24323 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024324 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24325 }
24326 fprintf(fd, "\n");
24327 }
24328 }
24329 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024330
24331 if (sorttab != NULL && st_len > 0)
24332 {
24333 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24334 prof_total_cmp);
24335 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24336 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24337 prof_self_cmp);
24338 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24339 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024340
24341 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342}
Bram Moolenaar73830342005-02-28 22:48:19 +000024343
24344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024345prof_sort_list(
24346 FILE *fd,
24347 ufunc_T **sorttab,
24348 int st_len,
24349 char *title,
24350 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024351{
24352 int i;
24353 ufunc_T *fp;
24354
24355 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24356 fprintf(fd, "count total (s) self (s) function\n");
24357 for (i = 0; i < 20 && i < st_len; ++i)
24358 {
24359 fp = sorttab[i];
24360 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24361 prefer_self);
24362 if (fp->uf_name[0] == K_SPECIAL)
24363 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24364 else
24365 fprintf(fd, " %s()\n", fp->uf_name);
24366 }
24367 fprintf(fd, "\n");
24368}
24369
24370/*
24371 * Print the count and times for one function or function line.
24372 */
24373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024374prof_func_line(
24375 FILE *fd,
24376 int count,
24377 proftime_T *total,
24378 proftime_T *self,
24379 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024380{
24381 if (count > 0)
24382 {
24383 fprintf(fd, "%5d ", count);
24384 if (prefer_self && profile_equal(total, self))
24385 fprintf(fd, " ");
24386 else
24387 fprintf(fd, "%s ", profile_msg(total));
24388 if (!prefer_self && profile_equal(total, self))
24389 fprintf(fd, " ");
24390 else
24391 fprintf(fd, "%s ", profile_msg(self));
24392 }
24393 else
24394 fprintf(fd, " ");
24395}
24396
24397/*
24398 * Compare function for total time sorting.
24399 */
24400 static int
24401#ifdef __BORLANDC__
24402_RTLENTRYF
24403#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024404prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024405{
24406 ufunc_T *p1, *p2;
24407
24408 p1 = *(ufunc_T **)s1;
24409 p2 = *(ufunc_T **)s2;
24410 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24411}
24412
24413/*
24414 * Compare function for self time sorting.
24415 */
24416 static int
24417#ifdef __BORLANDC__
24418_RTLENTRYF
24419#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024420prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024421{
24422 ufunc_T *p1, *p2;
24423
24424 p1 = *(ufunc_T **)s1;
24425 p2 = *(ufunc_T **)s2;
24426 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24427}
24428
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429#endif
24430
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024431/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024433 * Return TRUE if a package was loaded.
24434 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024436script_autoload(
24437 char_u *name,
24438 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024439{
24440 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024441 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024442 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024443 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024445 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024446 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024447 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024448 return FALSE;
24449
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024450 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024451
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024452 /* Find the name in the list of previously loaded package names. Skip
24453 * "autoload/", it's always the same. */
24454 for (i = 0; i < ga_loaded.ga_len; ++i)
24455 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24456 break;
24457 if (!reload && i < ga_loaded.ga_len)
24458 ret = FALSE; /* was loaded already */
24459 else
24460 {
24461 /* Remember the name if it wasn't loaded already. */
24462 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24463 {
24464 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24465 tofree = NULL;
24466 }
24467
24468 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024469 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024470 ret = TRUE;
24471 }
24472
24473 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024474 return ret;
24475}
24476
24477/*
24478 * Return the autoload script name for a function or variable name.
24479 * Returns NULL when out of memory.
24480 */
24481 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024482autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024483{
24484 char_u *p;
24485 char_u *scriptname;
24486
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024487 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024488 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24489 if (scriptname == NULL)
24490 return FALSE;
24491 STRCPY(scriptname, "autoload/");
24492 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024493 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024494 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024495 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024496 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024497 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024498}
24499
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24501
24502/*
24503 * Function given to ExpandGeneric() to obtain the list of user defined
24504 * function names.
24505 */
24506 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024507get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024509 static long_u done;
24510 static hashitem_T *hi;
24511 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512
24513 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024515 done = 0;
24516 hi = func_hashtab.ht_array;
24517 }
24518 if (done < func_hashtab.ht_used)
24519 {
24520 if (done++ > 0)
24521 ++hi;
24522 while (HASHITEM_EMPTY(hi))
24523 ++hi;
24524 fp = HI2UF(hi);
24525
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024526 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024527 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024528
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024529 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24530 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531
24532 cat_func_name(IObuff, fp);
24533 if (xp->xp_context != EXPAND_USER_FUNC)
24534 {
24535 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024536 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 STRCAT(IObuff, ")");
24538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024539 return IObuff;
24540 }
24541 return NULL;
24542}
24543
24544#endif /* FEAT_CMDL_COMPL */
24545
24546/*
24547 * Copy the function name of "fp" to buffer "buf".
24548 * "buf" must be able to hold the function name plus three bytes.
24549 * Takes care of script-local function names.
24550 */
24551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024552cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 {
24556 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024557 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 }
24559 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024560 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561}
24562
24563/*
24564 * ":delfunction {name}"
24565 */
24566 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024567ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024568{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024569 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 char_u *p;
24571 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024572 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573
24574 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024575 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024577 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024578 {
24579 if (fudi.fd_dict != NULL && !eap->skip)
24580 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024581 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024583 if (!ends_excmd(*skipwhite(p)))
24584 {
24585 vim_free(name);
24586 EMSG(_(e_trailing));
24587 return;
24588 }
24589 eap->nextcmd = check_nextcmd(p);
24590 if (eap->nextcmd != NULL)
24591 *p = NUL;
24592
24593 if (!eap->skip)
24594 fp = find_func(name);
24595 vim_free(name);
24596
24597 if (!eap->skip)
24598 {
24599 if (fp == NULL)
24600 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024601 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 return;
24603 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024604 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605 {
24606 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24607 return;
24608 }
24609
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024610 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024612 /* Delete the dict item that refers to the function, it will
24613 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024614 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024616 else
24617 func_free(fp);
24618 }
24619}
24620
24621/*
24622 * Free a function and remove it from the list of functions.
24623 */
24624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024625func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024626{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024627 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024628
24629 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024630 ga_clear_strings(&(fp->uf_args));
24631 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024632#ifdef FEAT_PROFILE
24633 vim_free(fp->uf_tml_count);
24634 vim_free(fp->uf_tml_total);
24635 vim_free(fp->uf_tml_self);
24636#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024637
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024638 /* remove the function from the function hashtable */
24639 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24640 if (HASHITEM_EMPTY(hi))
24641 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643 hash_remove(&func_hashtab, hi);
24644
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024645 vim_free(fp);
24646}
24647
24648/*
24649 * Unreference a Function: decrement the reference count and free it when it
24650 * becomes zero. Only for numbered functions.
24651 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024653func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024654{
24655 ufunc_T *fp;
24656
24657 if (name != NULL && isdigit(*name))
24658 {
24659 fp = find_func(name);
24660 if (fp == NULL)
24661 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024662 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024663 {
24664 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024665 * when "uf_calls" becomes zero. */
24666 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024667 func_free(fp);
24668 }
24669 }
24670}
24671
24672/*
24673 * Count a reference to a Function.
24674 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024676func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024677{
24678 ufunc_T *fp;
24679
24680 if (name != NULL && isdigit(*name))
24681 {
24682 fp = find_func(name);
24683 if (fp == NULL)
24684 EMSG2(_(e_intern2), "func_ref()");
24685 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024686 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687 }
24688}
24689
24690/*
24691 * Call a user function.
24692 */
24693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024694call_user_func(
24695 ufunc_T *fp, /* pointer to function */
24696 int argcount, /* nr of args */
24697 typval_T *argvars, /* arguments */
24698 typval_T *rettv, /* return value */
24699 linenr_T firstline, /* first line of range */
24700 linenr_T lastline, /* last line of range */
24701 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702{
Bram Moolenaar33570922005-01-25 22:26:29 +000024703 char_u *save_sourcing_name;
24704 linenr_T save_sourcing_lnum;
24705 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024706 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024707 int save_did_emsg;
24708 static int depth = 0;
24709 dictitem_T *v;
24710 int fixvar_idx = 0; /* index in fixvar[] */
24711 int i;
24712 int ai;
24713 char_u numbuf[NUMBUFLEN];
24714 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024715 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024716#ifdef FEAT_PROFILE
24717 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024718 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720
24721 /* If depth of calling is getting too high, don't execute the function */
24722 if (depth >= p_mfd)
24723 {
24724 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024725 rettv->v_type = VAR_NUMBER;
24726 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 return;
24728 }
24729 ++depth;
24730
24731 line_breakcheck(); /* check for CTRL-C hit */
24732
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024733 fc = (funccall_T *)alloc(sizeof(funccall_T));
24734 fc->caller = current_funccal;
24735 current_funccal = fc;
24736 fc->func = fp;
24737 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024738 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024739 fc->linenr = 0;
24740 fc->returned = FALSE;
24741 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024743 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24744 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745
Bram Moolenaar33570922005-01-25 22:26:29 +000024746 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024747 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024748 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24749 * each argument variable and saves a lot of time.
24750 */
24751 /*
24752 * Init l: variables.
24753 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024754 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024755 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024756 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024757 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24758 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024759 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024760 name = v->di_key;
24761 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024762 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024763 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024764 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024765 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024766 v->di_tv.vval.v_dict = selfdict;
24767 ++selfdict->dv_refcount;
24768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024769
Bram Moolenaar33570922005-01-25 22:26:29 +000024770 /*
24771 * Init a: variables.
24772 * Set a:0 to "argcount".
24773 * Set a:000 to a list with room for the "..." arguments.
24774 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024775 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024776 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024777 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024778 /* Use "name" to avoid a warning from some compiler that checks the
24779 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024780 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024781 name = v->di_key;
24782 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024783 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024784 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024785 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024786 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024787 v->di_tv.vval.v_list = &fc->l_varlist;
24788 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24789 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24790 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024791
24792 /*
24793 * Set a:firstline to "firstline" and a:lastline to "lastline".
24794 * Set a:name to named arguments.
24795 * Set a:N to the "..." arguments.
24796 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024797 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024798 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024799 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024800 (varnumber_T)lastline);
24801 for (i = 0; i < argcount; ++i)
24802 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024803 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024804 if (ai < 0)
24805 /* named argument a:name */
24806 name = FUNCARG(fp, i);
24807 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024808 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024809 /* "..." argument a:1, a:2, etc. */
24810 sprintf((char *)numbuf, "%d", ai + 1);
24811 name = numbuf;
24812 }
24813 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24814 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024815 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024816 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24817 }
24818 else
24819 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024820 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24821 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024822 if (v == NULL)
24823 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024824 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024825 }
24826 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024827 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024828
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024829 /* Note: the values are copied directly to avoid alloc/free.
24830 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024831 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024832 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024833
24834 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24835 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024836 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24837 fc->l_listitems[ai].li_tv = argvars[i];
24838 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024839 }
24840 }
24841
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 /* Don't redraw while executing the function. */
24843 ++RedrawingDisabled;
24844 save_sourcing_name = sourcing_name;
24845 save_sourcing_lnum = sourcing_lnum;
24846 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024847 /* need space for function name + ("function " + 3) or "[number]" */
24848 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24849 + STRLEN(fp->uf_name) + 20;
24850 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 if (sourcing_name != NULL)
24852 {
24853 if (save_sourcing_name != NULL
24854 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024855 sprintf((char *)sourcing_name, "%s[%d]..",
24856 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857 else
24858 STRCPY(sourcing_name, "function ");
24859 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24860
24861 if (p_verbose >= 12)
24862 {
24863 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024864 verbose_enter_scroll();
24865
Bram Moolenaar555b2802005-05-19 21:08:39 +000024866 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867 if (p_verbose >= 14)
24868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024870 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024871 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024872 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873
24874 msg_puts((char_u *)"(");
24875 for (i = 0; i < argcount; ++i)
24876 {
24877 if (i > 0)
24878 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024879 if (argvars[i].v_type == VAR_NUMBER)
24880 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 else
24882 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024883 /* Do not want errors such as E724 here. */
24884 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024885 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024886 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024887 if (s != NULL)
24888 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024889 if (vim_strsize(s) > MSG_BUF_CLEN)
24890 {
24891 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24892 s = buf;
24893 }
24894 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024895 vim_free(tofree);
24896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 }
24898 }
24899 msg_puts((char_u *)")");
24900 }
24901 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024902
24903 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 --no_wait_return;
24905 }
24906 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024907#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024908 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024909 {
24910 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24911 func_do_profile(fp);
24912 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024913 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024914 {
24915 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024916 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024917 profile_zero(&fp->uf_tm_children);
24918 }
24919 script_prof_save(&wait_start);
24920 }
24921#endif
24922
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024924 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 save_did_emsg = did_emsg;
24926 did_emsg = FALSE;
24927
24928 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024929 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24931
24932 --RedrawingDisabled;
24933
24934 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024935 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024937 clear_tv(rettv);
24938 rettv->v_type = VAR_NUMBER;
24939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 }
24941
Bram Moolenaar05159a02005-02-26 23:04:13 +000024942#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024943 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024944 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024945 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024946 profile_end(&call_start);
24947 profile_sub_wait(&wait_start, &call_start);
24948 profile_add(&fp->uf_tm_total, &call_start);
24949 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024950 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024952 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24953 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024954 }
24955 }
24956#endif
24957
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 /* when being verbose, mention the return value */
24959 if (p_verbose >= 12)
24960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024962 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024965 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024966 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024967 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024968 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024969 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024971 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024972 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024973 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024974 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024975
Bram Moolenaar555b2802005-05-19 21:08:39 +000024976 /* The value may be very long. Skip the middle part, so that we
24977 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024978 * truncate it at the end. Don't want errors such as E724 here. */
24979 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024980 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024981 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024982 if (s != NULL)
24983 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024984 if (vim_strsize(s) > MSG_BUF_CLEN)
24985 {
24986 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24987 s = buf;
24988 }
24989 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024990 vim_free(tofree);
24991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 }
24993 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024994
24995 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996 --no_wait_return;
24997 }
24998
24999 vim_free(sourcing_name);
25000 sourcing_name = save_sourcing_name;
25001 sourcing_lnum = save_sourcing_lnum;
25002 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025003#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025004 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025005 script_prof_restore(&wait_start);
25006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007
25008 if (p_verbose >= 12 && sourcing_name != NULL)
25009 {
25010 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025011 verbose_enter_scroll();
25012
Bram Moolenaar555b2802005-05-19 21:08:39 +000025013 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025015
25016 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 --no_wait_return;
25018 }
25019
25020 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025021 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025023
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025024 /* If the a:000 list and the l: and a: dicts are not referenced we can
25025 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025026 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25027 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25028 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25029 {
25030 free_funccal(fc, FALSE);
25031 }
25032 else
25033 {
25034 hashitem_T *hi;
25035 listitem_T *li;
25036 int todo;
25037
25038 /* "fc" is still in use. This can happen when returning "a:000" or
25039 * assigning "l:" to a global variable.
25040 * Link "fc" in the list for garbage collection later. */
25041 fc->caller = previous_funccal;
25042 previous_funccal = fc;
25043
25044 /* Make a copy of the a: variables, since we didn't do that above. */
25045 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25046 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25047 {
25048 if (!HASHITEM_EMPTY(hi))
25049 {
25050 --todo;
25051 v = HI2DI(hi);
25052 copy_tv(&v->di_tv, &v->di_tv);
25053 }
25054 }
25055
25056 /* Make a copy of the a:000 items, since we didn't do that above. */
25057 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25058 copy_tv(&li->li_tv, &li->li_tv);
25059 }
25060}
25061
25062/*
25063 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025064 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025065 */
25066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025067can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025068{
25069 return (fc->l_varlist.lv_copyID != copyID
25070 && fc->l_vars.dv_copyID != copyID
25071 && fc->l_avars.dv_copyID != copyID);
25072}
25073
25074/*
25075 * Free "fc" and what it contains.
25076 */
25077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025078free_funccal(
25079 funccall_T *fc,
25080 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025081{
25082 listitem_T *li;
25083
25084 /* The a: variables typevals may not have been allocated, only free the
25085 * allocated variables. */
25086 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25087
25088 /* free all l: variables */
25089 vars_clear(&fc->l_vars.dv_hashtab);
25090
25091 /* Free the a:000 variables if they were allocated. */
25092 if (free_val)
25093 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25094 clear_tv(&li->li_tv);
25095
25096 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097}
25098
25099/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025100 * Add a number variable "name" to dict "dp" with value "nr".
25101 */
25102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025103add_nr_var(
25104 dict_T *dp,
25105 dictitem_T *v,
25106 char *name,
25107 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025108{
25109 STRCPY(v->di_key, name);
25110 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25111 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25112 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025113 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025114 v->di_tv.vval.v_number = nr;
25115}
25116
25117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025118 * ":return [expr]"
25119 */
25120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025121ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122{
25123 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025124 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 int returning = FALSE;
25126
25127 if (current_funccal == NULL)
25128 {
25129 EMSG(_("E133: :return not inside a function"));
25130 return;
25131 }
25132
25133 if (eap->skip)
25134 ++emsg_skip;
25135
25136 eap->nextcmd = NULL;
25137 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025138 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 {
25140 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025141 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025143 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 }
25145 /* It's safer to return also on error. */
25146 else if (!eap->skip)
25147 {
25148 /*
25149 * Return unless the expression evaluation has been cancelled due to an
25150 * aborting error, an interrupt, or an exception.
25151 */
25152 if (!aborting())
25153 returning = do_return(eap, FALSE, TRUE, NULL);
25154 }
25155
25156 /* When skipping or the return gets pending, advance to the next command
25157 * in this line (!returning). Otherwise, ignore the rest of the line.
25158 * Following lines will be ignored by get_func_line(). */
25159 if (returning)
25160 eap->nextcmd = NULL;
25161 else if (eap->nextcmd == NULL) /* no argument */
25162 eap->nextcmd = check_nextcmd(arg);
25163
25164 if (eap->skip)
25165 --emsg_skip;
25166}
25167
25168/*
25169 * Return from a function. Possibly makes the return pending. Also called
25170 * for a pending return at the ":endtry" or after returning from an extra
25171 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025172 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025173 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174 * FALSE when the return gets pending.
25175 */
25176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025177do_return(
25178 exarg_T *eap,
25179 int reanimate,
25180 int is_cmd,
25181 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182{
25183 int idx;
25184 struct condstack *cstack = eap->cstack;
25185
25186 if (reanimate)
25187 /* Undo the return. */
25188 current_funccal->returned = FALSE;
25189
25190 /*
25191 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25192 * not in its finally clause (which then is to be executed next) is found.
25193 * In this case, make the ":return" pending for execution at the ":endtry".
25194 * Otherwise, return normally.
25195 */
25196 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25197 if (idx >= 0)
25198 {
25199 cstack->cs_pending[idx] = CSTP_RETURN;
25200
25201 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025202 /* A pending return again gets pending. "rettv" points to an
25203 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025205 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 else
25207 {
25208 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025209 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025211 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025213 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 {
25215 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025216 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025217 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218 else
25219 EMSG(_(e_outofmem));
25220 }
25221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025222 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223
25224 if (reanimate)
25225 {
25226 /* The pending return value could be overwritten by a ":return"
25227 * without argument in a finally clause; reset the default
25228 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025229 current_funccal->rettv->v_type = VAR_NUMBER;
25230 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 }
25232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025233 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 }
25235 else
25236 {
25237 current_funccal->returned = TRUE;
25238
25239 /* If the return is carried out now, store the return value. For
25240 * a return immediately after reanimation, the value is already
25241 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025242 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025244 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025245 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025247 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 }
25249 }
25250
25251 return idx < 0;
25252}
25253
25254/*
25255 * Free the variable with a pending return value.
25256 */
25257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025258discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259{
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261}
25262
25263/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025264 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 * is an allocated string. Used by report_pending() for verbose messages.
25266 */
25267 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025268get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025270 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025272 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025274 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025275 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025276 if (s == NULL)
25277 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025278
25279 STRCPY(IObuff, ":return ");
25280 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25281 if (STRLEN(s) + 8 >= IOSIZE)
25282 STRCPY(IObuff + IOSIZE - 4, "...");
25283 vim_free(tofree);
25284 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285}
25286
25287/*
25288 * Get next function line.
25289 * Called by do_cmdline() to get the next line.
25290 * Returns allocated string, or NULL for end of function.
25291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025293get_func_line(
25294 int c UNUSED,
25295 void *cookie,
25296 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297{
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025299 ufunc_T *fp = fcp->func;
25300 char_u *retval;
25301 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302
25303 /* If breakpoints have been added/deleted need to check for it. */
25304 if (fcp->dbg_tick != debug_tick)
25305 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025306 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025307 sourcing_lnum);
25308 fcp->dbg_tick = debug_tick;
25309 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025310#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025311 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025312 func_line_end(cookie);
25313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314
Bram Moolenaar05159a02005-02-26 23:04:13 +000025315 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025316 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25317 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 retval = NULL;
25319 else
25320 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025321 /* Skip NULL lines (continuation lines). */
25322 while (fcp->linenr < gap->ga_len
25323 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25324 ++fcp->linenr;
25325 if (fcp->linenr >= gap->ga_len)
25326 retval = NULL;
25327 else
25328 {
25329 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25330 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025331#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025332 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025333 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025334#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 }
25337
25338 /* Did we encounter a breakpoint? */
25339 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25340 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025341 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025343 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 sourcing_lnum);
25345 fcp->dbg_tick = debug_tick;
25346 }
25347
25348 return retval;
25349}
25350
Bram Moolenaar05159a02005-02-26 23:04:13 +000025351#if defined(FEAT_PROFILE) || defined(PROTO)
25352/*
25353 * Called when starting to read a function line.
25354 * "sourcing_lnum" must be correct!
25355 * When skipping lines it may not actually be executed, but we won't find out
25356 * until later and we need to store the time now.
25357 */
25358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025359func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360{
25361 funccall_T *fcp = (funccall_T *)cookie;
25362 ufunc_T *fp = fcp->func;
25363
25364 if (fp->uf_profiling && sourcing_lnum >= 1
25365 && sourcing_lnum <= fp->uf_lines.ga_len)
25366 {
25367 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025368 /* Skip continuation lines. */
25369 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25370 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025371 fp->uf_tml_execed = FALSE;
25372 profile_start(&fp->uf_tml_start);
25373 profile_zero(&fp->uf_tml_children);
25374 profile_get_wait(&fp->uf_tml_wait);
25375 }
25376}
25377
25378/*
25379 * Called when actually executing a function line.
25380 */
25381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025382func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025383{
25384 funccall_T *fcp = (funccall_T *)cookie;
25385 ufunc_T *fp = fcp->func;
25386
25387 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25388 fp->uf_tml_execed = TRUE;
25389}
25390
25391/*
25392 * Called when done with a function line.
25393 */
25394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025395func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025396{
25397 funccall_T *fcp = (funccall_T *)cookie;
25398 ufunc_T *fp = fcp->func;
25399
25400 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25401 {
25402 if (fp->uf_tml_execed)
25403 {
25404 ++fp->uf_tml_count[fp->uf_tml_idx];
25405 profile_end(&fp->uf_tml_start);
25406 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025407 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025408 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25409 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025410 }
25411 fp->uf_tml_idx = -1;
25412 }
25413}
25414#endif
25415
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416/*
25417 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025418 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 */
25420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025421func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422{
Bram Moolenaar33570922005-01-25 22:26:29 +000025423 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424
25425 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25426 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025427 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 || fcp->returned);
25429}
25430
25431/*
25432 * return TRUE if cookie indicates a function which "abort"s on errors.
25433 */
25434 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025435func_has_abort(
25436 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025438 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439}
25440
25441#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25442typedef enum
25443{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025444 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25445 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25446 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447} var_flavour_T;
25448
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025449static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450
25451 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025452var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453{
25454 char_u *p = varname;
25455
25456 if (ASCII_ISUPPER(*p))
25457 {
25458 while (*(++p))
25459 if (ASCII_ISLOWER(*p))
25460 return VAR_FLAVOUR_SESSION;
25461 return VAR_FLAVOUR_VIMINFO;
25462 }
25463 else
25464 return VAR_FLAVOUR_DEFAULT;
25465}
25466#endif
25467
25468#if defined(FEAT_VIMINFO) || defined(PROTO)
25469/*
25470 * Restore global vars that start with a capital from the viminfo file
25471 */
25472 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025473read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474{
25475 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025476 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025477 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025478 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479
25480 if (!writing && (find_viminfo_parameter('!') != NULL))
25481 {
25482 tab = vim_strchr(virp->vir_line + 1, '\t');
25483 if (tab != NULL)
25484 {
25485 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025486 switch (*tab)
25487 {
25488 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025489#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025490 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025491#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025492 case 'D': type = VAR_DICT; break;
25493 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025494 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496
25497 tab = vim_strchr(tab, '\t');
25498 if (tab != NULL)
25499 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025500 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025501 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025502 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025504#ifdef FEAT_FLOAT
25505 else if (type == VAR_FLOAT)
25506 (void)string2float(tab + 1, &tv.vval.v_float);
25507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025509 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025510 if (type == VAR_DICT || type == VAR_LIST)
25511 {
25512 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25513
25514 if (etv == NULL)
25515 /* Failed to parse back the dict or list, use it as a
25516 * string. */
25517 tv.v_type = VAR_STRING;
25518 else
25519 {
25520 vim_free(tv.vval.v_string);
25521 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025522 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025523 }
25524 }
25525
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025526 /* when in a function use global variables */
25527 save_funccal = current_funccal;
25528 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025529 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025530 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025531
25532 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025533 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025534 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25535 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 }
25537 }
25538 }
25539
25540 return viminfo_readline(virp);
25541}
25542
25543/*
25544 * Write global vars that start with a capital to the viminfo file
25545 */
25546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025547write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548{
Bram Moolenaar33570922005-01-25 22:26:29 +000025549 hashitem_T *hi;
25550 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025551 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025552 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025553 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025554 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556
25557 if (find_viminfo_parameter('!') == NULL)
25558 return;
25559
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025560 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025561
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025562 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025563 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025565 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025567 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025568 this_var = HI2DI(hi);
25569 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025570 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025571 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025572 {
25573 case VAR_STRING: s = "STR"; break;
25574 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025575 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025576 case VAR_DICT: s = "DIC"; break;
25577 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025578 case VAR_SPECIAL: s = "XPL"; break;
25579
25580 case VAR_UNKNOWN:
25581 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025582 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025584 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025585 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025586 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025587 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025588 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025589 if (p != NULL)
25590 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025591 vim_free(tofree);
25592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 }
25594 }
25595}
25596#endif
25597
25598#if defined(FEAT_SESSION) || defined(PROTO)
25599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025600store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601{
Bram Moolenaar33570922005-01-25 22:26:29 +000025602 hashitem_T *hi;
25603 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025604 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605 char_u *p, *t;
25606
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025607 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025608 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025610 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025612 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025613 this_var = HI2DI(hi);
25614 if ((this_var->di_tv.v_type == VAR_NUMBER
25615 || this_var->di_tv.v_type == VAR_STRING)
25616 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025617 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025618 /* Escape special characters with a backslash. Turn a LF and
25619 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025620 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025621 (char_u *)"\\\"\n\r");
25622 if (p == NULL) /* out of memory */
25623 break;
25624 for (t = p; *t != NUL; ++t)
25625 if (*t == '\n')
25626 *t = 'n';
25627 else if (*t == '\r')
25628 *t = 'r';
25629 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025630 this_var->di_key,
25631 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25632 : ' ',
25633 p,
25634 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25635 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025636 || put_eol(fd) == FAIL)
25637 {
25638 vim_free(p);
25639 return FAIL;
25640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 vim_free(p);
25642 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025643#ifdef FEAT_FLOAT
25644 else if (this_var->di_tv.v_type == VAR_FLOAT
25645 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25646 {
25647 float_T f = this_var->di_tv.vval.v_float;
25648 int sign = ' ';
25649
25650 if (f < 0)
25651 {
25652 f = -f;
25653 sign = '-';
25654 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025655 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025656 this_var->di_key, sign, f) < 0)
25657 || put_eol(fd) == FAIL)
25658 return FAIL;
25659 }
25660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 }
25662 }
25663 return OK;
25664}
25665#endif
25666
Bram Moolenaar661b1822005-07-28 22:36:45 +000025667/*
25668 * Display script name where an item was last set.
25669 * Should only be invoked when 'verbose' is non-zero.
25670 */
25671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025672last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025673{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025674 char_u *p;
25675
Bram Moolenaar661b1822005-07-28 22:36:45 +000025676 if (scriptID != 0)
25677 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025678 p = home_replace_save(NULL, get_scriptname(scriptID));
25679 if (p != NULL)
25680 {
25681 verbose_enter();
25682 MSG_PUTS(_("\n\tLast set from "));
25683 MSG_PUTS(p);
25684 vim_free(p);
25685 verbose_leave();
25686 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025687 }
25688}
25689
Bram Moolenaard812df62008-11-09 12:46:09 +000025690/*
25691 * List v:oldfiles in a nice way.
25692 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025694ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025695{
25696 list_T *l = vimvars[VV_OLDFILES].vv_list;
25697 listitem_T *li;
25698 int nr = 0;
25699
25700 if (l == NULL)
25701 msg((char_u *)_("No old files"));
25702 else
25703 {
25704 msg_start();
25705 msg_scroll = TRUE;
25706 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25707 {
25708 msg_outnum((long)++nr);
25709 MSG_PUTS(": ");
25710 msg_outtrans(get_tv_string(&li->li_tv));
25711 msg_putchar('\n');
25712 out_flush(); /* output one line at a time */
25713 ui_breakcheck();
25714 }
25715 /* Assume "got_int" was set to truncate the listing. */
25716 got_int = FALSE;
25717
25718#ifdef FEAT_BROWSE_CMD
25719 if (cmdmod.browse)
25720 {
25721 quit_more = FALSE;
25722 nr = prompt_for_number(FALSE);
25723 msg_starthere();
25724 if (nr > 0)
25725 {
25726 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25727 (long)nr);
25728
25729 if (p != NULL)
25730 {
25731 p = expand_env_save(p);
25732 eap->arg = p;
25733 eap->cmdidx = CMD_edit;
25734 cmdmod.browse = FALSE;
25735 do_exedit(eap, NULL);
25736 vim_free(p);
25737 }
25738 }
25739 }
25740#endif
25741 }
25742}
25743
Bram Moolenaar53744302015-07-17 17:38:22 +020025744/* reset v:option_new, v:option_old and v:option_type */
25745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025746reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025747{
25748 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25749 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25750 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25751}
25752
25753
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754#endif /* FEAT_EVAL */
25755
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025757#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758
25759#ifdef WIN3264
25760/*
25761 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25762 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025763static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25764static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25765static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766
25767/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025768 * Get the short path (8.3) for the filename in "fnamep".
25769 * Only works for a valid file name.
25770 * When the path gets longer "fnamep" is changed and the allocated buffer
25771 * is put in "bufp".
25772 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25773 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 */
25775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025776get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025778 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 char_u *newbuf;
25780
25781 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025782 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 if (l > len - 1)
25784 {
25785 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025786 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 newbuf = vim_strnsave(*fnamep, l);
25788 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790
25791 vim_free(*bufp);
25792 *fnamep = *bufp = newbuf;
25793
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025794 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025795 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 }
25797
25798 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025799 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800}
25801
25802/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025803 * Get the short path (8.3) for the filename in "fname". The converted
25804 * path is returned in "bufp".
25805 *
25806 * Some of the directories specified in "fname" may not exist. This function
25807 * will shorten the existing directories at the beginning of the path and then
25808 * append the remaining non-existing path.
25809 *
25810 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025811 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025812 * bufp - Pointer to an allocated buffer for the filename.
25813 * fnamelen - Length of the filename pointed to by fname
25814 *
25815 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816 */
25817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025818shortpath_for_invalid_fname(
25819 char_u **fname,
25820 char_u **bufp,
25821 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025823 char_u *short_fname, *save_fname, *pbuf_unused;
25824 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025826 int old_len, len;
25827 int new_len, sfx_len;
25828 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829
25830 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025831 old_len = *fnamelen;
25832 save_fname = vim_strnsave(*fname, old_len);
25833 pbuf_unused = NULL;
25834 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025836 endp = save_fname + old_len - 1; /* Find the end of the copy */
25837 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025839 /*
25840 * Try shortening the supplied path till it succeeds by removing one
25841 * directory at a time from the tail of the path.
25842 */
25843 len = 0;
25844 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025846 /* go back one path-separator */
25847 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25848 --endp;
25849 if (endp <= save_fname)
25850 break; /* processed the complete path */
25851
25852 /*
25853 * Replace the path separator with a NUL and try to shorten the
25854 * resulting path.
25855 */
25856 ch = *endp;
25857 *endp = 0;
25858 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025859 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025860 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25861 {
25862 retval = FAIL;
25863 goto theend;
25864 }
25865 *endp = ch; /* preserve the string */
25866
25867 if (len > 0)
25868 break; /* successfully shortened the path */
25869
25870 /* failed to shorten the path. Skip the path separator */
25871 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 }
25873
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025874 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025876 /*
25877 * Succeeded in shortening the path. Now concatenate the shortened
25878 * path with the remaining path at the tail.
25879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025881 /* Compute the length of the new path. */
25882 sfx_len = (int)(save_endp - endp) + 1;
25883 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025887 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025889 /* There is not enough space in the currently allocated string,
25890 * copy it to a buffer big enough. */
25891 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025893 {
25894 retval = FAIL;
25895 goto theend;
25896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897 }
25898 else
25899 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025900 /* Transfer short_fname to the main buffer (it's big enough),
25901 * unless get_short_pathname() did its work in-place. */
25902 *fname = *bufp = save_fname;
25903 if (short_fname != save_fname)
25904 vim_strncpy(save_fname, short_fname, len);
25905 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025907
25908 /* concat the not-shortened part of the path */
25909 vim_strncpy(*fname + len, endp, sfx_len);
25910 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025912
25913theend:
25914 vim_free(pbuf_unused);
25915 vim_free(save_fname);
25916
25917 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918}
25919
25920/*
25921 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025922 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025923 */
25924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025925shortpath_for_partial(
25926 char_u **fnamep,
25927 char_u **bufp,
25928 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929{
25930 int sepcount, len, tflen;
25931 char_u *p;
25932 char_u *pbuf, *tfname;
25933 int hasTilde;
25934
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025935 /* Count up the path separators from the RHS.. so we know which part
25936 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025938 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 if (vim_ispathsep(*p))
25940 ++sepcount;
25941
25942 /* Need full path first (use expand_env() to remove a "~/") */
25943 hasTilde = (**fnamep == '~');
25944 if (hasTilde)
25945 pbuf = tfname = expand_env_save(*fnamep);
25946 else
25947 pbuf = tfname = FullName_save(*fnamep, FALSE);
25948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025949 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953
25954 if (len == 0)
25955 {
25956 /* Don't have a valid filename, so shorten the rest of the
25957 * path if we can. This CAN give us invalid 8.3 filenames, but
25958 * there's not a lot of point in guessing what it might be.
25959 */
25960 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025961 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963 }
25964
25965 /* Count the paths backward to find the beginning of the desired string. */
25966 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025967 {
25968#ifdef FEAT_MBYTE
25969 if (has_mbyte)
25970 p -= mb_head_off(tfname, p);
25971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972 if (vim_ispathsep(*p))
25973 {
25974 if (sepcount == 0 || (hasTilde && sepcount == 1))
25975 break;
25976 else
25977 sepcount --;
25978 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 if (hasTilde)
25981 {
25982 --p;
25983 if (p >= tfname)
25984 *p = '~';
25985 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 }
25988 else
25989 ++p;
25990
25991 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25992 vim_free(*bufp);
25993 *fnamelen = (int)STRLEN(p);
25994 *bufp = pbuf;
25995 *fnamep = p;
25996
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025997 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998}
25999#endif /* WIN3264 */
26000
26001/*
26002 * Adjust a filename, according to a string of modifiers.
26003 * *fnamep must be NUL terminated when called. When returning, the length is
26004 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026005 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 * When there is an error, *fnamep is set to NULL.
26007 */
26008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026009modify_fname(
26010 char_u *src, /* string with modifiers */
26011 int *usedlen, /* characters after src that are used */
26012 char_u **fnamep, /* file name so far */
26013 char_u **bufp, /* buffer for allocated file name or NULL */
26014 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015{
26016 int valid = 0;
26017 char_u *tail;
26018 char_u *s, *p, *pbuf;
26019 char_u dirname[MAXPATHL];
26020 int c;
26021 int has_fullname = 0;
26022#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026023 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026024 int has_shortname = 0;
26025#endif
26026
26027repeat:
26028 /* ":p" - full path/file_name */
26029 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26030 {
26031 has_fullname = 1;
26032
26033 valid |= VALID_PATH;
26034 *usedlen += 2;
26035
26036 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26037 if ((*fnamep)[0] == '~'
26038#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26039 && ((*fnamep)[1] == '/'
26040# ifdef BACKSLASH_IN_FILENAME
26041 || (*fnamep)[1] == '\\'
26042# endif
26043 || (*fnamep)[1] == NUL)
26044
26045#endif
26046 )
26047 {
26048 *fnamep = expand_env_save(*fnamep);
26049 vim_free(*bufp); /* free any allocated file name */
26050 *bufp = *fnamep;
26051 if (*fnamep == NULL)
26052 return -1;
26053 }
26054
26055 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026056 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057 {
26058 if (vim_ispathsep(*p)
26059 && p[1] == '.'
26060 && (p[2] == NUL
26061 || vim_ispathsep(p[2])
26062 || (p[2] == '.'
26063 && (p[3] == NUL || vim_ispathsep(p[3])))))
26064 break;
26065 }
26066
26067 /* FullName_save() is slow, don't use it when not needed. */
26068 if (*p != NUL || !vim_isAbsName(*fnamep))
26069 {
26070 *fnamep = FullName_save(*fnamep, *p != NUL);
26071 vim_free(*bufp); /* free any allocated file name */
26072 *bufp = *fnamep;
26073 if (*fnamep == NULL)
26074 return -1;
26075 }
26076
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026077#ifdef WIN3264
26078# if _WIN32_WINNT >= 0x0500
26079 if (vim_strchr(*fnamep, '~') != NULL)
26080 {
26081 /* Expand 8.3 filename to full path. Needed to make sure the same
26082 * file does not have two different names.
26083 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26084 p = alloc(_MAX_PATH + 1);
26085 if (p != NULL)
26086 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026087 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026088 {
26089 vim_free(*bufp);
26090 *bufp = *fnamep = p;
26091 }
26092 else
26093 vim_free(p);
26094 }
26095 }
26096# endif
26097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098 /* Append a path separator to a directory. */
26099 if (mch_isdir(*fnamep))
26100 {
26101 /* Make room for one or two extra characters. */
26102 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26103 vim_free(*bufp); /* free any allocated file name */
26104 *bufp = *fnamep;
26105 if (*fnamep == NULL)
26106 return -1;
26107 add_pathsep(*fnamep);
26108 }
26109 }
26110
26111 /* ":." - path relative to the current directory */
26112 /* ":~" - path relative to the home directory */
26113 /* ":8" - shortname path - postponed till after */
26114 while (src[*usedlen] == ':'
26115 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26116 {
26117 *usedlen += 2;
26118 if (c == '8')
26119 {
26120#ifdef WIN3264
26121 has_shortname = 1; /* Postpone this. */
26122#endif
26123 continue;
26124 }
26125 pbuf = NULL;
26126 /* Need full path first (use expand_env() to remove a "~/") */
26127 if (!has_fullname)
26128 {
26129 if (c == '.' && **fnamep == '~')
26130 p = pbuf = expand_env_save(*fnamep);
26131 else
26132 p = pbuf = FullName_save(*fnamep, FALSE);
26133 }
26134 else
26135 p = *fnamep;
26136
26137 has_fullname = 0;
26138
26139 if (p != NULL)
26140 {
26141 if (c == '.')
26142 {
26143 mch_dirname(dirname, MAXPATHL);
26144 s = shorten_fname(p, dirname);
26145 if (s != NULL)
26146 {
26147 *fnamep = s;
26148 if (pbuf != NULL)
26149 {
26150 vim_free(*bufp); /* free any allocated file name */
26151 *bufp = pbuf;
26152 pbuf = NULL;
26153 }
26154 }
26155 }
26156 else
26157 {
26158 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26159 /* Only replace it when it starts with '~' */
26160 if (*dirname == '~')
26161 {
26162 s = vim_strsave(dirname);
26163 if (s != NULL)
26164 {
26165 *fnamep = s;
26166 vim_free(*bufp);
26167 *bufp = s;
26168 }
26169 }
26170 }
26171 vim_free(pbuf);
26172 }
26173 }
26174
26175 tail = gettail(*fnamep);
26176 *fnamelen = (int)STRLEN(*fnamep);
26177
26178 /* ":h" - head, remove "/file_name", can be repeated */
26179 /* Don't remove the first "/" or "c:\" */
26180 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26181 {
26182 valid |= VALID_HEAD;
26183 *usedlen += 2;
26184 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026185 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026186 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187 *fnamelen = (int)(tail - *fnamep);
26188#ifdef VMS
26189 if (*fnamelen > 0)
26190 *fnamelen += 1; /* the path separator is part of the path */
26191#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026192 if (*fnamelen == 0)
26193 {
26194 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26195 p = vim_strsave((char_u *)".");
26196 if (p == NULL)
26197 return -1;
26198 vim_free(*bufp);
26199 *bufp = *fnamep = tail = p;
26200 *fnamelen = 1;
26201 }
26202 else
26203 {
26204 while (tail > s && !after_pathsep(s, tail))
26205 mb_ptr_back(*fnamep, tail);
26206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 }
26208
26209 /* ":8" - shortname */
26210 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26211 {
26212 *usedlen += 2;
26213#ifdef WIN3264
26214 has_shortname = 1;
26215#endif
26216 }
26217
26218#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026219 /*
26220 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 */
26222 if (has_shortname)
26223 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026224 /* Copy the string if it is shortened by :h and when it wasn't copied
26225 * yet, because we are going to change it in place. Avoids changing
26226 * the buffer name for "%:8". */
26227 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228 {
26229 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026230 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231 return -1;
26232 vim_free(*bufp);
26233 *bufp = *fnamep = p;
26234 }
26235
26236 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026237 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 if (!has_fullname && !vim_isAbsName(*fnamep))
26239 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026240 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241 return -1;
26242 }
26243 else
26244 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026245 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246
Bram Moolenaardc935552011-08-17 15:23:23 +020026247 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026249 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250 return -1;
26251
26252 if (l == 0)
26253 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026254 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026256 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257 return -1;
26258 }
26259 *fnamelen = l;
26260 }
26261 }
26262#endif /* WIN3264 */
26263
26264 /* ":t" - tail, just the basename */
26265 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26266 {
26267 *usedlen += 2;
26268 *fnamelen -= (int)(tail - *fnamep);
26269 *fnamep = tail;
26270 }
26271
26272 /* ":e" - extension, can be repeated */
26273 /* ":r" - root, without extension, can be repeated */
26274 while (src[*usedlen] == ':'
26275 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26276 {
26277 /* find a '.' in the tail:
26278 * - for second :e: before the current fname
26279 * - otherwise: The last '.'
26280 */
26281 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26282 s = *fnamep - 2;
26283 else
26284 s = *fnamep + *fnamelen - 1;
26285 for ( ; s > tail; --s)
26286 if (s[0] == '.')
26287 break;
26288 if (src[*usedlen + 1] == 'e') /* :e */
26289 {
26290 if (s > tail)
26291 {
26292 *fnamelen += (int)(*fnamep - (s + 1));
26293 *fnamep = s + 1;
26294#ifdef VMS
26295 /* cut version from the extension */
26296 s = *fnamep + *fnamelen - 1;
26297 for ( ; s > *fnamep; --s)
26298 if (s[0] == ';')
26299 break;
26300 if (s > *fnamep)
26301 *fnamelen = s - *fnamep;
26302#endif
26303 }
26304 else if (*fnamep <= tail)
26305 *fnamelen = 0;
26306 }
26307 else /* :r */
26308 {
26309 if (s > tail) /* remove one extension */
26310 *fnamelen = (int)(s - *fnamep);
26311 }
26312 *usedlen += 2;
26313 }
26314
26315 /* ":s?pat?foo?" - substitute */
26316 /* ":gs?pat?foo?" - global substitute */
26317 if (src[*usedlen] == ':'
26318 && (src[*usedlen + 1] == 's'
26319 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26320 {
26321 char_u *str;
26322 char_u *pat;
26323 char_u *sub;
26324 int sep;
26325 char_u *flags;
26326 int didit = FALSE;
26327
26328 flags = (char_u *)"";
26329 s = src + *usedlen + 2;
26330 if (src[*usedlen + 1] == 'g')
26331 {
26332 flags = (char_u *)"g";
26333 ++s;
26334 }
26335
26336 sep = *s++;
26337 if (sep)
26338 {
26339 /* find end of pattern */
26340 p = vim_strchr(s, sep);
26341 if (p != NULL)
26342 {
26343 pat = vim_strnsave(s, (int)(p - s));
26344 if (pat != NULL)
26345 {
26346 s = p + 1;
26347 /* find end of substitution */
26348 p = vim_strchr(s, sep);
26349 if (p != NULL)
26350 {
26351 sub = vim_strnsave(s, (int)(p - s));
26352 str = vim_strnsave(*fnamep, *fnamelen);
26353 if (sub != NULL && str != NULL)
26354 {
26355 *usedlen = (int)(p + 1 - src);
26356 s = do_string_sub(str, pat, sub, flags);
26357 if (s != NULL)
26358 {
26359 *fnamep = s;
26360 *fnamelen = (int)STRLEN(s);
26361 vim_free(*bufp);
26362 *bufp = s;
26363 didit = TRUE;
26364 }
26365 }
26366 vim_free(sub);
26367 vim_free(str);
26368 }
26369 vim_free(pat);
26370 }
26371 }
26372 /* after using ":s", repeat all the modifiers */
26373 if (didit)
26374 goto repeat;
26375 }
26376 }
26377
Bram Moolenaar26df0922014-02-23 23:39:13 +010026378 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26379 {
26380 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26381 if (p == NULL)
26382 return -1;
26383 vim_free(*bufp);
26384 *bufp = *fnamep = p;
26385 *fnamelen = (int)STRLEN(p);
26386 *usedlen += 2;
26387 }
26388
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389 return valid;
26390}
26391
26392/*
26393 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26394 * "flags" can be "g" to do a global substitute.
26395 * Returns an allocated string, NULL for error.
26396 */
26397 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026398do_string_sub(
26399 char_u *str,
26400 char_u *pat,
26401 char_u *sub,
26402 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403{
26404 int sublen;
26405 regmatch_T regmatch;
26406 int i;
26407 int do_all;
26408 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026409 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410 garray_T ga;
26411 char_u *ret;
26412 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026413 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414
26415 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26416 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026417 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418
26419 ga_init2(&ga, 1, 200);
26420
26421 do_all = (flags[0] == 'g');
26422
26423 regmatch.rm_ic = p_ic;
26424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26425 if (regmatch.regprog != NULL)
26426 {
26427 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026428 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26430 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026431 /* Skip empty match except for first match. */
26432 if (regmatch.startp[0] == regmatch.endp[0])
26433 {
26434 if (zero_width == regmatch.startp[0])
26435 {
26436 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026437 i = MB_PTR2LEN(tail);
26438 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26439 (size_t)i);
26440 ga.ga_len += i;
26441 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026442 continue;
26443 }
26444 zero_width = regmatch.startp[0];
26445 }
26446
Bram Moolenaar071d4272004-06-13 20:20:40 +000026447 /*
26448 * Get some space for a temporary buffer to do the substitution
26449 * into. It will contain:
26450 * - The text up to where the match is.
26451 * - The substituted text.
26452 * - The text after the match.
26453 */
26454 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026455 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26457 {
26458 ga_clear(&ga);
26459 break;
26460 }
26461
26462 /* copy the text up to where the match is */
26463 i = (int)(regmatch.startp[0] - tail);
26464 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26465 /* add the substituted text */
26466 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26467 + ga.ga_len + i, TRUE, TRUE, FALSE);
26468 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026469 tail = regmatch.endp[0];
26470 if (*tail == NUL)
26471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026472 if (!do_all)
26473 break;
26474 }
26475
26476 if (ga.ga_data != NULL)
26477 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26478
Bram Moolenaar473de612013-06-08 18:19:48 +020026479 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480 }
26481
26482 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26483 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026484 if (p_cpo == empty_option)
26485 p_cpo = save_cpo;
26486 else
26487 /* Darn, evaluating {sub} expression changed the value. */
26488 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489
26490 return ret;
26491}
26492
26493#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */