blob: 3db58f8d805f4f6bc90854fc4ef1f1cbef1fe09a [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 Moolenaar1735bc92016-03-14 23:05:14 +010011907 if (pt != NULL)
11908 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011909 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011910 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011911 listitem_T *li;
11912 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011913 int arg_len = 0;
11914 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011915
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011916 if (arg_pt != NULL)
11917 arg_len = arg_pt->pt_argc;
11918 if (list != NULL)
11919 lv_len = list->lv_len;
11920 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011921 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011922 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011923 if (pt->pt_argv == NULL)
11924 {
11925 vim_free(pt);
11926 vim_free(name);
11927 return;
11928 }
11929 else
11930 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011931 for (i = 0; i < arg_len; i++)
11932 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11933 if (lv_len > 0)
11934 for (li = list->lv_first; li != NULL;
11935 li = li->li_next)
11936 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011937 }
11938 }
11939
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011940 /* For "function(dict.func, [], dict)" and "func" is a partial
11941 * use "dict". That is backwards compatible. */
11942 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011943 {
11944 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11945 ++pt->pt_dict->dv_refcount;
11946 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011947 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011948 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011949 pt->pt_dict = arg_pt->pt_dict;
11950 if (pt->pt_dict != NULL)
11951 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011952 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011953
11954 pt->pt_refcount = 1;
11955 pt->pt_name = name;
11956 func_ref(pt->pt_name);
11957 }
11958 rettv->v_type = VAR_PARTIAL;
11959 rettv->vval.v_partial = pt;
11960 }
11961 else
11962 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011963 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011964 rettv->v_type = VAR_FUNC;
11965 rettv->vval.v_string = name;
11966 func_ref(name);
11967 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011968 }
11969}
11970
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011971 static void
11972partial_free(partial_T *pt)
11973{
11974 int i;
11975
11976 for (i = 0; i < pt->pt_argc; ++i)
11977 clear_tv(&pt->pt_argv[i]);
11978 vim_free(pt->pt_argv);
11979 func_unref(pt->pt_name);
11980 vim_free(pt->pt_name);
11981 vim_free(pt);
11982}
11983
11984/*
11985 * Unreference a closure: decrement the reference count and free it when it
11986 * becomes zero.
11987 */
11988 void
11989partial_unref(partial_T *pt)
11990{
11991 if (pt != NULL && --pt->pt_refcount <= 0)
11992 partial_free(pt);
11993}
11994
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011995/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011996 * "garbagecollect()" function
11997 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012000{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012001 /* This is postponed until we are back at the toplevel, because we may be
12002 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12003 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012004
12005 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12006 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012007}
12008
12009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010 * "get()" function
12011 */
12012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012013f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014{
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 listitem_T *li;
12016 list_T *l;
12017 dictitem_T *di;
12018 dict_T *d;
12019 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020
Bram Moolenaare9a41262005-01-15 22:18:47 +000012021 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012023 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 int error = FALSE;
12026
12027 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12028 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012029 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012032 else if (argvars[0].v_type == VAR_DICT)
12033 {
12034 if ((d = argvars[0].vval.v_dict) != NULL)
12035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012036 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012037 if (di != NULL)
12038 tv = &di->di_tv;
12039 }
12040 }
12041 else
12042 EMSG2(_(e_listdictarg), "get()");
12043
12044 if (tv == NULL)
12045 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012047 copy_tv(&argvars[2], rettv);
12048 }
12049 else
12050 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012051}
12052
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012053static 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 +000012054
12055/*
12056 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012057 * Return a range (from start to end) of lines in rettv from the specified
12058 * buffer.
12059 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012060 */
12061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062get_buffer_lines(
12063 buf_T *buf,
12064 linenr_T start,
12065 linenr_T end,
12066 int retlist,
12067 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012068{
12069 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012070
Bram Moolenaar959a1432013-12-14 12:17:38 +010012071 rettv->v_type = VAR_STRING;
12072 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012073 if (retlist && rettv_list_alloc(rettv) == FAIL)
12074 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012075
12076 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12077 return;
12078
12079 if (!retlist)
12080 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012081 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12082 p = ml_get_buf(buf, start, FALSE);
12083 else
12084 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012085 rettv->vval.v_string = vim_strsave(p);
12086 }
12087 else
12088 {
12089 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012090 return;
12091
12092 if (start < 1)
12093 start = 1;
12094 if (end > buf->b_ml.ml_line_count)
12095 end = buf->b_ml.ml_line_count;
12096 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012097 if (list_append_string(rettv->vval.v_list,
12098 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012099 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012100 }
12101}
12102
12103/*
12104 * "getbufline()" function
12105 */
12106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012107f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012108{
12109 linenr_T lnum;
12110 linenr_T end;
12111 buf_T *buf;
12112
12113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012115 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012116 --emsg_off;
12117
Bram Moolenaar661b1822005-07-28 22:36:45 +000012118 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012119 if (argvars[2].v_type == VAR_UNKNOWN)
12120 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012121 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012122 end = get_tv_lnum_buf(&argvars[2], buf);
12123
Bram Moolenaar342337a2005-07-21 21:11:17 +000012124 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012125}
12126
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127/*
12128 * "getbufvar()" function
12129 */
12130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012131f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132{
12133 buf_T *buf;
12134 buf_T *save_curbuf;
12135 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012137 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012139 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12140 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012142 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012144 rettv->v_type = VAR_STRING;
12145 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146
12147 if (buf != NULL && varname != NULL)
12148 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012149 /* set curbuf to be our buf, temporarily */
12150 save_curbuf = curbuf;
12151 curbuf = buf;
12152
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012154 {
12155 if (get_option_tv(&varname, rettv, TRUE) == OK)
12156 done = TRUE;
12157 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012158 else if (STRCMP(varname, "changedtick") == 0)
12159 {
12160 rettv->v_type = VAR_NUMBER;
12161 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012162 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 else
12165 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012166 /* Look up the variable. */
12167 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12168 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12169 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012172 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012173 done = TRUE;
12174 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012175 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012176
12177 /* restore previous notion of curbuf */
12178 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012179 }
12180
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012181 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12182 /* use the default value */
12183 copy_tv(&argvars[2], rettv);
12184
Bram Moolenaar0d660222005-01-07 21:51:51 +000012185 --emsg_off;
12186}
12187
12188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189 * "getchar()" function
12190 */
12191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012192f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193{
12194 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012195 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012197 /* Position the cursor. Needed after a message that ends in a space. */
12198 windgoto(msg_row, msg_col);
12199
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 ++no_mapping;
12201 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012202 for (;;)
12203 {
12204 if (argvars[0].v_type == VAR_UNKNOWN)
12205 /* getchar(): blocking wait. */
12206 n = safe_vgetc();
12207 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12208 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012209 n = vpeekc_any();
12210 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012211 /* illegal argument or getchar(0) and no char avail: return zero */
12212 n = 0;
12213 else
12214 /* getchar(0) and char avail: return char */
12215 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012216
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012217 if (n == K_IGNORE)
12218 continue;
12219 break;
12220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 --no_mapping;
12222 --allow_keys;
12223
Bram Moolenaar219b8702006-11-01 14:32:36 +000012224 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12225 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12226 vimvars[VV_MOUSE_COL].vv_nr = 0;
12227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012228 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229 if (IS_SPECIAL(n) || mod_mask != 0)
12230 {
12231 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12232 int i = 0;
12233
12234 /* Turn a special key into three bytes, plus modifier. */
12235 if (mod_mask != 0)
12236 {
12237 temp[i++] = K_SPECIAL;
12238 temp[i++] = KS_MODIFIER;
12239 temp[i++] = mod_mask;
12240 }
12241 if (IS_SPECIAL(n))
12242 {
12243 temp[i++] = K_SPECIAL;
12244 temp[i++] = K_SECOND(n);
12245 temp[i++] = K_THIRD(n);
12246 }
12247#ifdef FEAT_MBYTE
12248 else if (has_mbyte)
12249 i += (*mb_char2bytes)(n, temp + i);
12250#endif
12251 else
12252 temp[i++] = n;
12253 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 rettv->v_type = VAR_STRING;
12255 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012256
12257#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012258 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012259 {
12260 int row = mouse_row;
12261 int col = mouse_col;
12262 win_T *win;
12263 linenr_T lnum;
12264# ifdef FEAT_WINDOWS
12265 win_T *wp;
12266# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012267 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012268
12269 if (row >= 0 && col >= 0)
12270 {
12271 /* Find the window at the mouse coordinates and compute the
12272 * text position. */
12273 win = mouse_find_win(&row, &col);
12274 (void)mouse_comp_pos(win, &row, &col, &lnum);
12275# ifdef FEAT_WINDOWS
12276 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012277 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012278# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012279 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012280 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12281 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12282 }
12283 }
12284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 }
12286}
12287
12288/*
12289 * "getcharmod()" function
12290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295}
12296
12297/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012298 * "getcharsearch()" function
12299 */
12300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012301f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012302{
12303 if (rettv_dict_alloc(rettv) != FAIL)
12304 {
12305 dict_T *dict = rettv->vval.v_dict;
12306
12307 dict_add_nr_str(dict, "char", 0L, last_csearch());
12308 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12309 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12310 }
12311}
12312
12313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 * "getcmdline()" function
12315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012317f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->v_type = VAR_STRING;
12320 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321}
12322
12323/*
12324 * "getcmdpos()" function
12325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012327f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330}
12331
12332/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012333 * "getcmdtype()" function
12334 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012336f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012337{
12338 rettv->v_type = VAR_STRING;
12339 rettv->vval.v_string = alloc(2);
12340 if (rettv->vval.v_string != NULL)
12341 {
12342 rettv->vval.v_string[0] = get_cmdline_type();
12343 rettv->vval.v_string[1] = NUL;
12344 }
12345}
12346
12347/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012348 * "getcmdwintype()" function
12349 */
12350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012351f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012352{
12353 rettv->v_type = VAR_STRING;
12354 rettv->vval.v_string = NULL;
12355#ifdef FEAT_CMDWIN
12356 rettv->vval.v_string = alloc(2);
12357 if (rettv->vval.v_string != NULL)
12358 {
12359 rettv->vval.v_string[0] = cmdwin_type;
12360 rettv->vval.v_string[1] = NUL;
12361 }
12362#endif
12363}
12364
12365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 * "getcwd()" function
12367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012369f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012371 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012372 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012375 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012376
12377 wp = find_tabwin(&argvars[0], &argvars[1]);
12378 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012380 if (wp->w_localdir != NULL)
12381 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12382 else if(globaldir != NULL)
12383 rettv->vval.v_string = vim_strsave(globaldir);
12384 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012385 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012386 cwd = alloc(MAXPATHL);
12387 if (cwd != NULL)
12388 {
12389 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12390 rettv->vval.v_string = vim_strsave(cwd);
12391 vim_free(cwd);
12392 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012393 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012394#ifdef BACKSLASH_IN_FILENAME
12395 if (rettv->vval.v_string != NULL)
12396 slash_adjust(rettv->vval.v_string);
12397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398 }
12399}
12400
12401/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012402 * "getfontname()" function
12403 */
12404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012405f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012406{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 rettv->v_type = VAR_STRING;
12408 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012409#ifdef FEAT_GUI
12410 if (gui.in_use)
12411 {
12412 GuiFont font;
12413 char_u *name = NULL;
12414
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012415 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012416 {
12417 /* Get the "Normal" font. Either the name saved by
12418 * hl_set_font_name() or from the font ID. */
12419 font = gui.norm_font;
12420 name = hl_get_font_name();
12421 }
12422 else
12423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012424 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012425 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12426 return;
12427 font = gui_mch_get_font(name, FALSE);
12428 if (font == NOFONT)
12429 return; /* Invalid font name, return empty string. */
12430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012432 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012433 gui_mch_free_font(font);
12434 }
12435#endif
12436}
12437
12438/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012439 * "getfperm({fname})" function
12440 */
12441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012442f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012443{
12444 char_u *fname;
12445 struct stat st;
12446 char_u *perm = NULL;
12447 char_u flags[] = "rwx";
12448 int i;
12449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012453 if (mch_stat((char *)fname, &st) >= 0)
12454 {
12455 perm = vim_strsave((char_u *)"---------");
12456 if (perm != NULL)
12457 {
12458 for (i = 0; i < 9; i++)
12459 {
12460 if (st.st_mode & (1 << (8 - i)))
12461 perm[i] = flags[i % 3];
12462 }
12463 }
12464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012466}
12467
12468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 * "getfsize({fname})" function
12470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473{
12474 char_u *fname;
12475 struct stat st;
12476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480
12481 if (mch_stat((char *)fname, &st) >= 0)
12482 {
12483 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012488
12489 /* non-perfect check for overflow */
12490 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12491 rettv->vval.v_number = -2;
12492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 }
12494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496}
12497
12498/*
12499 * "getftime({fname})" function
12500 */
12501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503{
12504 char_u *fname;
12505 struct stat st;
12506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508
12509 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513}
12514
12515/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012516 * "getftype({fname})" function
12517 */
12518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012519f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012520{
12521 char_u *fname;
12522 struct stat st;
12523 char_u *type = NULL;
12524 char *t;
12525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012529 if (mch_lstat((char *)fname, &st) >= 0)
12530 {
12531#ifdef S_ISREG
12532 if (S_ISREG(st.st_mode))
12533 t = "file";
12534 else if (S_ISDIR(st.st_mode))
12535 t = "dir";
12536# ifdef S_ISLNK
12537 else if (S_ISLNK(st.st_mode))
12538 t = "link";
12539# endif
12540# ifdef S_ISBLK
12541 else if (S_ISBLK(st.st_mode))
12542 t = "bdev";
12543# endif
12544# ifdef S_ISCHR
12545 else if (S_ISCHR(st.st_mode))
12546 t = "cdev";
12547# endif
12548# ifdef S_ISFIFO
12549 else if (S_ISFIFO(st.st_mode))
12550 t = "fifo";
12551# endif
12552# ifdef S_ISSOCK
12553 else if (S_ISSOCK(st.st_mode))
12554 t = "fifo";
12555# endif
12556 else
12557 t = "other";
12558#else
12559# ifdef S_IFMT
12560 switch (st.st_mode & S_IFMT)
12561 {
12562 case S_IFREG: t = "file"; break;
12563 case S_IFDIR: t = "dir"; break;
12564# ifdef S_IFLNK
12565 case S_IFLNK: t = "link"; break;
12566# endif
12567# ifdef S_IFBLK
12568 case S_IFBLK: t = "bdev"; break;
12569# endif
12570# ifdef S_IFCHR
12571 case S_IFCHR: t = "cdev"; break;
12572# endif
12573# ifdef S_IFIFO
12574 case S_IFIFO: t = "fifo"; break;
12575# endif
12576# ifdef S_IFSOCK
12577 case S_IFSOCK: t = "socket"; break;
12578# endif
12579 default: t = "other";
12580 }
12581# else
12582 if (mch_isdir(fname))
12583 t = "dir";
12584 else
12585 t = "file";
12586# endif
12587#endif
12588 type = vim_strsave((char_u *)t);
12589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012591}
12592
12593/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012594 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598{
12599 linenr_T lnum;
12600 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012601 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012602
12603 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012604 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012605 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012606 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012607 retlist = FALSE;
12608 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012610 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012612 retlist = TRUE;
12613 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012614
Bram Moolenaar342337a2005-07-21 21:11:17 +000012615 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012616}
12617
12618/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012619 * "getmatches()" function
12620 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012622f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012623{
12624#ifdef FEAT_SEARCH_EXTRA
12625 dict_T *dict;
12626 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012627 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012628
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012629 if (rettv_list_alloc(rettv) == OK)
12630 {
12631 while (cur != NULL)
12632 {
12633 dict = dict_alloc();
12634 if (dict == NULL)
12635 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012636 if (cur->match.regprog == NULL)
12637 {
12638 /* match added with matchaddpos() */
12639 for (i = 0; i < MAXPOSMATCH; ++i)
12640 {
12641 llpos_T *llpos;
12642 char buf[6];
12643 list_T *l;
12644
12645 llpos = &cur->pos.pos[i];
12646 if (llpos->lnum == 0)
12647 break;
12648 l = list_alloc();
12649 if (l == NULL)
12650 break;
12651 list_append_number(l, (varnumber_T)llpos->lnum);
12652 if (llpos->col > 0)
12653 {
12654 list_append_number(l, (varnumber_T)llpos->col);
12655 list_append_number(l, (varnumber_T)llpos->len);
12656 }
12657 sprintf(buf, "pos%d", i + 1);
12658 dict_add_list(dict, buf, l);
12659 }
12660 }
12661 else
12662 {
12663 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12664 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012665 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012666 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12667 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012668# ifdef FEAT_CONCEAL
12669 if (cur->conceal_char)
12670 {
12671 char_u buf[MB_MAXBYTES + 1];
12672
12673 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12674 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12675 }
12676# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012677 list_append_dict(rettv->vval.v_list, dict);
12678 cur = cur->next;
12679 }
12680 }
12681#endif
12682}
12683
12684/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012685 * "getpid()" function
12686 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012688f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012689{
12690 rettv->vval.v_number = mch_get_pid();
12691}
12692
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012693static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012694
12695/*
12696 * "getcurpos()" function
12697 */
12698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012699f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012700{
12701 getpos_both(argvars, rettv, TRUE);
12702}
12703
Bram Moolenaar18081e32008-02-20 19:11:07 +000012704/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012705 * "getpos(string)" function
12706 */
12707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012708f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012709{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012710 getpos_both(argvars, rettv, FALSE);
12711}
12712
12713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714getpos_both(
12715 typval_T *argvars,
12716 typval_T *rettv,
12717 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012718{
Bram Moolenaara5525202006-03-02 22:52:09 +000012719 pos_T *fp;
12720 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012721 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012722
12723 if (rettv_list_alloc(rettv) == OK)
12724 {
12725 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012726 if (getcurpos)
12727 fp = &curwin->w_cursor;
12728 else
12729 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012730 if (fnum != -1)
12731 list_append_number(l, (varnumber_T)fnum);
12732 else
12733 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012734 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12735 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012736 list_append_number(l, (fp != NULL)
12737 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012738 : (varnumber_T)0);
12739 list_append_number(l,
12740#ifdef FEAT_VIRTUALEDIT
12741 (fp != NULL) ? (varnumber_T)fp->coladd :
12742#endif
12743 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012744 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012745 {
12746 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012747 list_append_number(l, curwin->w_curswant == MAXCOL ?
12748 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012749 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012750 }
12751 else
12752 rettv->vval.v_number = FALSE;
12753}
12754
12755/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012756 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012757 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012759f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012760{
12761#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012762 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012763#endif
12764
Bram Moolenaar2641f772005-03-25 21:58:17 +000012765#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012766 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012767 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012768 wp = NULL;
12769 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12770 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012771 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012772 if (wp == NULL)
12773 return;
12774 }
12775
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012776 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012777 }
12778#endif
12779}
12780
12781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 * "getreg()" function
12783 */
12784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012785f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786{
12787 char_u *strregname;
12788 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012789 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012790 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012791 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012793 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 strregname = get_tv_string_chk(&argvars[0]);
12796 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012797 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012800 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12801 return_list = get_tv_number_chk(&argvars[2], &error);
12802 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012805 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012806
12807 if (error)
12808 return;
12809
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 regname = (strregname == NULL ? '"' : *strregname);
12811 if (regname == 0)
12812 regname = '"';
12813
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012814 if (return_list)
12815 {
12816 rettv->v_type = VAR_LIST;
12817 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12818 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012819 if (rettv->vval.v_list != NULL)
12820 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012821 }
12822 else
12823 {
12824 rettv->v_type = VAR_STRING;
12825 rettv->vval.v_string = get_reg_contents(regname,
12826 arg2 ? GREG_EXPR_SRC : 0);
12827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
12831 * "getregtype()" function
12832 */
12833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012834f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835{
12836 char_u *strregname;
12837 int regname;
12838 char_u buf[NUMBUFLEN + 2];
12839 long reglen = 0;
12840
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012841 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012842 {
12843 strregname = get_tv_string_chk(&argvars[0]);
12844 if (strregname == NULL) /* type error; errmsg already given */
12845 {
12846 rettv->v_type = VAR_STRING;
12847 rettv->vval.v_string = NULL;
12848 return;
12849 }
12850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851 else
12852 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012853 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854
12855 regname = (strregname == NULL ? '"' : *strregname);
12856 if (regname == 0)
12857 regname = '"';
12858
12859 buf[0] = NUL;
12860 buf[1] = NUL;
12861 switch (get_reg_type(regname, &reglen))
12862 {
12863 case MLINE: buf[0] = 'V'; break;
12864 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 case MBLOCK:
12866 buf[0] = Ctrl_V;
12867 sprintf((char *)buf + 1, "%ld", reglen + 1);
12868 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 rettv->v_type = VAR_STRING;
12871 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
12874/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012875 * "gettabvar()" function
12876 */
12877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012878f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012879{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012880 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012881 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012882 dictitem_T *v;
12883 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012884 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012885
12886 rettv->v_type = VAR_STRING;
12887 rettv->vval.v_string = NULL;
12888
12889 varname = get_tv_string_chk(&argvars[1]);
12890 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12891 if (tp != NULL && varname != NULL)
12892 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012893 /* Set tp to be our tabpage, temporarily. Also set the window to the
12894 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012895 if (switch_win(&oldcurwin, &oldtabpage,
12896 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012897 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012898 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012899 /* look up the variable */
12900 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12901 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12902 if (v != NULL)
12903 {
12904 copy_tv(&v->di_tv, rettv);
12905 done = TRUE;
12906 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012907 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012908
12909 /* restore previous notion of curwin */
12910 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012911 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012912
12913 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012914 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012915}
12916
12917/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012918 * "gettabwinvar()" function
12919 */
12920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012921f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012922{
12923 getwinvar(argvars, rettv, 1);
12924}
12925
12926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 * "getwinposx()" function
12928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012930f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933#ifdef FEAT_GUI
12934 if (gui.in_use)
12935 {
12936 int x, y;
12937
12938 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 }
12941#endif
12942}
12943
12944/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012945 * "win_findbuf()" function
12946 */
12947 static void
12948f_win_findbuf(typval_T *argvars, typval_T *rettv)
12949{
12950 if (rettv_list_alloc(rettv) != FAIL)
12951 win_findbuf(argvars, rettv->vval.v_list);
12952}
12953
12954/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012955 * "win_getid()" function
12956 */
12957 static void
12958f_win_getid(typval_T *argvars, typval_T *rettv)
12959{
12960 rettv->vval.v_number = win_getid(argvars);
12961}
12962
12963/*
12964 * "win_gotoid()" function
12965 */
12966 static void
12967f_win_gotoid(typval_T *argvars, typval_T *rettv)
12968{
12969 rettv->vval.v_number = win_gotoid(argvars);
12970}
12971
12972/*
12973 * "win_id2tabwin()" function
12974 */
12975 static void
12976f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12977{
12978 if (rettv_list_alloc(rettv) != FAIL)
12979 win_id2tabwin(argvars, rettv->vval.v_list);
12980}
12981
12982/*
12983 * "win_id2win()" function
12984 */
12985 static void
12986f_win_id2win(typval_T *argvars, typval_T *rettv)
12987{
12988 rettv->vval.v_number = win_id2win(argvars);
12989}
12990
12991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 * "getwinposy()" function
12993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012995f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998#ifdef FEAT_GUI
12999 if (gui.in_use)
13000 {
13001 int x, y;
13002
13003 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013004 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 }
13006#endif
13007}
13008
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013009/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013010 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013011 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013012 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013013find_win_by_nr(
13014 typval_T *vp,
13015 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013016{
13017#ifdef FEAT_WINDOWS
13018 win_T *wp;
13019#endif
13020 int nr;
13021
13022 nr = get_tv_number_chk(vp, NULL);
13023
13024#ifdef FEAT_WINDOWS
13025 if (nr < 0)
13026 return NULL;
13027 if (nr == 0)
13028 return curwin;
13029
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013030 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13031 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013032 if (--nr <= 0)
13033 break;
13034 return wp;
13035#else
13036 if (nr == 0 || nr == 1)
13037 return curwin;
13038 return NULL;
13039#endif
13040}
13041
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013043 * Find window specified by "wvp" in tabpage "tvp".
13044 */
13045 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046find_tabwin(
13047 typval_T *wvp, /* VAR_UNKNOWN for current window */
13048 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013049{
13050 win_T *wp = NULL;
13051 tabpage_T *tp = NULL;
13052 long n;
13053
13054 if (wvp->v_type != VAR_UNKNOWN)
13055 {
13056 if (tvp->v_type != VAR_UNKNOWN)
13057 {
13058 n = get_tv_number(tvp);
13059 if (n >= 0)
13060 tp = find_tabpage(n);
13061 }
13062 else
13063 tp = curtab;
13064
13065 if (tp != NULL)
13066 wp = find_win_by_nr(wvp, tp);
13067 }
13068 else
13069 wp = curwin;
13070
13071 return wp;
13072}
13073
13074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013075 * "getwinvar()" function
13076 */
13077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013078f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013080 getwinvar(argvars, rettv, 0);
13081}
13082
13083/*
13084 * getwinvar() and gettabwinvar()
13085 */
13086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013087getwinvar(
13088 typval_T *argvars,
13089 typval_T *rettv,
13090 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013091{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013092 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013094 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013095 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013096 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013097#ifdef FEAT_WINDOWS
13098 win_T *oldcurwin;
13099 tabpage_T *oldtabpage;
13100 int need_switch_win;
13101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013103#ifdef FEAT_WINDOWS
13104 if (off == 1)
13105 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13106 else
13107 tp = curtab;
13108#endif
13109 win = find_win_by_nr(&argvars[off], tp);
13110 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013111 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115
13116 if (win != NULL && varname != NULL)
13117 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013118#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013119 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013120 * otherwise the window is not valid. Only do this when needed,
13121 * autocommands get blocked. */
13122 need_switch_win = !(tp == curtab && win == curwin);
13123 if (!need_switch_win
13124 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13125#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013126 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013127 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013128 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013129 if (get_option_tv(&varname, rettv, 1) == OK)
13130 done = TRUE;
13131 }
13132 else
13133 {
13134 /* Look up the variable. */
13135 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13136 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13137 varname, FALSE);
13138 if (v != NULL)
13139 {
13140 copy_tv(&v->di_tv, rettv);
13141 done = TRUE;
13142 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013145
Bram Moolenaarba117c22015-09-29 16:53:22 +020013146#ifdef FEAT_WINDOWS
13147 if (need_switch_win)
13148 /* restore previous notion of curwin */
13149 restore_win(oldcurwin, oldtabpage, TRUE);
13150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151 }
13152
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013153 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13154 /* use the default return value */
13155 copy_tv(&argvars[off + 2], rettv);
13156
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 --emsg_off;
13158}
13159
13160/*
13161 * "glob()" function
13162 */
13163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013164f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013166 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013168 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013170 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013171 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013173 if (argvars[1].v_type != VAR_UNKNOWN)
13174 {
13175 if (get_tv_number_chk(&argvars[1], &error))
13176 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013177 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013178 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013179 if (get_tv_number_chk(&argvars[2], &error))
13180 {
13181 rettv->v_type = VAR_LIST;
13182 rettv->vval.v_list = NULL;
13183 }
13184 if (argvars[3].v_type != VAR_UNKNOWN
13185 && get_tv_number_chk(&argvars[3], &error))
13186 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013187 }
13188 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013189 if (!error)
13190 {
13191 ExpandInit(&xpc);
13192 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013193 if (p_wic)
13194 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013195 if (rettv->v_type == VAR_STRING)
13196 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013197 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013198 else if (rettv_list_alloc(rettv) != FAIL)
13199 {
13200 int i;
13201
13202 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13203 NULL, options, WILD_ALL_KEEP);
13204 for (i = 0; i < xpc.xp_numfiles; i++)
13205 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13206
13207 ExpandCleanup(&xpc);
13208 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013209 }
13210 else
13211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212}
13213
13214/*
13215 * "globpath()" function
13216 */
13217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013220 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013223 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013224 garray_T ga;
13225 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013227 /* When the optional second argument is non-zero, don't remove matches
13228 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013230 if (argvars[2].v_type != VAR_UNKNOWN)
13231 {
13232 if (get_tv_number_chk(&argvars[2], &error))
13233 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013234 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013235 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013236 if (get_tv_number_chk(&argvars[3], &error))
13237 {
13238 rettv->v_type = VAR_LIST;
13239 rettv->vval.v_list = NULL;
13240 }
13241 if (argvars[4].v_type != VAR_UNKNOWN
13242 && get_tv_number_chk(&argvars[4], &error))
13243 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013244 }
13245 }
13246 if (file != NULL && !error)
13247 {
13248 ga_init2(&ga, (int)sizeof(char_u *), 10);
13249 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13250 if (rettv->v_type == VAR_STRING)
13251 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13252 else if (rettv_list_alloc(rettv) != FAIL)
13253 for (i = 0; i < ga.ga_len; ++i)
13254 list_append_string(rettv->vval.v_list,
13255 ((char_u **)(ga.ga_data))[i], -1);
13256 ga_clear_strings(&ga);
13257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260}
13261
13262/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013263 * "glob2regpat()" function
13264 */
13265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013266f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013267{
13268 char_u *pat = get_tv_string_chk(&argvars[0]);
13269
13270 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013271 rettv->vval.v_string = (pat == NULL)
13272 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013273}
13274
13275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 * "has()" function
13277 */
13278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013279f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280{
13281 int i;
13282 char_u *name;
13283 int n = FALSE;
13284 static char *(has_list[]) =
13285 {
13286#ifdef AMIGA
13287 "amiga",
13288# ifdef FEAT_ARP
13289 "arp",
13290# endif
13291#endif
13292#ifdef __BEOS__
13293 "beos",
13294#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013295#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 "mac",
13297#endif
13298#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013299 "macunix", /* built with 'darwin' enabled */
13300#endif
13301#if defined(__APPLE__) && __APPLE__ == 1
13302 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#ifdef __QNX__
13305 "qnx",
13306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307#ifdef UNIX
13308 "unix",
13309#endif
13310#ifdef VMS
13311 "vms",
13312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef WIN32
13314 "win32",
13315#endif
13316#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13317 "win32unix",
13318#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013319#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 "win64",
13321#endif
13322#ifdef EBCDIC
13323 "ebcdic",
13324#endif
13325#ifndef CASE_INSENSITIVE_FILENAME
13326 "fname_case",
13327#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013328#ifdef HAVE_ACL
13329 "acl",
13330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331#ifdef FEAT_ARABIC
13332 "arabic",
13333#endif
13334#ifdef FEAT_AUTOCMD
13335 "autocmd",
13336#endif
13337#ifdef FEAT_BEVAL
13338 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013339# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13340 "balloon_multiline",
13341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342#endif
13343#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13344 "builtin_terms",
13345# ifdef ALL_BUILTIN_TCAPS
13346 "all_builtin_terms",
13347# endif
13348#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013349#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13350 || defined(FEAT_GUI_W32) \
13351 || defined(FEAT_GUI_MOTIF))
13352 "browsefilter",
13353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354#ifdef FEAT_BYTEOFF
13355 "byte_offset",
13356#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013357#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013358 "channel",
13359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#ifdef FEAT_CINDENT
13361 "cindent",
13362#endif
13363#ifdef FEAT_CLIENTSERVER
13364 "clientserver",
13365#endif
13366#ifdef FEAT_CLIPBOARD
13367 "clipboard",
13368#endif
13369#ifdef FEAT_CMDL_COMPL
13370 "cmdline_compl",
13371#endif
13372#ifdef FEAT_CMDHIST
13373 "cmdline_hist",
13374#endif
13375#ifdef FEAT_COMMENTS
13376 "comments",
13377#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013378#ifdef FEAT_CONCEAL
13379 "conceal",
13380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381#ifdef FEAT_CRYPT
13382 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013383 "crypt-blowfish",
13384 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385#endif
13386#ifdef FEAT_CSCOPE
13387 "cscope",
13388#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013389#ifdef FEAT_CURSORBIND
13390 "cursorbind",
13391#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013392#ifdef CURSOR_SHAPE
13393 "cursorshape",
13394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395#ifdef DEBUG
13396 "debug",
13397#endif
13398#ifdef FEAT_CON_DIALOG
13399 "dialog_con",
13400#endif
13401#ifdef FEAT_GUI_DIALOG
13402 "dialog_gui",
13403#endif
13404#ifdef FEAT_DIFF
13405 "diff",
13406#endif
13407#ifdef FEAT_DIGRAPHS
13408 "digraphs",
13409#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013410#ifdef FEAT_DIRECTX
13411 "directx",
13412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413#ifdef FEAT_DND
13414 "dnd",
13415#endif
13416#ifdef FEAT_EMACS_TAGS
13417 "emacs_tags",
13418#endif
13419 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013420 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421#ifdef FEAT_SEARCH_EXTRA
13422 "extra_search",
13423#endif
13424#ifdef FEAT_FKMAP
13425 "farsi",
13426#endif
13427#ifdef FEAT_SEARCHPATH
13428 "file_in_path",
13429#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013430#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013431 "filterpipe",
13432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433#ifdef FEAT_FIND_ID
13434 "find_in_path",
13435#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013436#ifdef FEAT_FLOAT
13437 "float",
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef FEAT_FOLDING
13440 "folding",
13441#endif
13442#ifdef FEAT_FOOTER
13443 "footer",
13444#endif
13445#if !defined(USE_SYSTEM) && defined(UNIX)
13446 "fork",
13447#endif
13448#ifdef FEAT_GETTEXT
13449 "gettext",
13450#endif
13451#ifdef FEAT_GUI
13452 "gui",
13453#endif
13454#ifdef FEAT_GUI_ATHENA
13455# ifdef FEAT_GUI_NEXTAW
13456 "gui_neXtaw",
13457# else
13458 "gui_athena",
13459# endif
13460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461#ifdef FEAT_GUI_GTK
13462 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013463# ifdef USE_GTK3
13464 "gui_gtk3",
13465# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013469#ifdef FEAT_GUI_GNOME
13470 "gui_gnome",
13471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472#ifdef FEAT_GUI_MAC
13473 "gui_mac",
13474#endif
13475#ifdef FEAT_GUI_MOTIF
13476 "gui_motif",
13477#endif
13478#ifdef FEAT_GUI_PHOTON
13479 "gui_photon",
13480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481#ifdef FEAT_GUI_W32
13482 "gui_win32",
13483#endif
13484#ifdef FEAT_HANGULIN
13485 "hangul_input",
13486#endif
13487#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13488 "iconv",
13489#endif
13490#ifdef FEAT_INS_EXPAND
13491 "insert_expand",
13492#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013494 "job",
13495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496#ifdef FEAT_JUMPLIST
13497 "jumplist",
13498#endif
13499#ifdef FEAT_KEYMAP
13500 "keymap",
13501#endif
13502#ifdef FEAT_LANGMAP
13503 "langmap",
13504#endif
13505#ifdef FEAT_LIBCALL
13506 "libcall",
13507#endif
13508#ifdef FEAT_LINEBREAK
13509 "linebreak",
13510#endif
13511#ifdef FEAT_LISP
13512 "lispindent",
13513#endif
13514#ifdef FEAT_LISTCMDS
13515 "listcmds",
13516#endif
13517#ifdef FEAT_LOCALMAP
13518 "localmap",
13519#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013520#ifdef FEAT_LUA
13521# ifndef DYNAMIC_LUA
13522 "lua",
13523# endif
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#ifdef FEAT_MENU
13526 "menu",
13527#endif
13528#ifdef FEAT_SESSION
13529 "mksession",
13530#endif
13531#ifdef FEAT_MODIFY_FNAME
13532 "modify_fname",
13533#endif
13534#ifdef FEAT_MOUSE
13535 "mouse",
13536#endif
13537#ifdef FEAT_MOUSESHAPE
13538 "mouseshape",
13539#endif
13540#if defined(UNIX) || defined(VMS)
13541# ifdef FEAT_MOUSE_DEC
13542 "mouse_dec",
13543# endif
13544# ifdef FEAT_MOUSE_GPM
13545 "mouse_gpm",
13546# endif
13547# ifdef FEAT_MOUSE_JSB
13548 "mouse_jsbterm",
13549# endif
13550# ifdef FEAT_MOUSE_NET
13551 "mouse_netterm",
13552# endif
13553# ifdef FEAT_MOUSE_PTERM
13554 "mouse_pterm",
13555# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013556# ifdef FEAT_MOUSE_SGR
13557 "mouse_sgr",
13558# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013559# ifdef FEAT_SYSMOUSE
13560 "mouse_sysmouse",
13561# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013562# ifdef FEAT_MOUSE_URXVT
13563 "mouse_urxvt",
13564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565# ifdef FEAT_MOUSE_XTERM
13566 "mouse_xterm",
13567# endif
13568#endif
13569#ifdef FEAT_MBYTE
13570 "multi_byte",
13571#endif
13572#ifdef FEAT_MBYTE_IME
13573 "multi_byte_ime",
13574#endif
13575#ifdef FEAT_MULTI_LANG
13576 "multi_lang",
13577#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013578#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013579#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013580 "mzscheme",
13581#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583#ifdef FEAT_OLE
13584 "ole",
13585#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013586 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef FEAT_PATH_EXTRA
13588 "path_extra",
13589#endif
13590#ifdef FEAT_PERL
13591#ifndef DYNAMIC_PERL
13592 "perl",
13593#endif
13594#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013595#ifdef FEAT_PERSISTENT_UNDO
13596 "persistent_undo",
13597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#ifdef FEAT_PYTHON
13599#ifndef DYNAMIC_PYTHON
13600 "python",
13601#endif
13602#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013603#ifdef FEAT_PYTHON3
13604#ifndef DYNAMIC_PYTHON3
13605 "python3",
13606#endif
13607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#ifdef FEAT_POSTSCRIPT
13609 "postscript",
13610#endif
13611#ifdef FEAT_PRINTER
13612 "printer",
13613#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013614#ifdef FEAT_PROFILE
13615 "profile",
13616#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013617#ifdef FEAT_RELTIME
13618 "reltime",
13619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620#ifdef FEAT_QUICKFIX
13621 "quickfix",
13622#endif
13623#ifdef FEAT_RIGHTLEFT
13624 "rightleft",
13625#endif
13626#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13627 "ruby",
13628#endif
13629#ifdef FEAT_SCROLLBIND
13630 "scrollbind",
13631#endif
13632#ifdef FEAT_CMDL_INFO
13633 "showcmd",
13634 "cmdline_info",
13635#endif
13636#ifdef FEAT_SIGNS
13637 "signs",
13638#endif
13639#ifdef FEAT_SMARTINDENT
13640 "smartindent",
13641#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013642#ifdef STARTUPTIME
13643 "startuptime",
13644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645#ifdef FEAT_STL_OPT
13646 "statusline",
13647#endif
13648#ifdef FEAT_SUN_WORKSHOP
13649 "sun_workshop",
13650#endif
13651#ifdef FEAT_NETBEANS_INTG
13652 "netbeans_intg",
13653#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013654#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013655 "spell",
13656#endif
13657#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 "syntax",
13659#endif
13660#if defined(USE_SYSTEM) || !defined(UNIX)
13661 "system",
13662#endif
13663#ifdef FEAT_TAG_BINS
13664 "tag_binary",
13665#endif
13666#ifdef FEAT_TAG_OLDSTATIC
13667 "tag_old_static",
13668#endif
13669#ifdef FEAT_TAG_ANYWHITE
13670 "tag_any_white",
13671#endif
13672#ifdef FEAT_TCL
13673# ifndef DYNAMIC_TCL
13674 "tcl",
13675# endif
13676#endif
13677#ifdef TERMINFO
13678 "terminfo",
13679#endif
13680#ifdef FEAT_TERMRESPONSE
13681 "termresponse",
13682#endif
13683#ifdef FEAT_TEXTOBJ
13684 "textobjects",
13685#endif
13686#ifdef HAVE_TGETENT
13687 "tgetent",
13688#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013689#ifdef FEAT_TIMERS
13690 "timers",
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#ifdef FEAT_TITLE
13693 "title",
13694#endif
13695#ifdef FEAT_TOOLBAR
13696 "toolbar",
13697#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013698#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13699 "unnamedplus",
13700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701#ifdef FEAT_USR_CMDS
13702 "user-commands", /* was accidentally included in 5.4 */
13703 "user_commands",
13704#endif
13705#ifdef FEAT_VIMINFO
13706 "viminfo",
13707#endif
13708#ifdef FEAT_VERTSPLIT
13709 "vertsplit",
13710#endif
13711#ifdef FEAT_VIRTUALEDIT
13712 "virtualedit",
13713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715#ifdef FEAT_VISUALEXTRA
13716 "visualextra",
13717#endif
13718#ifdef FEAT_VREPLACE
13719 "vreplace",
13720#endif
13721#ifdef FEAT_WILDIGN
13722 "wildignore",
13723#endif
13724#ifdef FEAT_WILDMENU
13725 "wildmenu",
13726#endif
13727#ifdef FEAT_WINDOWS
13728 "windows",
13729#endif
13730#ifdef FEAT_WAK
13731 "winaltkeys",
13732#endif
13733#ifdef FEAT_WRITEBACKUP
13734 "writebackup",
13735#endif
13736#ifdef FEAT_XIM
13737 "xim",
13738#endif
13739#ifdef FEAT_XFONTSET
13740 "xfontset",
13741#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013742#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013743 "xpm",
13744 "xpm_w32", /* for backward compatibility */
13745#else
13746# if defined(HAVE_XPM)
13747 "xpm",
13748# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#ifdef USE_XSMP
13751 "xsmp",
13752#endif
13753#ifdef USE_XSMP_INTERACT
13754 "xsmp_interact",
13755#endif
13756#ifdef FEAT_XCLIPBOARD
13757 "xterm_clipboard",
13758#endif
13759#ifdef FEAT_XTERM_SAVE
13760 "xterm_save",
13761#endif
13762#if defined(UNIX) && defined(FEAT_X11)
13763 "X11",
13764#endif
13765 NULL
13766 };
13767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 for (i = 0; has_list[i] != NULL; ++i)
13770 if (STRICMP(name, has_list[i]) == 0)
13771 {
13772 n = TRUE;
13773 break;
13774 }
13775
13776 if (n == FALSE)
13777 {
13778 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013779 {
13780 if (name[5] == '-'
13781 && STRLEN(name) > 11
13782 && vim_isdigit(name[6])
13783 && vim_isdigit(name[8])
13784 && vim_isdigit(name[10]))
13785 {
13786 int major = atoi((char *)name + 6);
13787 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013788
13789 /* Expect "patch-9.9.01234". */
13790 n = (major < VIM_VERSION_MAJOR
13791 || (major == VIM_VERSION_MAJOR
13792 && (minor < VIM_VERSION_MINOR
13793 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013794 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013795 }
13796 else
13797 n = has_patch(atoi((char *)name + 5));
13798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799 else if (STRICMP(name, "vim_starting") == 0)
13800 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013801#ifdef FEAT_MBYTE
13802 else if (STRICMP(name, "multi_byte_encoding") == 0)
13803 n = has_mbyte;
13804#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013805#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13806 else if (STRICMP(name, "balloon_multiline") == 0)
13807 n = multiline_balloon_available();
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef DYNAMIC_TCL
13810 else if (STRICMP(name, "tcl") == 0)
13811 n = tcl_enabled(FALSE);
13812#endif
13813#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13814 else if (STRICMP(name, "iconv") == 0)
13815 n = iconv_enabled(FALSE);
13816#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013817#ifdef DYNAMIC_LUA
13818 else if (STRICMP(name, "lua") == 0)
13819 n = lua_enabled(FALSE);
13820#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013821#ifdef DYNAMIC_MZSCHEME
13822 else if (STRICMP(name, "mzscheme") == 0)
13823 n = mzscheme_enabled(FALSE);
13824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef DYNAMIC_RUBY
13826 else if (STRICMP(name, "ruby") == 0)
13827 n = ruby_enabled(FALSE);
13828#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013829#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#ifdef DYNAMIC_PYTHON
13831 else if (STRICMP(name, "python") == 0)
13832 n = python_enabled(FALSE);
13833#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013834#endif
13835#ifdef FEAT_PYTHON3
13836#ifdef DYNAMIC_PYTHON3
13837 else if (STRICMP(name, "python3") == 0)
13838 n = python3_enabled(FALSE);
13839#endif
13840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef DYNAMIC_PERL
13842 else if (STRICMP(name, "perl") == 0)
13843 n = perl_enabled(FALSE);
13844#endif
13845#ifdef FEAT_GUI
13846 else if (STRICMP(name, "gui_running") == 0)
13847 n = (gui.in_use || gui.starting);
13848# ifdef FEAT_GUI_W32
13849 else if (STRICMP(name, "gui_win32s") == 0)
13850 n = gui_is_win32s();
13851# endif
13852# ifdef FEAT_BROWSE
13853 else if (STRICMP(name, "browse") == 0)
13854 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13855# endif
13856#endif
13857#ifdef FEAT_SYN_HL
13858 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013859 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860#endif
13861#if defined(WIN3264)
13862 else if (STRICMP(name, "win95") == 0)
13863 n = mch_windows95();
13864#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013865#ifdef FEAT_NETBEANS_INTG
13866 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013867 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 }
13870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013871 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872}
13873
13874/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013875 * "has_key()" function
13876 */
13877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013878f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013879{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013880 if (argvars[0].v_type != VAR_DICT)
13881 {
13882 EMSG(_(e_dictreq));
13883 return;
13884 }
13885 if (argvars[0].vval.v_dict == NULL)
13886 return;
13887
13888 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013889 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013890}
13891
13892/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013893 * "haslocaldir()" function
13894 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013896f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013897{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013898 win_T *wp = NULL;
13899
13900 wp = find_tabwin(&argvars[0], &argvars[1]);
13901 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013902}
13903
13904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 * "hasmapto()" function
13906 */
13907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013908f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
13910 char_u *name;
13911 char_u *mode;
13912 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013913 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013915 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013916 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917 mode = (char_u *)"nvo";
13918 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013921 if (argvars[2].v_type != VAR_UNKNOWN)
13922 abbr = get_tv_number(&argvars[2]);
13923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar2c932302006-03-18 21:42:09 +000013925 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929}
13930
13931/*
13932 * "histadd()" function
13933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013935f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936{
13937#ifdef FEAT_CMDHIST
13938 int histype;
13939 char_u *str;
13940 char_u buf[NUMBUFLEN];
13941#endif
13942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 if (check_restricted() || check_secure())
13945 return;
13946#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013947 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13948 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 if (histype >= 0)
13950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013951 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 if (*str != NUL)
13953 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013954 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957 return;
13958 }
13959 }
13960#endif
13961}
13962
13963/*
13964 * "histdel()" function
13965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013967f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968{
13969#ifdef FEAT_CMDHIST
13970 int n;
13971 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013972 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13975 if (str == NULL)
13976 n = 0;
13977 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013979 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013980 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 else
13985 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 get_tv_string_buf(&argvars[1], buf));
13988 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989#endif
13990}
13991
13992/*
13993 * "histget()" function
13994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013996f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997{
13998#ifdef FEAT_CMDHIST
13999 int type;
14000 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14004 if (str == NULL)
14005 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 {
14008 type = get_histtype(str);
14009 if (argvars[1].v_type == VAR_UNKNOWN)
14010 idx = get_history_idx(type);
14011 else
14012 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14013 /* -1 on type error */
14014 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020}
14021
14022/*
14023 * "histnr()" function
14024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014026f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027{
14028 int i;
14029
14030#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 char_u *history = get_tv_string_chk(&argvars[0]);
14032
14033 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034 if (i >= HIST_CMD && i < HIST_COUNT)
14035 i = get_history_idx(i);
14036 else
14037#endif
14038 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014039 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040}
14041
14042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 * "highlightID(name)" function
14044 */
14045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014046f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049}
14050
14051/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052 * "highlight_exists()" function
14053 */
14054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014056{
14057 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14058}
14059
14060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 * "hostname()" function
14062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014064f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065{
14066 char_u hostname[256];
14067
14068 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 rettv->v_type = VAR_STRING;
14070 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071}
14072
14073/*
14074 * iconv() function
14075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014077f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078{
14079#ifdef FEAT_MBYTE
14080 char_u buf1[NUMBUFLEN];
14081 char_u buf2[NUMBUFLEN];
14082 char_u *from, *to, *str;
14083 vimconv_T vimconv;
14084#endif
14085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 rettv->v_type = VAR_STRING;
14087 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088
14089#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014090 str = get_tv_string(&argvars[0]);
14091 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14092 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 vimconv.vc_type = CONV_NONE;
14094 convert_setup(&vimconv, from, to);
14095
14096 /* If the encodings are equal, no conversion needed. */
14097 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101
14102 convert_setup(&vimconv, NULL, NULL);
14103 vim_free(from);
14104 vim_free(to);
14105#endif
14106}
14107
14108/*
14109 * "indent()" function
14110 */
14111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014112f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113{
14114 linenr_T lnum;
14115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121}
14122
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014123/*
14124 * "index()" function
14125 */
14126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014127f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014128{
Bram Moolenaar33570922005-01-25 22:26:29 +000014129 list_T *l;
14130 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014131 long idx = 0;
14132 int ic = FALSE;
14133
14134 rettv->vval.v_number = -1;
14135 if (argvars[0].v_type != VAR_LIST)
14136 {
14137 EMSG(_(e_listreq));
14138 return;
14139 }
14140 l = argvars[0].vval.v_list;
14141 if (l != NULL)
14142 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014143 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014144 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 int error = FALSE;
14147
Bram Moolenaar758711c2005-02-02 23:11:38 +000014148 /* Start at specified item. Use the cached index that list_find()
14149 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014151 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014152 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 ic = get_tv_number_chk(&argvars[3], &error);
14154 if (error)
14155 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014156 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014157
Bram Moolenaar758711c2005-02-02 23:11:38 +000014158 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014159 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014160 {
14161 rettv->vval.v_number = idx;
14162 break;
14163 }
14164 }
14165}
14166
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167static int inputsecret_flag = 0;
14168
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014169static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014170
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014172 * This function is used by f_input() and f_inputdialog() functions. The third
14173 * argument to f_input() specifies the type of completion to use at the
14174 * prompt. The third argument to f_inputdialog() specifies the value to return
14175 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 */
14177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014178get_user_input(
14179 typval_T *argvars,
14180 typval_T *rettv,
14181 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 char_u *p = NULL;
14185 int c;
14186 char_u buf[NUMBUFLEN];
14187 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014189 int xp_type = EXPAND_NOTHING;
14190 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014192 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014193 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194
14195#ifdef NO_CONSOLE_INPUT
14196 /* While starting up, there is no place to enter text. */
14197 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199#endif
14200
14201 cmd_silent = FALSE; /* Want to see the prompt. */
14202 if (prompt != NULL)
14203 {
14204 /* Only the part of the message after the last NL is considered as
14205 * prompt for the command line */
14206 p = vim_strrchr(prompt, '\n');
14207 if (p == NULL)
14208 p = prompt;
14209 else
14210 {
14211 ++p;
14212 c = *p;
14213 *p = NUL;
14214 msg_start();
14215 msg_clr_eos();
14216 msg_puts_attr(prompt, echo_attr);
14217 msg_didout = FALSE;
14218 msg_starthere();
14219 *p = c;
14220 }
14221 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 if (argvars[1].v_type != VAR_UNKNOWN)
14224 {
14225 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14226 if (defstr != NULL)
14227 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014229 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014230 {
14231 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014232 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014233 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014234
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014235 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014236 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014237
Bram Moolenaar4463f292005-09-25 22:20:24 +000014238 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14239 if (xp_name == NULL)
14240 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014242 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014243
Bram Moolenaar4463f292005-09-25 22:20:24 +000014244 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14245 &xp_arg) == FAIL)
14246 return;
14247 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014248 }
14249
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014250 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014251 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014252 int save_ex_normal_busy = ex_normal_busy;
14253 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014254 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014255 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14256 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014257 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014258 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014259 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014260 && argvars[1].v_type != VAR_UNKNOWN
14261 && argvars[2].v_type != VAR_UNKNOWN)
14262 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14263 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014264
14265 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 /* since the user typed this, no need to wait for return */
14268 need_wait_return = FALSE;
14269 msg_didout = FALSE;
14270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 cmd_silent = cmd_silent_save;
14272}
14273
14274/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014275 * "input()" function
14276 * Also handles inputsecret() when inputsecret is set.
14277 */
14278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014280{
14281 get_user_input(argvars, rettv, FALSE);
14282}
14283
14284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 * "inputdialog()" function
14286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289{
14290#if defined(FEAT_GUI_TEXTDIALOG)
14291 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14292 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14293 {
14294 char_u *message;
14295 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 message = get_tv_string_chk(&argvars[0]);
14299 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014300 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014301 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302 else
14303 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 if (message != NULL && defstr != NULL
14305 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014306 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 else
14309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 if (message != NULL && defstr != NULL
14311 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014312 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_string = vim_strsave(
14314 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 }
14320 else
14321#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014322 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323}
14324
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014325/*
14326 * "inputlist()" function
14327 */
14328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014329f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014330{
14331 listitem_T *li;
14332 int selected;
14333 int mouse_used;
14334
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014335#ifdef NO_CONSOLE_INPUT
14336 /* While starting up, there is no place to enter text. */
14337 if (no_console_input())
14338 return;
14339#endif
14340 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14341 {
14342 EMSG2(_(e_listarg), "inputlist()");
14343 return;
14344 }
14345
14346 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014347 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014348 lines_left = Rows; /* avoid more prompt */
14349 msg_scroll = TRUE;
14350 msg_clr_eos();
14351
14352 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14353 {
14354 msg_puts(get_tv_string(&li->li_tv));
14355 msg_putchar('\n');
14356 }
14357
14358 /* Ask for choice. */
14359 selected = prompt_for_number(&mouse_used);
14360 if (mouse_used)
14361 selected -= lines_left;
14362
14363 rettv->vval.v_number = selected;
14364}
14365
14366
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14368
14369/*
14370 * "inputrestore()" function
14371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375 if (ga_userinput.ga_len > 0)
14376 {
14377 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14379 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014380 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 }
14382 else if (p_verbose > 1)
14383 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014384 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 }
14387}
14388
14389/*
14390 * "inputsave()" function
14391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014393f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396 if (ga_grow(&ga_userinput, 1) == OK)
14397 {
14398 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14399 + ga_userinput.ga_len);
14400 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014401 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 }
14403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405}
14406
14407/*
14408 * "inputsecret()" function
14409 */
14410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014411f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
14413 ++cmdline_star;
14414 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 --cmdline_star;
14417 --inputsecret_flag;
14418}
14419
14420/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421 * "insert()" function
14422 */
14423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014424f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014425{
14426 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014427 listitem_T *item;
14428 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014430
14431 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014433 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014434 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435 {
14436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 before = get_tv_number_chk(&argvars[2], &error);
14438 if (error)
14439 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014440
Bram Moolenaar758711c2005-02-02 23:11:38 +000014441 if (before == l->lv_len)
14442 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014443 else
14444 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014445 item = list_find(l, before);
14446 if (item == NULL)
14447 {
14448 EMSGN(_(e_listidx), before);
14449 l = NULL;
14450 }
14451 }
14452 if (l != NULL)
14453 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014454 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014455 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014456 }
14457 }
14458}
14459
14460/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014461 * "invert(expr)" function
14462 */
14463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014465{
14466 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14467}
14468
14469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 * "isdirectory()" function
14471 */
14472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014473f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476}
14477
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014478/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014479 * "islocked()" function
14480 */
14481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014482f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014483{
14484 lval_T lv;
14485 char_u *end;
14486 dictitem_T *di;
14487
14488 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014489 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14490 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014491 if (end != NULL && lv.ll_name != NULL)
14492 {
14493 if (*end != NUL)
14494 EMSG(_(e_trailing));
14495 else
14496 {
14497 if (lv.ll_tv == NULL)
14498 {
14499 if (check_changedtick(lv.ll_name))
14500 rettv->vval.v_number = 1; /* always locked */
14501 else
14502 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014503 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014504 if (di != NULL)
14505 {
14506 /* Consider a variable locked when:
14507 * 1. the variable itself is locked
14508 * 2. the value of the variable is locked.
14509 * 3. the List or Dict value is locked.
14510 */
14511 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14512 || tv_islocked(&di->di_tv));
14513 }
14514 }
14515 }
14516 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014517 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014518 else if (lv.ll_newkey != NULL)
14519 EMSG2(_(e_dictkey), lv.ll_newkey);
14520 else if (lv.ll_list != NULL)
14521 /* List item. */
14522 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14523 else
14524 /* Dictionary item. */
14525 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14526 }
14527 }
14528
14529 clear_lval(&lv);
14530}
14531
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014532#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14533/*
14534 * "isnan()" function
14535 */
14536 static void
14537f_isnan(typval_T *argvars, typval_T *rettv)
14538{
14539 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14540 && isnan(argvars[0].vval.v_float);
14541}
14542#endif
14543
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014544static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014545
14546/*
14547 * Turn a dict into a list:
14548 * "what" == 0: list of keys
14549 * "what" == 1: list of values
14550 * "what" == 2: list of items
14551 */
14552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014553dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014554{
Bram Moolenaar33570922005-01-25 22:26:29 +000014555 list_T *l2;
14556 dictitem_T *di;
14557 hashitem_T *hi;
14558 listitem_T *li;
14559 listitem_T *li2;
14560 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014561 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014562
Bram Moolenaar8c711452005-01-14 21:53:12 +000014563 if (argvars[0].v_type != VAR_DICT)
14564 {
14565 EMSG(_(e_dictreq));
14566 return;
14567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014568 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569 return;
14570
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014571 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014572 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014574 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014575 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014577 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014579 --todo;
14580 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014582 li = listitem_alloc();
14583 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014584 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014585 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014587 if (what == 0)
14588 {
14589 /* keys() */
14590 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014591 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014592 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14593 }
14594 else if (what == 1)
14595 {
14596 /* values() */
14597 copy_tv(&di->di_tv, &li->li_tv);
14598 }
14599 else
14600 {
14601 /* items() */
14602 l2 = list_alloc();
14603 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014604 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014605 li->li_tv.vval.v_list = l2;
14606 if (l2 == NULL)
14607 break;
14608 ++l2->lv_refcount;
14609
14610 li2 = listitem_alloc();
14611 if (li2 == NULL)
14612 break;
14613 list_append(l2, li2);
14614 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014615 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014616 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14617
14618 li2 = listitem_alloc();
14619 if (li2 == NULL)
14620 break;
14621 list_append(l2, li2);
14622 copy_tv(&di->di_tv, &li2->li_tv);
14623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014624 }
14625 }
14626}
14627
14628/*
14629 * "items(dict)" function
14630 */
14631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014632f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014633{
14634 dict_list(argvars, rettv, 2);
14635}
14636
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014637#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014638/*
14639 * Get the job from the argument.
14640 * Returns NULL if the job is invalid.
14641 */
14642 static job_T *
14643get_job_arg(typval_T *tv)
14644{
14645 job_T *job;
14646
14647 if (tv->v_type != VAR_JOB)
14648 {
14649 EMSG2(_(e_invarg2), get_tv_string(tv));
14650 return NULL;
14651 }
14652 job = tv->vval.v_job;
14653
14654 if (job == NULL)
14655 EMSG(_("E916: not a valid job"));
14656 return job;
14657}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014658
Bram Moolenaar835dc632016-02-07 14:27:38 +010014659/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014660 * "job_getchannel()" function
14661 */
14662 static void
14663f_job_getchannel(typval_T *argvars, typval_T *rettv)
14664{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014665 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014666
Bram Moolenaar65edff82016-02-21 16:40:11 +010014667 if (job != NULL)
14668 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014669 rettv->v_type = VAR_CHANNEL;
14670 rettv->vval.v_channel = job->jv_channel;
14671 if (job->jv_channel != NULL)
14672 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014673 }
14674}
14675
14676/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014677 * "job_info()" function
14678 */
14679 static void
14680f_job_info(typval_T *argvars, typval_T *rettv)
14681{
14682 job_T *job = get_job_arg(&argvars[0]);
14683
14684 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14685 job_info(job, rettv->vval.v_dict);
14686}
14687
14688/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014689 * "job_setoptions()" function
14690 */
14691 static void
14692f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14693{
14694 job_T *job = get_job_arg(&argvars[0]);
14695 jobopt_T opt;
14696
14697 if (job == NULL)
14698 return;
14699 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014700 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014701 return;
14702 job_set_options(job, &opt);
14703}
14704
14705/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014706 * "job_start()" function
14707 */
14708 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014709f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014710{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014711 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014712 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014713}
14714
14715/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014716 * "job_status()" function
14717 */
14718 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014719f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014720{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014721 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014722
Bram Moolenaar65edff82016-02-21 16:40:11 +010014723 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014724 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014725 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014726 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014727 }
14728}
14729
14730/*
14731 * "job_stop()" function
14732 */
14733 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014734f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014735{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014736 job_T *job = get_job_arg(&argvars[0]);
14737
14738 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014739 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014740}
14741#endif
14742
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014744 * "join()" function
14745 */
14746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014747f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014748{
14749 garray_T ga;
14750 char_u *sep;
14751
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014752 if (argvars[0].v_type != VAR_LIST)
14753 {
14754 EMSG(_(e_listreq));
14755 return;
14756 }
14757 if (argvars[0].vval.v_list == NULL)
14758 return;
14759 if (argvars[1].v_type == VAR_UNKNOWN)
14760 sep = (char_u *)" ";
14761 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014762 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014763
14764 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014765
14766 if (sep != NULL)
14767 {
14768 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014769 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770 ga_append(&ga, NUL);
14771 rettv->vval.v_string = (char_u *)ga.ga_data;
14772 }
14773 else
14774 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014775}
14776
14777/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014778 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014779 */
14780 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014781f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014782{
14783 js_read_T reader;
14784
14785 reader.js_buf = get_tv_string(&argvars[0]);
14786 reader.js_fill = NULL;
14787 reader.js_used = 0;
14788 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14789 EMSG(_(e_invarg));
14790}
14791
14792/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014793 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014794 */
14795 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014796f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014797{
14798 rettv->v_type = VAR_STRING;
14799 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14800}
14801
14802/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014803 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014804 */
14805 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014806f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014807{
14808 js_read_T reader;
14809
14810 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014811 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014812 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014813 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014814 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014815}
14816
14817/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014818 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014819 */
14820 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014821f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014822{
14823 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014824 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014825}
14826
14827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014828 * "keys()" function
14829 */
14830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014832{
14833 dict_list(argvars, rettv, 0);
14834}
14835
14836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 * "last_buffer_nr()" function.
14838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014840f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841{
14842 int n = 0;
14843 buf_T *buf;
14844
14845 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14846 if (n < buf->b_fnum)
14847 n = buf->b_fnum;
14848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014849 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850}
14851
14852/*
14853 * "len()" function
14854 */
14855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014856f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014857{
14858 switch (argvars[0].v_type)
14859 {
14860 case VAR_STRING:
14861 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->vval.v_number = (varnumber_T)STRLEN(
14863 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014864 break;
14865 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014866 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014867 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014868 case VAR_DICT:
14869 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14870 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014871 case VAR_UNKNOWN:
14872 case VAR_SPECIAL:
14873 case VAR_FLOAT:
14874 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014875 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014876 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014877 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014878 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014879 break;
14880 }
14881}
14882
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014883static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014884
14885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014886libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014887{
14888#ifdef FEAT_LIBCALL
14889 char_u *string_in;
14890 char_u **string_result;
14891 int nr_result;
14892#endif
14893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014895 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014896 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014897
14898 if (check_restricted() || check_secure())
14899 return;
14900
14901#ifdef FEAT_LIBCALL
14902 /* The first two args must be strings, otherwise its meaningless */
14903 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014905 string_in = NULL;
14906 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014907 string_in = argvars[2].vval.v_string;
14908 if (type == VAR_NUMBER)
14909 string_result = NULL;
14910 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014912 if (mch_libcall(argvars[0].vval.v_string,
14913 argvars[1].vval.v_string,
14914 string_in,
14915 argvars[2].vval.v_number,
14916 string_result,
14917 &nr_result) == OK
14918 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014919 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014920 }
14921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922}
14923
14924/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014925 * "libcall()" function
14926 */
14927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014928f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014929{
14930 libcall_common(argvars, rettv, VAR_STRING);
14931}
14932
14933/*
14934 * "libcallnr()" function
14935 */
14936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014937f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938{
14939 libcall_common(argvars, rettv, VAR_NUMBER);
14940}
14941
14942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 * "line(string)" function
14944 */
14945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014946f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947{
14948 linenr_T lnum = 0;
14949 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014950 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014952 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 if (fp != NULL)
14954 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956}
14957
14958/*
14959 * "line2byte(lnum)" function
14960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014962f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963{
14964#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966#else
14967 linenr_T lnum;
14968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014969 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14974 if (rettv->vval.v_number >= 0)
14975 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976#endif
14977}
14978
14979/*
14980 * "lispindent(lnum)" function
14981 */
14982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014983f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984{
14985#ifdef FEAT_LISP
14986 pos_T pos;
14987 linenr_T lnum;
14988
14989 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14992 {
14993 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 curwin->w_cursor = pos;
14996 }
14997 else
14998#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000}
15001
15002/*
15003 * "localtime()" function
15004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009}
15010
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015011static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015012
15013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015014get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015{
15016 char_u *keys;
15017 char_u *which;
15018 char_u buf[NUMBUFLEN];
15019 char_u *keys_buf = NULL;
15020 char_u *rhs;
15021 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015022 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015023 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015024 mapblock_T *mp;
15025 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026
15027 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->v_type = VAR_STRING;
15029 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 if (*keys == NUL)
15033 return;
15034
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015035 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015036 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015037 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015039 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015040 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015041 if (argvars[3].v_type != VAR_UNKNOWN)
15042 get_dict = get_tv_number(&argvars[3]);
15043 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 else
15046 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 if (which == NULL)
15048 return;
15049
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 mode = get_map_mode(&which, 0);
15051
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015052 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015053 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015055
15056 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015058 /* Return a string. */
15059 if (rhs != NULL)
15060 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaarbd743252010-10-20 21:23:33 +020015062 }
15063 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15064 {
15065 /* Return a dictionary. */
15066 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15067 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15068 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069
Bram Moolenaarbd743252010-10-20 21:23:33 +020015070 dict_add_nr_str(dict, "lhs", 0L, lhs);
15071 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15072 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15073 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15074 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15075 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15076 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015077 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015078 dict_add_nr_str(dict, "mode", 0L, mapmode);
15079
15080 vim_free(lhs);
15081 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 }
15083}
15084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015085#ifdef FEAT_FLOAT
15086/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015087 * "log()" function
15088 */
15089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015090f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015091{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015092 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015093
15094 rettv->v_type = VAR_FLOAT;
15095 if (get_float_arg(argvars, &f) == OK)
15096 rettv->vval.v_float = log(f);
15097 else
15098 rettv->vval.v_float = 0.0;
15099}
15100
15101/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015102 * "log10()" function
15103 */
15104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015105f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015106{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015107 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015108
15109 rettv->v_type = VAR_FLOAT;
15110 if (get_float_arg(argvars, &f) == OK)
15111 rettv->vval.v_float = log10(f);
15112 else
15113 rettv->vval.v_float = 0.0;
15114}
15115#endif
15116
Bram Moolenaar1dced572012-04-05 16:54:08 +020015117#ifdef FEAT_LUA
15118/*
15119 * "luaeval()" function
15120 */
15121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015122f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015123{
15124 char_u *str;
15125 char_u buf[NUMBUFLEN];
15126
15127 str = get_tv_string_buf(&argvars[0], buf);
15128 do_luaeval(str, argvars + 1, rettv);
15129}
15130#endif
15131
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015133 * "map()" function
15134 */
15135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015136f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137{
15138 filter_map(argvars, rettv, TRUE);
15139}
15140
15141/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015143 */
15144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015145f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015147 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148}
15149
15150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015151 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 */
15153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015154f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157}
15158
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015159static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160
15161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015162find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015164 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015165 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015166 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 char_u *pat;
15168 regmatch_T regmatch;
15169 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015170 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 char_u *save_cpo;
15172 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015173 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015174 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015175 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 list_T *l = NULL;
15177 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015178 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015179 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180
15181 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15182 save_cpo = p_cpo;
15183 p_cpo = (char_u *)"";
15184
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015185 rettv->vval.v_number = -1;
15186 if (type == 3)
15187 {
15188 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015190 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015191 }
15192 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 rettv->v_type = VAR_STRING;
15195 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198 if (argvars[0].v_type == VAR_LIST)
15199 {
15200 if ((l = argvars[0].vval.v_list) == NULL)
15201 goto theend;
15202 li = l->lv_first;
15203 }
15204 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015205 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015206 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015207 len = (long)STRLEN(str);
15208 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15211 if (pat == NULL)
15212 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015214 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 int error = FALSE;
15217
15218 start = get_tv_number_chk(&argvars[2], &error);
15219 if (error)
15220 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015221 if (l != NULL)
15222 {
15223 li = list_find(l, start);
15224 if (li == NULL)
15225 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015226 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015227 }
15228 else
15229 {
15230 if (start < 0)
15231 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015232 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015233 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015234 /* When "count" argument is there ignore matches before "start",
15235 * otherwise skip part of the string. Differs when pattern is "^"
15236 * or "\<". */
15237 if (argvars[3].v_type != VAR_UNKNOWN)
15238 startcol = start;
15239 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015240 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015241 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015242 len -= start;
15243 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015244 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015247 nth = get_tv_number_chk(&argvars[3], &error);
15248 if (error)
15249 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 }
15251
15252 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15253 if (regmatch.regprog != NULL)
15254 {
15255 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015256
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015257 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015258 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015259 if (l != NULL)
15260 {
15261 if (li == NULL)
15262 {
15263 match = FALSE;
15264 break;
15265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015266 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015267 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015268 if (str == NULL)
15269 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015270 }
15271
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015272 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015274 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015275 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 if (l == NULL && !match)
15277 break;
15278
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015279 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015280 if (l != NULL)
15281 {
15282 li = li->li_next;
15283 ++idx;
15284 }
15285 else
15286 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015287#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015288 startcol = (colnr_T)(regmatch.startp[0]
15289 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015290#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015291 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015292#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015293 if (startcol > (colnr_T)len
15294 || str + startcol <= regmatch.startp[0])
15295 {
15296 match = FALSE;
15297 break;
15298 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015300 }
15301
15302 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015304 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015306 int i;
15307
15308 /* return list with matched string and submatches */
15309 for (i = 0; i < NSUBEXP; ++i)
15310 {
15311 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015312 {
15313 if (list_append_string(rettv->vval.v_list,
15314 (char_u *)"", 0) == FAIL)
15315 break;
15316 }
15317 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015318 regmatch.startp[i],
15319 (int)(regmatch.endp[i] - regmatch.startp[i]))
15320 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015322 }
15323 }
15324 else if (type == 2)
15325 {
15326 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015327 if (l != NULL)
15328 copy_tv(&li->li_tv, rettv);
15329 else
15330 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015331 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015332 }
15333 else if (l != NULL)
15334 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 else
15336 {
15337 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 (varnumber_T)(regmatch.startp[0] - str);
15340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015343 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 }
15345 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015346 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 }
15348
15349theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 p_cpo = save_cpo;
15352}
15353
15354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355 * "match()" function
15356 */
15357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015358f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359{
15360 find_some_match(argvars, rettv, 1);
15361}
15362
15363/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015364 * "matchadd()" function
15365 */
15366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015367f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015368{
15369#ifdef FEAT_SEARCH_EXTRA
15370 char_u buf[NUMBUFLEN];
15371 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15372 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15373 int prio = 10; /* default priority */
15374 int id = -1;
15375 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015376 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015377
15378 rettv->vval.v_number = -1;
15379
15380 if (grp == NULL || pat == NULL)
15381 return;
15382 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015383 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015384 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015385 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015386 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015387 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015388 if (argvars[4].v_type != VAR_UNKNOWN)
15389 {
15390 if (argvars[4].v_type != VAR_DICT)
15391 {
15392 EMSG(_(e_dictreq));
15393 return;
15394 }
15395 if (dict_find(argvars[4].vval.v_dict,
15396 (char_u *)"conceal", -1) != NULL)
15397 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15398 (char_u *)"conceal", FALSE);
15399 }
15400 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015401 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015402 if (error == TRUE)
15403 return;
15404 if (id >= 1 && id <= 3)
15405 {
15406 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15407 return;
15408 }
15409
Bram Moolenaar6561d522015-07-21 15:48:27 +020015410 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15411 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015412#endif
15413}
15414
15415/*
15416 * "matchaddpos()" function
15417 */
15418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015419f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015420{
15421#ifdef FEAT_SEARCH_EXTRA
15422 char_u buf[NUMBUFLEN];
15423 char_u *group;
15424 int prio = 10;
15425 int id = -1;
15426 int error = FALSE;
15427 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015428 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015429
15430 rettv->vval.v_number = -1;
15431
15432 group = get_tv_string_buf_chk(&argvars[0], buf);
15433 if (group == NULL)
15434 return;
15435
15436 if (argvars[1].v_type != VAR_LIST)
15437 {
15438 EMSG2(_(e_listarg), "matchaddpos()");
15439 return;
15440 }
15441 l = argvars[1].vval.v_list;
15442 if (l == NULL)
15443 return;
15444
15445 if (argvars[2].v_type != VAR_UNKNOWN)
15446 {
15447 prio = get_tv_number_chk(&argvars[2], &error);
15448 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015449 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015450 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015451 if (argvars[4].v_type != VAR_UNKNOWN)
15452 {
15453 if (argvars[4].v_type != VAR_DICT)
15454 {
15455 EMSG(_(e_dictreq));
15456 return;
15457 }
15458 if (dict_find(argvars[4].vval.v_dict,
15459 (char_u *)"conceal", -1) != NULL)
15460 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15461 (char_u *)"conceal", FALSE);
15462 }
15463 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015464 }
15465 if (error == TRUE)
15466 return;
15467
15468 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15469 if (id == 1 || id == 2)
15470 {
15471 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15472 return;
15473 }
15474
Bram Moolenaar6561d522015-07-21 15:48:27 +020015475 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15476 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015477#endif
15478}
15479
15480/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015481 * "matcharg()" function
15482 */
15483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015484f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015485{
15486 if (rettv_list_alloc(rettv) == OK)
15487 {
15488#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015489 int id = get_tv_number(&argvars[0]);
15490 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015491
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015492 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015493 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015494 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15495 {
15496 list_append_string(rettv->vval.v_list,
15497 syn_id2name(m->hlg_id), -1);
15498 list_append_string(rettv->vval.v_list, m->pattern, -1);
15499 }
15500 else
15501 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015502 list_append_string(rettv->vval.v_list, NULL, -1);
15503 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015504 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015505 }
15506#endif
15507 }
15508}
15509
15510/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015511 * "matchdelete()" function
15512 */
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015515{
15516#ifdef FEAT_SEARCH_EXTRA
15517 rettv->vval.v_number = match_delete(curwin,
15518 (int)get_tv_number(&argvars[0]), TRUE);
15519#endif
15520}
15521
15522/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 * "matchend()" function
15524 */
15525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015527{
15528 find_some_match(argvars, rettv, 0);
15529}
15530
15531/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015532 * "matchlist()" function
15533 */
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015536{
15537 find_some_match(argvars, rettv, 3);
15538}
15539
15540/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015541 * "matchstr()" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015545{
15546 find_some_match(argvars, rettv, 2);
15547}
15548
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015549static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015550
15551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015552max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015553{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015554 long n = 0;
15555 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015556 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015557
15558 if (argvars[0].v_type == VAR_LIST)
15559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015560 list_T *l;
15561 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015562
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015563 l = argvars[0].vval.v_list;
15564 if (l != NULL)
15565 {
15566 li = l->lv_first;
15567 if (li != NULL)
15568 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015570 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015571 {
15572 li = li->li_next;
15573 if (li == NULL)
15574 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015575 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015576 if (domax ? i > n : i < n)
15577 n = i;
15578 }
15579 }
15580 }
15581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015582 else if (argvars[0].v_type == VAR_DICT)
15583 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015585 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015586 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015587 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015588
15589 d = argvars[0].vval.v_dict;
15590 if (d != NULL)
15591 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015592 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015594 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015595 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015596 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015597 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015598 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015599 if (first)
15600 {
15601 n = i;
15602 first = FALSE;
15603 }
15604 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015605 n = i;
15606 }
15607 }
15608 }
15609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015610 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015611 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015613}
15614
15615/*
15616 * "max()" function
15617 */
15618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015619f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015620{
15621 max_min(argvars, rettv, TRUE);
15622}
15623
15624/*
15625 * "min()" function
15626 */
15627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015629{
15630 max_min(argvars, rettv, FALSE);
15631}
15632
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015633static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015634
15635/*
15636 * Create the directory in which "dir" is located, and higher levels when
15637 * needed.
15638 */
15639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015640mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015641{
15642 char_u *p;
15643 char_u *updir;
15644 int r = FAIL;
15645
15646 /* Get end of directory name in "dir".
15647 * We're done when it's "/" or "c:/". */
15648 p = gettail_sep(dir);
15649 if (p <= get_past_head(dir))
15650 return OK;
15651
15652 /* If the directory exists we're done. Otherwise: create it.*/
15653 updir = vim_strnsave(dir, (int)(p - dir));
15654 if (updir == NULL)
15655 return FAIL;
15656 if (mch_isdir(updir))
15657 r = OK;
15658 else if (mkdir_recurse(updir, prot) == OK)
15659 r = vim_mkdir_emsg(updir, prot);
15660 vim_free(updir);
15661 return r;
15662}
15663
15664#ifdef vim_mkdir
15665/*
15666 * "mkdir()" function
15667 */
15668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015669f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015670{
15671 char_u *dir;
15672 char_u buf[NUMBUFLEN];
15673 int prot = 0755;
15674
15675 rettv->vval.v_number = FAIL;
15676 if (check_restricted() || check_secure())
15677 return;
15678
15679 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015680 if (*dir == NUL)
15681 rettv->vval.v_number = FAIL;
15682 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015683 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015684 if (*gettail(dir) == NUL)
15685 /* remove trailing slashes */
15686 *gettail_sep(dir) = NUL;
15687
15688 if (argvars[1].v_type != VAR_UNKNOWN)
15689 {
15690 if (argvars[2].v_type != VAR_UNKNOWN)
15691 prot = get_tv_number_chk(&argvars[2], NULL);
15692 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15693 mkdir_recurse(dir, prot);
15694 }
15695 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015696 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015697}
15698#endif
15699
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 * "mode()" function
15702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015704f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015706 char_u buf[3];
15707
15708 buf[1] = NUL;
15709 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 if (VIsual_active)
15712 {
15713 if (VIsual_select)
15714 buf[0] = VIsual_mode + 's' - 'v';
15715 else
15716 buf[0] = VIsual_mode;
15717 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015718 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015719 || State == CONFIRM)
15720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015722 if (State == ASKMORE)
15723 buf[1] = 'm';
15724 else if (State == CONFIRM)
15725 buf[1] = '?';
15726 }
15727 else if (State == EXTERNCMD)
15728 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 else if (State & INSERT)
15730 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015731#ifdef FEAT_VREPLACE
15732 if (State & VREPLACE_FLAG)
15733 {
15734 buf[0] = 'R';
15735 buf[1] = 'v';
15736 }
15737 else
15738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 if (State & REPLACE_FLAG)
15740 buf[0] = 'R';
15741 else
15742 buf[0] = 'i';
15743 }
15744 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747 if (exmode_active)
15748 buf[1] = 'v';
15749 }
15750 else if (exmode_active)
15751 {
15752 buf[0] = 'c';
15753 buf[1] = 'e';
15754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015758 if (finish_op)
15759 buf[1] = 'o';
15760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015762 /* Clear out the minor mode when the argument is not a non-zero number or
15763 * non-empty string. */
15764 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015765 buf[1] = NUL;
15766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015767 rettv->vval.v_string = vim_strsave(buf);
15768 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769}
15770
Bram Moolenaar429fa852013-04-15 12:27:36 +020015771#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015772/*
15773 * "mzeval()" function
15774 */
15775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015776f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015777{
15778 char_u *str;
15779 char_u buf[NUMBUFLEN];
15780
15781 str = get_tv_string_buf(&argvars[0], buf);
15782 do_mzeval(str, rettv);
15783}
Bram Moolenaar75676462013-01-30 14:55:42 +010015784
15785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015786mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015787{
15788 typval_T argvars[3];
15789
15790 argvars[0].v_type = VAR_STRING;
15791 argvars[0].vval.v_string = name;
15792 copy_tv(args, &argvars[1]);
15793 argvars[2].v_type = VAR_UNKNOWN;
15794 f_call(argvars, rettv);
15795 clear_tv(&argvars[1]);
15796}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015797#endif
15798
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015800 * "nextnonblank()" function
15801 */
15802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015803f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804{
15805 linenr_T lnum;
15806
15807 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810 {
15811 lnum = 0;
15812 break;
15813 }
15814 if (*skipwhite(ml_get(lnum)) != NUL)
15815 break;
15816 }
15817 rettv->vval.v_number = lnum;
15818}
15819
15820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 * "nr2char()" function
15822 */
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825{
15826 char_u buf[NUMBUFLEN];
15827
15828#ifdef FEAT_MBYTE
15829 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015830 {
15831 int utf8 = 0;
15832
15833 if (argvars[1].v_type != VAR_UNKNOWN)
15834 utf8 = get_tv_number_chk(&argvars[1], NULL);
15835 if (utf8)
15836 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15837 else
15838 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840 else
15841#endif
15842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015843 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 buf[1] = NUL;
15845 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846 rettv->v_type = VAR_STRING;
15847 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015848}
15849
15850/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015851 * "or(expr, expr)" function
15852 */
15853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015854f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015855{
15856 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15857 | get_tv_number_chk(&argvars[1], NULL);
15858}
15859
15860/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015861 * "pathshorten()" function
15862 */
15863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015864f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015865{
15866 char_u *p;
15867
15868 rettv->v_type = VAR_STRING;
15869 p = get_tv_string_chk(&argvars[0]);
15870 if (p == NULL)
15871 rettv->vval.v_string = NULL;
15872 else
15873 {
15874 p = vim_strsave(p);
15875 rettv->vval.v_string = p;
15876 if (p != NULL)
15877 shorten_dir(p);
15878 }
15879}
15880
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015881#ifdef FEAT_PERL
15882/*
15883 * "perleval()" function
15884 */
15885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015886f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015887{
15888 char_u *str;
15889 char_u buf[NUMBUFLEN];
15890
15891 str = get_tv_string_buf(&argvars[0], buf);
15892 do_perleval(str, rettv);
15893}
15894#endif
15895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015896#ifdef FEAT_FLOAT
15897/*
15898 * "pow()" function
15899 */
15900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015901f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015902{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015903 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015904
15905 rettv->v_type = VAR_FLOAT;
15906 if (get_float_arg(argvars, &fx) == OK
15907 && get_float_arg(&argvars[1], &fy) == OK)
15908 rettv->vval.v_float = pow(fx, fy);
15909 else
15910 rettv->vval.v_float = 0.0;
15911}
15912#endif
15913
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 * "prevnonblank()" function
15916 */
15917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015918f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919{
15920 linenr_T lnum;
15921
15922 lnum = get_tv_lnum(argvars);
15923 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15924 lnum = 0;
15925 else
15926 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15927 --lnum;
15928 rettv->vval.v_number = lnum;
15929}
15930
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015931/* This dummy va_list is here because:
15932 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15933 * - locally in the function results in a "used before set" warning
15934 * - using va_start() to initialize it gives "function with fixed args" error */
15935static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015936
Bram Moolenaar8c711452005-01-14 21:53:12 +000015937/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015938 * "printf()" function
15939 */
15940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015942{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015943 char_u buf[NUMBUFLEN];
15944 int len;
15945 char_u *s;
15946 int saved_did_emsg = did_emsg;
15947 char *fmt;
15948
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015949 rettv->v_type = VAR_STRING;
15950 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015951
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015952 /* Get the required length, allocate the buffer and do it for real. */
15953 did_emsg = FALSE;
15954 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15955 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15956 if (!did_emsg)
15957 {
15958 s = alloc(len + 1);
15959 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015960 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015961 rettv->vval.v_string = s;
15962 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015963 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015964 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015965 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015966}
15967
15968/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015969 * "pumvisible()" function
15970 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015972f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015974#ifdef FEAT_INS_EXPAND
15975 if (pum_visible())
15976 rettv->vval.v_number = 1;
15977#endif
15978}
15979
Bram Moolenaardb913952012-06-29 12:54:53 +020015980#ifdef FEAT_PYTHON3
15981/*
15982 * "py3eval()" function
15983 */
15984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015985f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015986{
15987 char_u *str;
15988 char_u buf[NUMBUFLEN];
15989
15990 str = get_tv_string_buf(&argvars[0], buf);
15991 do_py3eval(str, rettv);
15992}
15993#endif
15994
15995#ifdef FEAT_PYTHON
15996/*
15997 * "pyeval()" function
15998 */
15999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016000f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016001{
16002 char_u *str;
16003 char_u buf[NUMBUFLEN];
16004
16005 str = get_tv_string_buf(&argvars[0], buf);
16006 do_pyeval(str, rettv);
16007}
16008#endif
16009
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016010/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016011 * "range()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016015{
16016 long start;
16017 long end;
16018 long stride = 1;
16019 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016023 if (argvars[1].v_type == VAR_UNKNOWN)
16024 {
16025 end = start - 1;
16026 start = 0;
16027 }
16028 else
16029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016030 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016033 }
16034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016035 if (error)
16036 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016037 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016038 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016039 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016040 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016041 else
16042 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016043 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016044 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016045 if (list_append_number(rettv->vval.v_list,
16046 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016047 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016048 }
16049}
16050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016051/*
16052 * "readfile()" function
16053 */
16054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016055f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016056{
16057 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016058 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016059 char_u *fname;
16060 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016061 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16062 int io_size = sizeof(buf);
16063 int readlen; /* size of last fread() */
16064 char_u *prev = NULL; /* previously read bytes, if any */
16065 long prevlen = 0; /* length of data in prev */
16066 long prevsize = 0; /* size of prev buffer */
16067 long maxline = MAXLNUM;
16068 long cnt = 0;
16069 char_u *p; /* position in buf */
16070 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016072 if (argvars[1].v_type != VAR_UNKNOWN)
16073 {
16074 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16075 binary = TRUE;
16076 if (argvars[2].v_type != VAR_UNKNOWN)
16077 maxline = get_tv_number(&argvars[2]);
16078 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016079
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016080 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016081 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016082
16083 /* Always open the file in binary mode, library functions have a mind of
16084 * their own about CR-LF conversion. */
16085 fname = get_tv_string(&argvars[0]);
16086 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16087 {
16088 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16089 return;
16090 }
16091
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016092 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016094 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016095
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016096 /* This for loop processes what was read, but is also entered at end
16097 * of file so that either:
16098 * - an incomplete line gets written
16099 * - a "binary" file gets an empty line at the end if it ends in a
16100 * newline. */
16101 for (p = buf, start = buf;
16102 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16103 ++p)
16104 {
16105 if (*p == '\n' || readlen <= 0)
16106 {
16107 listitem_T *li;
16108 char_u *s = NULL;
16109 long_u len = p - start;
16110
16111 /* Finished a line. Remove CRs before NL. */
16112 if (readlen > 0 && !binary)
16113 {
16114 while (len > 0 && start[len - 1] == '\r')
16115 --len;
16116 /* removal may cross back to the "prev" string */
16117 if (len == 0)
16118 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16119 --prevlen;
16120 }
16121 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016122 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016123 else
16124 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016125 /* Change "prev" buffer to be the right size. This way
16126 * the bytes are only copied once, and very long lines are
16127 * allocated only once. */
16128 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016129 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016130 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016131 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016132 prev = NULL; /* the list will own the string */
16133 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016134 }
16135 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016136 if (s == NULL)
16137 {
16138 do_outofmem_msg((long_u) prevlen + len + 1);
16139 failed = TRUE;
16140 break;
16141 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144 {
16145 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016146 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016147 break;
16148 }
16149 li->li_tv.v_type = VAR_STRING;
16150 li->li_tv.v_lock = 0;
16151 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016152 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016153
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016154 start = p + 1; /* step over newline */
16155 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156 break;
16157 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016158 else if (*p == NUL)
16159 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016160#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16162 * when finding the BF and check the previous two bytes. */
16163 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016164 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016165 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16166 * + 1, these may be in the "prev" string. */
16167 char_u back1 = p >= buf + 1 ? p[-1]
16168 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16169 char_u back2 = p >= buf + 2 ? p[-2]
16170 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16171 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016172
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016173 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016174 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016175 char_u *dest = p - 2;
16176
16177 /* Usually a BOM is at the beginning of a file, and so at
16178 * the beginning of a line; then we can just step over it.
16179 */
16180 if (start == dest)
16181 start = p + 1;
16182 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016183 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016184 /* have to shuffle buf to close gap */
16185 int adjust_prevlen = 0;
16186
16187 if (dest < buf)
16188 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016189 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 dest = buf;
16191 }
16192 if (readlen > p - buf + 1)
16193 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16194 readlen -= 3 - adjust_prevlen;
16195 prevlen -= adjust_prevlen;
16196 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016197 }
16198 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016199 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016200#endif
16201 } /* for */
16202
16203 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16204 break;
16205 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016206 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016207 /* There's part of a line in buf, store it in "prev". */
16208 if (p - start + prevlen >= prevsize)
16209 {
16210 /* need bigger "prev" buffer */
16211 char_u *newprev;
16212
16213 /* A common use case is ordinary text files and "prev" gets a
16214 * fragment of a line, so the first allocation is made
16215 * small, to avoid repeatedly 'allocing' large and
16216 * 'reallocing' small. */
16217 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016218 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016219 else
16220 {
16221 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016222 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016223 prevsize = grow50pc > growmin ? grow50pc : growmin;
16224 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016225 newprev = prev == NULL ? alloc(prevsize)
16226 : vim_realloc(prev, prevsize);
16227 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016228 {
16229 do_outofmem_msg((long_u)prevsize);
16230 failed = TRUE;
16231 break;
16232 }
16233 prev = newprev;
16234 }
16235 /* Add the line part to end of "prev". */
16236 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016237 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016238 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016239 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016240
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016241 /*
16242 * For a negative line count use only the lines at the end of the file,
16243 * free the rest.
16244 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016245 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016246 while (cnt > -maxline)
16247 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016248 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016249 --cnt;
16250 }
16251
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016252 if (failed)
16253 {
16254 list_free(rettv->vval.v_list, TRUE);
16255 /* readfile doc says an empty list is returned on error */
16256 rettv->vval.v_list = list_alloc();
16257 }
16258
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016259 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016260 fclose(fd);
16261}
16262
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016263#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016264static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016265
16266/*
16267 * Convert a List to proftime_T.
16268 * Return FAIL when there is something wrong.
16269 */
16270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016271list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016272{
16273 long n1, n2;
16274 int error = FALSE;
16275
16276 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16277 || arg->vval.v_list->lv_len != 2)
16278 return FAIL;
16279 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16280 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16281# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016282 tm->HighPart = n1;
16283 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016284# else
16285 tm->tv_sec = n1;
16286 tm->tv_usec = n2;
16287# endif
16288 return error ? FAIL : OK;
16289}
16290#endif /* FEAT_RELTIME */
16291
16292/*
16293 * "reltime()" function
16294 */
16295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016296f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016297{
16298#ifdef FEAT_RELTIME
16299 proftime_T res;
16300 proftime_T start;
16301
16302 if (argvars[0].v_type == VAR_UNKNOWN)
16303 {
16304 /* No arguments: get current time. */
16305 profile_start(&res);
16306 }
16307 else if (argvars[1].v_type == VAR_UNKNOWN)
16308 {
16309 if (list2proftime(&argvars[0], &res) == FAIL)
16310 return;
16311 profile_end(&res);
16312 }
16313 else
16314 {
16315 /* Two arguments: compute the difference. */
16316 if (list2proftime(&argvars[0], &start) == FAIL
16317 || list2proftime(&argvars[1], &res) == FAIL)
16318 return;
16319 profile_sub(&res, &start);
16320 }
16321
16322 if (rettv_list_alloc(rettv) == OK)
16323 {
16324 long n1, n2;
16325
16326# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016327 n1 = res.HighPart;
16328 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016329# else
16330 n1 = res.tv_sec;
16331 n2 = res.tv_usec;
16332# endif
16333 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16334 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16335 }
16336#endif
16337}
16338
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016339#ifdef FEAT_FLOAT
16340/*
16341 * "reltimefloat()" function
16342 */
16343 static void
16344f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16345{
16346# ifdef FEAT_RELTIME
16347 proftime_T tm;
16348# endif
16349
16350 rettv->v_type = VAR_FLOAT;
16351 rettv->vval.v_float = 0;
16352# ifdef FEAT_RELTIME
16353 if (list2proftime(&argvars[0], &tm) == OK)
16354 rettv->vval.v_float = profile_float(&tm);
16355# endif
16356}
16357#endif
16358
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016359/*
16360 * "reltimestr()" function
16361 */
16362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016363f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016364{
16365#ifdef FEAT_RELTIME
16366 proftime_T tm;
16367#endif
16368
16369 rettv->v_type = VAR_STRING;
16370 rettv->vval.v_string = NULL;
16371#ifdef FEAT_RELTIME
16372 if (list2proftime(&argvars[0], &tm) == OK)
16373 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16374#endif
16375}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016376
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016378static void make_connection(void);
16379static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016380
16381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016382make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383{
16384 if (X_DISPLAY == NULL
16385# ifdef FEAT_GUI
16386 && !gui.in_use
16387# endif
16388 )
16389 {
16390 x_force_connect = TRUE;
16391 setup_term_clip();
16392 x_force_connect = FALSE;
16393 }
16394}
16395
16396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016397check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398{
16399 make_connection();
16400 if (X_DISPLAY == NULL)
16401 {
16402 EMSG(_("E240: No connection to Vim server"));
16403 return FAIL;
16404 }
16405 return OK;
16406}
16407#endif
16408
16409#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016410static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411
16412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016413remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414{
16415 char_u *server_name;
16416 char_u *keys;
16417 char_u *r = NULL;
16418 char_u buf[NUMBUFLEN];
16419# ifdef WIN32
16420 HWND w;
16421# else
16422 Window w;
16423# endif
16424
16425 if (check_restricted() || check_secure())
16426 return;
16427
16428# ifdef FEAT_X11
16429 if (check_connection() == FAIL)
16430 return;
16431# endif
16432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016433 server_name = get_tv_string_chk(&argvars[0]);
16434 if (server_name == NULL)
16435 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 keys = get_tv_string_buf(&argvars[1], buf);
16437# ifdef WIN32
16438 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16439# else
16440 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16441 < 0)
16442# endif
16443 {
16444 if (r != NULL)
16445 EMSG(r); /* sending worked but evaluation failed */
16446 else
16447 EMSG2(_("E241: Unable to send to %s"), server_name);
16448 return;
16449 }
16450
16451 rettv->vval.v_string = r;
16452
16453 if (argvars[2].v_type != VAR_UNKNOWN)
16454 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016455 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016456 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016459 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016460 v.di_tv.v_type = VAR_STRING;
16461 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 idvar = get_tv_string_chk(&argvars[2]);
16463 if (idvar != NULL)
16464 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016465 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016466 }
16467}
16468#endif
16469
16470/*
16471 * "remote_expr()" function
16472 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475{
16476 rettv->v_type = VAR_STRING;
16477 rettv->vval.v_string = NULL;
16478#ifdef FEAT_CLIENTSERVER
16479 remote_common(argvars, rettv, TRUE);
16480#endif
16481}
16482
16483/*
16484 * "remote_foreground()" function
16485 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016487f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489#ifdef FEAT_CLIENTSERVER
16490# ifdef WIN32
16491 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 {
16493 char_u *server_name = get_tv_string_chk(&argvars[0]);
16494
16495 if (server_name != NULL)
16496 serverForeground(server_name);
16497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498# else
16499 /* Send a foreground() expression to the server. */
16500 argvars[1].v_type = VAR_STRING;
16501 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16502 argvars[2].v_type = VAR_UNKNOWN;
16503 remote_common(argvars, rettv, TRUE);
16504 vim_free(argvars[1].vval.v_string);
16505# endif
16506#endif
16507}
16508
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016510f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511{
16512#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 char_u *s = NULL;
16515# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016516 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016518 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519
16520 if (check_restricted() || check_secure())
16521 {
16522 rettv->vval.v_number = -1;
16523 return;
16524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 serverid = get_tv_string_chk(&argvars[0]);
16526 if (serverid == NULL)
16527 {
16528 rettv->vval.v_number = -1;
16529 return; /* type error; errmsg already given */
16530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016531# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016532 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533 if (n == 0)
16534 rettv->vval.v_number = -1;
16535 else
16536 {
16537 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16538 rettv->vval.v_number = (s != NULL);
16539 }
16540# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016541 if (check_connection() == FAIL)
16542 return;
16543
16544 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016545 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546# endif
16547
16548 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 char_u *retvar;
16551
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 v.di_tv.v_type = VAR_STRING;
16553 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016554 retvar = get_tv_string_chk(&argvars[1]);
16555 if (retvar != NULL)
16556 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016557 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 }
16559#else
16560 rettv->vval.v_number = -1;
16561#endif
16562}
16563
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016565f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566{
16567 char_u *r = NULL;
16568
16569#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 char_u *serverid = get_tv_string_chk(&argvars[0]);
16571
16572 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016573 {
16574# ifdef WIN32
16575 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016576 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016578 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579 if (n != 0)
16580 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16581 if (r == NULL)
16582# else
16583 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585# endif
16586 EMSG(_("E277: Unable to read a server reply"));
16587 }
16588#endif
16589 rettv->v_type = VAR_STRING;
16590 rettv->vval.v_string = r;
16591}
16592
16593/*
16594 * "remote_send()" function
16595 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598{
16599 rettv->v_type = VAR_STRING;
16600 rettv->vval.v_string = NULL;
16601#ifdef FEAT_CLIENTSERVER
16602 remote_common(argvars, rettv, FALSE);
16603#endif
16604}
16605
16606/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016607 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016608 */
16609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016610f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016611{
Bram Moolenaar33570922005-01-25 22:26:29 +000016612 list_T *l;
16613 listitem_T *item, *item2;
16614 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016615 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016616 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016617 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 dict_T *d;
16619 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016620 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016621
Bram Moolenaar8c711452005-01-14 21:53:12 +000016622 if (argvars[0].v_type == VAR_DICT)
16623 {
16624 if (argvars[2].v_type != VAR_UNKNOWN)
16625 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016626 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016627 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016629 key = get_tv_string_chk(&argvars[1]);
16630 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016632 di = dict_find(d, key, -1);
16633 if (di == NULL)
16634 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016635 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16636 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016637 {
16638 *rettv = di->di_tv;
16639 init_tv(&di->di_tv);
16640 dictitem_remove(d, di);
16641 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016643 }
16644 }
16645 else if (argvars[0].v_type != VAR_LIST)
16646 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016647 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016648 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 int error = FALSE;
16651
16652 idx = get_tv_number_chk(&argvars[1], &error);
16653 if (error)
16654 ; /* type error: do nothing, errmsg already given */
16655 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016656 EMSGN(_(e_listidx), idx);
16657 else
16658 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016659 if (argvars[2].v_type == VAR_UNKNOWN)
16660 {
16661 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016662 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016663 *rettv = item->li_tv;
16664 vim_free(item);
16665 }
16666 else
16667 {
16668 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016669 end = get_tv_number_chk(&argvars[2], &error);
16670 if (error)
16671 ; /* type error: do nothing */
16672 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016673 EMSGN(_(e_listidx), end);
16674 else
16675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016676 int cnt = 0;
16677
16678 for (li = item; li != NULL; li = li->li_next)
16679 {
16680 ++cnt;
16681 if (li == item2)
16682 break;
16683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016684 if (li == NULL) /* didn't find "item2" after "item" */
16685 EMSG(_(e_invrange));
16686 else
16687 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016688 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016689 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016690 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016691 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016692 l->lv_first = item;
16693 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016694 item->li_prev = NULL;
16695 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016696 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016697 }
16698 }
16699 }
16700 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016701 }
16702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703}
16704
16705/*
16706 * "rename({from}, {to})" function
16707 */
16708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016709f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710{
16711 char_u buf[NUMBUFLEN];
16712
16713 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16717 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718}
16719
16720/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016721 * "repeat()" function
16722 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016724f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016725{
16726 char_u *p;
16727 int n;
16728 int slen;
16729 int len;
16730 char_u *r;
16731 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016732
16733 n = get_tv_number(&argvars[1]);
16734 if (argvars[0].v_type == VAR_LIST)
16735 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016736 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016737 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016738 if (list_extend(rettv->vval.v_list,
16739 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016740 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016741 }
16742 else
16743 {
16744 p = get_tv_string(&argvars[0]);
16745 rettv->v_type = VAR_STRING;
16746 rettv->vval.v_string = NULL;
16747
16748 slen = (int)STRLEN(p);
16749 len = slen * n;
16750 if (len <= 0)
16751 return;
16752
16753 r = alloc(len + 1);
16754 if (r != NULL)
16755 {
16756 for (i = 0; i < n; i++)
16757 mch_memmove(r + i * slen, p, (size_t)slen);
16758 r[len] = NUL;
16759 }
16760
16761 rettv->vval.v_string = r;
16762 }
16763}
16764
16765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 * "resolve()" function
16767 */
16768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016769f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770{
16771 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016772#ifdef HAVE_READLINK
16773 char_u *buf = NULL;
16774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016776 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777#ifdef FEAT_SHORTCUT
16778 {
16779 char_u *v = NULL;
16780
16781 v = mch_resolve_shortcut(p);
16782 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786 }
16787#else
16788# ifdef HAVE_READLINK
16789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 char_u *cpy;
16791 int len;
16792 char_u *remain = NULL;
16793 char_u *q;
16794 int is_relative_to_current = FALSE;
16795 int has_trailing_pathsep = FALSE;
16796 int limit = 100;
16797
16798 p = vim_strsave(p);
16799
16800 if (p[0] == '.' && (vim_ispathsep(p[1])
16801 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16802 is_relative_to_current = TRUE;
16803
16804 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016805 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016808 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810
16811 q = getnextcomp(p);
16812 if (*q != NUL)
16813 {
16814 /* Separate the first path component in "p", and keep the
16815 * remainder (beginning with the path separator). */
16816 remain = vim_strsave(q - 1);
16817 q[-1] = NUL;
16818 }
16819
Bram Moolenaard9462e32011-04-11 21:35:11 +020016820 buf = alloc(MAXPATHL + 1);
16821 if (buf == NULL)
16822 goto fail;
16823
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 for (;;)
16825 {
16826 for (;;)
16827 {
16828 len = readlink((char *)p, (char *)buf, MAXPATHL);
16829 if (len <= 0)
16830 break;
16831 buf[len] = NUL;
16832
16833 if (limit-- == 0)
16834 {
16835 vim_free(p);
16836 vim_free(remain);
16837 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 goto fail;
16840 }
16841
16842 /* Ensure that the result will have a trailing path separator
16843 * if the argument has one. */
16844 if (remain == NULL && has_trailing_pathsep)
16845 add_pathsep(buf);
16846
16847 /* Separate the first path component in the link value and
16848 * concatenate the remainders. */
16849 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16850 if (*q != NUL)
16851 {
16852 if (remain == NULL)
16853 remain = vim_strsave(q - 1);
16854 else
16855 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016856 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857 if (cpy != NULL)
16858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 vim_free(remain);
16860 remain = cpy;
16861 }
16862 }
16863 q[-1] = NUL;
16864 }
16865
16866 q = gettail(p);
16867 if (q > p && *q == NUL)
16868 {
16869 /* Ignore trailing path separator. */
16870 q[-1] = NUL;
16871 q = gettail(p);
16872 }
16873 if (q > p && !mch_isFullName(buf))
16874 {
16875 /* symlink is relative to directory of argument */
16876 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16877 if (cpy != NULL)
16878 {
16879 STRCPY(cpy, p);
16880 STRCPY(gettail(cpy), buf);
16881 vim_free(p);
16882 p = cpy;
16883 }
16884 }
16885 else
16886 {
16887 vim_free(p);
16888 p = vim_strsave(buf);
16889 }
16890 }
16891
16892 if (remain == NULL)
16893 break;
16894
16895 /* Append the first path component of "remain" to "p". */
16896 q = getnextcomp(remain + 1);
16897 len = q - remain - (*q != NUL);
16898 cpy = vim_strnsave(p, STRLEN(p) + len);
16899 if (cpy != NULL)
16900 {
16901 STRNCAT(cpy, remain, len);
16902 vim_free(p);
16903 p = cpy;
16904 }
16905 /* Shorten "remain". */
16906 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016907 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908 else
16909 {
16910 vim_free(remain);
16911 remain = NULL;
16912 }
16913 }
16914
16915 /* If the result is a relative path name, make it explicitly relative to
16916 * the current directory if and only if the argument had this form. */
16917 if (!vim_ispathsep(*p))
16918 {
16919 if (is_relative_to_current
16920 && *p != NUL
16921 && !(p[0] == '.'
16922 && (p[1] == NUL
16923 || vim_ispathsep(p[1])
16924 || (p[1] == '.'
16925 && (p[2] == NUL
16926 || vim_ispathsep(p[2]))))))
16927 {
16928 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016929 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930 if (cpy != NULL)
16931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 vim_free(p);
16933 p = cpy;
16934 }
16935 }
16936 else if (!is_relative_to_current)
16937 {
16938 /* Strip leading "./". */
16939 q = p;
16940 while (q[0] == '.' && vim_ispathsep(q[1]))
16941 q += 2;
16942 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016943 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 }
16945 }
16946
16947 /* Ensure that the result will have no trailing path separator
16948 * if the argument had none. But keep "/" or "//". */
16949 if (!has_trailing_pathsep)
16950 {
16951 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016952 if (after_pathsep(p, q))
16953 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954 }
16955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016956 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 }
16958# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016959 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960# endif
16961#endif
16962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016963 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016964
16965#ifdef HAVE_READLINK
16966fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016967 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970}
16971
16972/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 */
16975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016976f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977{
Bram Moolenaar33570922005-01-25 22:26:29 +000016978 list_T *l;
16979 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981 if (argvars[0].v_type != VAR_LIST)
16982 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016983 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016984 && !tv_check_lock(l->lv_lock,
16985 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 {
16987 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016988 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016989 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990 while (li != NULL)
16991 {
16992 ni = li->li_prev;
16993 list_append(l, li);
16994 li = ni;
16995 }
16996 rettv->vval.v_list = l;
16997 rettv->v_type = VAR_LIST;
16998 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016999 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001}
17002
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017003#define SP_NOMOVE 0x01 /* don't move cursor */
17004#define SP_REPEAT 0x02 /* repeat to find outer pair */
17005#define SP_RETCOUNT 0x04 /* return matchcount */
17006#define SP_SETPCMARK 0x08 /* set previous context mark */
17007#define SP_START 0x10 /* accept match at start position */
17008#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17009#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017010#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017011
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017012static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013
17014/*
17015 * Get flags for a search function.
17016 * Possibly sets "p_ws".
17017 * Returns BACKWARD, FORWARD or zero (for an error).
17018 */
17019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017020get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021{
17022 int dir = FORWARD;
17023 char_u *flags;
17024 char_u nbuf[NUMBUFLEN];
17025 int mask;
17026
17027 if (varp->v_type != VAR_UNKNOWN)
17028 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 flags = get_tv_string_buf_chk(varp, nbuf);
17030 if (flags == NULL)
17031 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 while (*flags != NUL)
17033 {
17034 switch (*flags)
17035 {
17036 case 'b': dir = BACKWARD; break;
17037 case 'w': p_ws = TRUE; break;
17038 case 'W': p_ws = FALSE; break;
17039 default: mask = 0;
17040 if (flagsp != NULL)
17041 switch (*flags)
17042 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017043 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017044 case 'e': mask = SP_END; break;
17045 case 'm': mask = SP_RETCOUNT; break;
17046 case 'n': mask = SP_NOMOVE; break;
17047 case 'p': mask = SP_SUBPAT; break;
17048 case 'r': mask = SP_REPEAT; break;
17049 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017050 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 }
17052 if (mask == 0)
17053 {
17054 EMSG2(_(e_invarg2), flags);
17055 dir = 0;
17056 }
17057 else
17058 *flagsp |= mask;
17059 }
17060 if (dir == 0)
17061 break;
17062 ++flags;
17063 }
17064 }
17065 return dir;
17066}
17067
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017069 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017072search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017074 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 char_u *pat;
17076 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017077 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 int save_p_ws = p_ws;
17079 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017080 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017081 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017082 proftime_T tm;
17083#ifdef FEAT_RELTIME
17084 long time_limit = 0;
17085#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017086 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017087 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017089 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017090 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017091 if (dir == 0)
17092 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017093 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017094 if (flags & SP_START)
17095 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017096 if (flags & SP_END)
17097 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017098 if (flags & SP_COLUMN)
17099 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017100
Bram Moolenaar76929292008-01-06 19:07:36 +000017101 /* Optional arguments: line number to stop searching and timeout. */
17102 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017103 {
17104 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17105 if (lnum_stop < 0)
17106 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017107#ifdef FEAT_RELTIME
17108 if (argvars[3].v_type != VAR_UNKNOWN)
17109 {
17110 time_limit = get_tv_number_chk(&argvars[3], NULL);
17111 if (time_limit < 0)
17112 goto theend;
17113 }
17114#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017115 }
17116
Bram Moolenaar76929292008-01-06 19:07:36 +000017117#ifdef FEAT_RELTIME
17118 /* Set the time limit, if there is one. */
17119 profile_setlimit(time_limit, &tm);
17120#endif
17121
Bram Moolenaar231334e2005-07-25 20:46:57 +000017122 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017123 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017124 * Check to make sure only those flags are set.
17125 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17126 * flags cannot be set. Check for that condition also.
17127 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017128 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017129 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017131 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017132 goto theend;
17133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017135 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017136 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017137 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017138 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017139 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017140 if (flags & SP_SUBPAT)
17141 retval = subpatnum;
17142 else
17143 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017144 if (flags & SP_SETPCMARK)
17145 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017147 if (match_pos != NULL)
17148 {
17149 /* Store the match cursor position */
17150 match_pos->lnum = pos.lnum;
17151 match_pos->col = pos.col + 1;
17152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 /* "/$" will put the cursor after the end of the line, may need to
17154 * correct that here */
17155 check_cursor();
17156 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017157
17158 /* If 'n' flag is used: restore cursor position. */
17159 if (flags & SP_NOMOVE)
17160 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017161 else
17162 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017163theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017165
17166 return retval;
17167}
17168
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017169#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017170
17171/*
17172 * round() is not in C90, use ceil() or floor() instead.
17173 */
17174 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017175vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017176{
17177 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17178}
17179
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017180/*
17181 * "round({float})" function
17182 */
17183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017184f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017185{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017186 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017187
17188 rettv->v_type = VAR_FLOAT;
17189 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017190 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017191 else
17192 rettv->vval.v_float = 0.0;
17193}
17194#endif
17195
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017196/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017197 * "screenattr()" function
17198 */
17199 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017200f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017201{
17202 int row;
17203 int col;
17204 int c;
17205
17206 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17207 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17208 if (row < 0 || row >= screen_Rows
17209 || col < 0 || col >= screen_Columns)
17210 c = -1;
17211 else
17212 c = ScreenAttrs[LineOffset[row] + col];
17213 rettv->vval.v_number = c;
17214}
17215
17216/*
17217 * "screenchar()" function
17218 */
17219 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017220f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017221{
17222 int row;
17223 int col;
17224 int off;
17225 int c;
17226
17227 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17228 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17229 if (row < 0 || row >= screen_Rows
17230 || col < 0 || col >= screen_Columns)
17231 c = -1;
17232 else
17233 {
17234 off = LineOffset[row] + col;
17235#ifdef FEAT_MBYTE
17236 if (enc_utf8 && ScreenLinesUC[off] != 0)
17237 c = ScreenLinesUC[off];
17238 else
17239#endif
17240 c = ScreenLines[off];
17241 }
17242 rettv->vval.v_number = c;
17243}
17244
17245/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017246 * "screencol()" function
17247 *
17248 * First column is 1 to be consistent with virtcol().
17249 */
17250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017251f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017252{
17253 rettv->vval.v_number = screen_screencol() + 1;
17254}
17255
17256/*
17257 * "screenrow()" function
17258 */
17259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017260f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017261{
17262 rettv->vval.v_number = screen_screenrow() + 1;
17263}
17264
17265/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017266 * "search()" function
17267 */
17268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017270{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017271 int flags = 0;
17272
17273 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274}
17275
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017277 * "searchdecl()" function
17278 */
17279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017280f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017281{
17282 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017283 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017284 int error = FALSE;
17285 char_u *name;
17286
17287 rettv->vval.v_number = 1; /* default: FAIL */
17288
17289 name = get_tv_string_chk(&argvars[0]);
17290 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017291 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017292 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017293 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17294 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17295 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017296 if (!error && name != NULL)
17297 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017298 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017299}
17300
17301/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017302 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017304 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017305searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017306{
17307 char_u *spat, *mpat, *epat;
17308 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 int dir;
17311 int flags = 0;
17312 char_u nbuf1[NUMBUFLEN];
17313 char_u nbuf2[NUMBUFLEN];
17314 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017316 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017317 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017320 spat = get_tv_string_chk(&argvars[0]);
17321 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17322 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17323 if (spat == NULL || mpat == NULL || epat == NULL)
17324 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326 /* Handle the optional fourth argument: flags */
17327 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017328 if (dir == 0)
17329 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017330
17331 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017332 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17333 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017334 if ((flags & (SP_END | SP_SUBPAT)) != 0
17335 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017336 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017337 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017338 goto theend;
17339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017341 /* Using 'r' implies 'W', otherwise it doesn't work. */
17342 if (flags & SP_REPEAT)
17343 p_ws = FALSE;
17344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017345 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017346 if (argvars[3].v_type == VAR_UNKNOWN
17347 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 skip = (char_u *)"";
17349 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017351 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017352 if (argvars[5].v_type != VAR_UNKNOWN)
17353 {
17354 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17355 if (lnum_stop < 0)
17356 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017357#ifdef FEAT_RELTIME
17358 if (argvars[6].v_type != VAR_UNKNOWN)
17359 {
17360 time_limit = get_tv_number_chk(&argvars[6], NULL);
17361 if (time_limit < 0)
17362 goto theend;
17363 }
17364#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017365 }
17366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017367 if (skip == NULL)
17368 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017370 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017371 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017372
17373theend:
17374 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017375
17376 return retval;
17377}
17378
17379/*
17380 * "searchpair()" function
17381 */
17382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017383f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017384{
17385 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17386}
17387
17388/*
17389 * "searchpairpos()" function
17390 */
17391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017392f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017393{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017394 pos_T match_pos;
17395 int lnum = 0;
17396 int col = 0;
17397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017399 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017400
17401 if (searchpair_cmn(argvars, &match_pos) > 0)
17402 {
17403 lnum = match_pos.lnum;
17404 col = match_pos.col;
17405 }
17406
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017407 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17408 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017409}
17410
17411/*
17412 * Search for a start/middle/end thing.
17413 * Used by searchpair(), see its documentation for the details.
17414 * Returns 0 or -1 for no match,
17415 */
17416 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417do_searchpair(
17418 char_u *spat, /* start pattern */
17419 char_u *mpat, /* middle pattern */
17420 char_u *epat, /* end pattern */
17421 int dir, /* BACKWARD or FORWARD */
17422 char_u *skip, /* skip expression */
17423 int flags, /* SP_SETPCMARK and other SP_ values */
17424 pos_T *match_pos,
17425 linenr_T lnum_stop, /* stop at this line if not zero */
17426 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017427{
17428 char_u *save_cpo;
17429 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17430 long retval = 0;
17431 pos_T pos;
17432 pos_T firstpos;
17433 pos_T foundpos;
17434 pos_T save_cursor;
17435 pos_T save_pos;
17436 int n;
17437 int r;
17438 int nest = 1;
17439 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017440 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017441 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017442
17443 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17444 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017445 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017446
Bram Moolenaar76929292008-01-06 19:07:36 +000017447#ifdef FEAT_RELTIME
17448 /* Set the time limit, if there is one. */
17449 profile_setlimit(time_limit, &tm);
17450#endif
17451
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017452 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17453 * start/middle/end (pat3, for the top pair). */
17454 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17455 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17456 if (pat2 == NULL || pat3 == NULL)
17457 goto theend;
17458 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17459 if (*mpat == NUL)
17460 STRCPY(pat3, pat2);
17461 else
17462 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17463 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017464 if (flags & SP_START)
17465 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017466
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 save_cursor = curwin->w_cursor;
17468 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017469 clearpos(&firstpos);
17470 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 pat = pat3;
17472 for (;;)
17473 {
17474 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017475 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17477 /* didn't find it or found the first match again: FAIL */
17478 break;
17479
17480 if (firstpos.lnum == 0)
17481 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017482 if (equalpos(pos, foundpos))
17483 {
17484 /* Found the same position again. Can happen with a pattern that
17485 * has "\zs" at the end and searching backwards. Advance one
17486 * character and try again. */
17487 if (dir == BACKWARD)
17488 decl(&pos);
17489 else
17490 incl(&pos);
17491 }
17492 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017494 /* clear the start flag to avoid getting stuck here */
17495 options &= ~SEARCH_START;
17496
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 /* If the skip pattern matches, ignore this match. */
17498 if (*skip != NUL)
17499 {
17500 save_pos = curwin->w_cursor;
17501 curwin->w_cursor = pos;
17502 r = eval_to_bool(skip, &err, NULL, FALSE);
17503 curwin->w_cursor = save_pos;
17504 if (err)
17505 {
17506 /* Evaluating {skip} caused an error, break here. */
17507 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017508 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 break;
17510 }
17511 if (r)
17512 continue;
17513 }
17514
17515 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17516 {
17517 /* Found end when searching backwards or start when searching
17518 * forward: nested pair. */
17519 ++nest;
17520 pat = pat2; /* nested, don't search for middle */
17521 }
17522 else
17523 {
17524 /* Found end when searching forward or start when searching
17525 * backward: end of (nested) pair; or found middle in outer pair. */
17526 if (--nest == 1)
17527 pat = pat3; /* outer level, search for middle */
17528 }
17529
17530 if (nest == 0)
17531 {
17532 /* Found the match: return matchcount or line number. */
17533 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017534 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017536 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017537 if (flags & SP_SETPCMARK)
17538 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 curwin->w_cursor = pos;
17540 if (!(flags & SP_REPEAT))
17541 break;
17542 nest = 1; /* search for next unmatched */
17543 }
17544 }
17545
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017546 if (match_pos != NULL)
17547 {
17548 /* Store the match cursor position */
17549 match_pos->lnum = curwin->w_cursor.lnum;
17550 match_pos->col = curwin->w_cursor.col + 1;
17551 }
17552
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017554 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 curwin->w_cursor = save_cursor;
17556
17557theend:
17558 vim_free(pat2);
17559 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017560 if (p_cpo == empty_option)
17561 p_cpo = save_cpo;
17562 else
17563 /* Darn, evaluating the {skip} expression changed the value. */
17564 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017565
17566 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567}
17568
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017569/*
17570 * "searchpos()" function
17571 */
17572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017573f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017574{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017575 pos_T match_pos;
17576 int lnum = 0;
17577 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017578 int n;
17579 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017580
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017581 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017582 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017583
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017584 n = search_cmn(argvars, &match_pos, &flags);
17585 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017586 {
17587 lnum = match_pos.lnum;
17588 col = match_pos.col;
17589 }
17590
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017591 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17592 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017593 if (flags & SP_SUBPAT)
17594 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017595}
17596
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017600#ifdef FEAT_CLIENTSERVER
17601 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017602 char_u *server = get_tv_string_chk(&argvars[0]);
17603 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604
Bram Moolenaar0d660222005-01-07 21:51:51 +000017605 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017606 if (server == NULL || reply == NULL)
17607 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017608 if (check_restricted() || check_secure())
17609 return;
17610# ifdef FEAT_X11
17611 if (check_connection() == FAIL)
17612 return;
17613# endif
17614
17615 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017617 EMSG(_("E258: Unable to send to client"));
17618 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017620 rettv->vval.v_number = 0;
17621#else
17622 rettv->vval.v_number = -1;
17623#endif
17624}
17625
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017627f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628{
17629 char_u *r = NULL;
17630
17631#ifdef FEAT_CLIENTSERVER
17632# ifdef WIN32
17633 r = serverGetVimNames();
17634# else
17635 make_connection();
17636 if (X_DISPLAY != NULL)
17637 r = serverGetVimNames(X_DISPLAY);
17638# endif
17639#endif
17640 rettv->v_type = VAR_STRING;
17641 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642}
17643
17644/*
17645 * "setbufvar()" function
17646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017648f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649{
17650 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017653 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 char_u nbuf[NUMBUFLEN];
17655
17656 if (check_restricted() || check_secure())
17657 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017658 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17659 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017660 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 varp = &argvars[2];
17662
17663 if (buf != NULL && varname != NULL && varp != NULL)
17664 {
17665 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667
17668 if (*varname == '&')
17669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 long numval;
17671 char_u *strval;
17672 int error = FALSE;
17673
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017675 numval = get_tv_number_chk(varp, &error);
17676 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017677 if (!error && strval != NULL)
17678 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 }
17680 else
17681 {
17682 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17683 if (bufvarname != NULL)
17684 {
17685 STRCPY(bufvarname, "b:");
17686 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017687 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 vim_free(bufvarname);
17689 }
17690 }
17691
17692 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695}
17696
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017698f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017699{
17700 dict_T *d;
17701 dictitem_T *di;
17702 char_u *csearch;
17703
17704 if (argvars[0].v_type != VAR_DICT)
17705 {
17706 EMSG(_(e_dictreq));
17707 return;
17708 }
17709
17710 if ((d = argvars[0].vval.v_dict) != NULL)
17711 {
17712 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17713 if (csearch != NULL)
17714 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017715#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017716 if (enc_utf8)
17717 {
17718 int pcc[MAX_MCO];
17719 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017720
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017721 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17722 }
17723 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017724#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017725 set_last_csearch(PTR2CHAR(csearch),
17726 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017727 }
17728
17729 di = dict_find(d, (char_u *)"forward", -1);
17730 if (di != NULL)
17731 set_csearch_direction(get_tv_number(&di->di_tv)
17732 ? FORWARD : BACKWARD);
17733
17734 di = dict_find(d, (char_u *)"until", -1);
17735 if (di != NULL)
17736 set_csearch_until(!!get_tv_number(&di->di_tv));
17737 }
17738}
17739
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740/*
17741 * "setcmdpos()" function
17742 */
17743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017744f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 int pos = (int)get_tv_number(&argvars[0]) - 1;
17747
17748 if (pos >= 0)
17749 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750}
17751
17752/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017753 * "setfperm({fname}, {mode})" function
17754 */
17755 static void
17756f_setfperm(typval_T *argvars, typval_T *rettv)
17757{
17758 char_u *fname;
17759 char_u modebuf[NUMBUFLEN];
17760 char_u *mode_str;
17761 int i;
17762 int mask;
17763 int mode = 0;
17764
17765 rettv->vval.v_number = 0;
17766 fname = get_tv_string_chk(&argvars[0]);
17767 if (fname == NULL)
17768 return;
17769 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17770 if (mode_str == NULL)
17771 return;
17772 if (STRLEN(mode_str) != 9)
17773 {
17774 EMSG2(_(e_invarg2), mode_str);
17775 return;
17776 }
17777
17778 mask = 1;
17779 for (i = 8; i >= 0; --i)
17780 {
17781 if (mode_str[i] != '-')
17782 mode |= mask;
17783 mask = mask << 1;
17784 }
17785 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17786}
17787
17788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 * "setline()" function
17790 */
17791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017792f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
17794 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017795 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017796 list_T *l = NULL;
17797 listitem_T *li = NULL;
17798 long added = 0;
17799 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017801 lnum = get_tv_lnum(&argvars[0]);
17802 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017804 l = argvars[1].vval.v_list;
17805 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017807 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017808 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017809
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017810 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017811 for (;;)
17812 {
17813 if (l != NULL)
17814 {
17815 /* list argument, get next string */
17816 if (li == NULL)
17817 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017818 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017819 li = li->li_next;
17820 }
17821
17822 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017823 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017824 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017825
17826 /* When coming here from Insert mode, sync undo, so that this can be
17827 * undone separately from what was previously inserted. */
17828 if (u_sync_once == 2)
17829 {
17830 u_sync_once = 1; /* notify that u_sync() was called */
17831 u_sync(TRUE);
17832 }
17833
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017834 if (lnum <= curbuf->b_ml.ml_line_count)
17835 {
17836 /* existing line, replace it */
17837 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17838 {
17839 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017840 if (lnum == curwin->w_cursor.lnum)
17841 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017842 rettv->vval.v_number = 0; /* OK */
17843 }
17844 }
17845 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17846 {
17847 /* lnum is one past the last line, append the line */
17848 ++added;
17849 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17850 rettv->vval.v_number = 0; /* OK */
17851 }
17852
17853 if (l == NULL) /* only one string argument */
17854 break;
17855 ++lnum;
17856 }
17857
17858 if (added > 0)
17859 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860}
17861
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017862static 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 +000017863
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017865 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017866 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017868set_qf_ll_list(
17869 win_T *wp UNUSED,
17870 typval_T *list_arg UNUSED,
17871 typval_T *action_arg UNUSED,
17872 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017873{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017874#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017875 char_u *act;
17876 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017877#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017878
Bram Moolenaar2641f772005-03-25 21:58:17 +000017879 rettv->vval.v_number = -1;
17880
17881#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017882 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017883 EMSG(_(e_listreq));
17884 else
17885 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017886 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017887
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017888 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017889 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017890 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017891 if (act == NULL)
17892 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017893 if (*act == 'a' || *act == 'r')
17894 action = *act;
17895 }
17896
Bram Moolenaar81484f42012-12-05 15:16:47 +010017897 if (l != NULL && set_errorlist(wp, l, action,
17898 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017899 rettv->vval.v_number = 0;
17900 }
17901#endif
17902}
17903
17904/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017905 * "setloclist()" function
17906 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017908f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017909{
17910 win_T *win;
17911
17912 rettv->vval.v_number = -1;
17913
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017914 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017915 if (win != NULL)
17916 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17917}
17918
17919/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017920 * "setmatches()" function
17921 */
17922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017923f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017924{
17925#ifdef FEAT_SEARCH_EXTRA
17926 list_T *l;
17927 listitem_T *li;
17928 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017929 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017930
17931 rettv->vval.v_number = -1;
17932 if (argvars[0].v_type != VAR_LIST)
17933 {
17934 EMSG(_(e_listreq));
17935 return;
17936 }
17937 if ((l = argvars[0].vval.v_list) != NULL)
17938 {
17939
17940 /* To some extent make sure that we are dealing with a list from
17941 * "getmatches()". */
17942 li = l->lv_first;
17943 while (li != NULL)
17944 {
17945 if (li->li_tv.v_type != VAR_DICT
17946 || (d = li->li_tv.vval.v_dict) == NULL)
17947 {
17948 EMSG(_(e_invarg));
17949 return;
17950 }
17951 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017952 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17953 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017954 && dict_find(d, (char_u *)"priority", -1) != NULL
17955 && dict_find(d, (char_u *)"id", -1) != NULL))
17956 {
17957 EMSG(_(e_invarg));
17958 return;
17959 }
17960 li = li->li_next;
17961 }
17962
17963 clear_matches(curwin);
17964 li = l->lv_first;
17965 while (li != NULL)
17966 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017967 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017968 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017969 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017970 char_u *group;
17971 int priority;
17972 int id;
17973 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017974
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017975 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017976 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17977 {
17978 if (s == NULL)
17979 {
17980 s = list_alloc();
17981 if (s == NULL)
17982 return;
17983 }
17984
17985 /* match from matchaddpos() */
17986 for (i = 1; i < 9; i++)
17987 {
17988 sprintf((char *)buf, (char *)"pos%d", i);
17989 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17990 {
17991 if (di->di_tv.v_type != VAR_LIST)
17992 return;
17993
17994 list_append_tv(s, &di->di_tv);
17995 s->lv_refcount++;
17996 }
17997 else
17998 break;
17999 }
18000 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018001
18002 group = get_dict_string(d, (char_u *)"group", FALSE);
18003 priority = (int)get_dict_number(d, (char_u *)"priority");
18004 id = (int)get_dict_number(d, (char_u *)"id");
18005 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18006 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18007 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018008 if (i == 0)
18009 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018010 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018011 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018012 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018013 }
18014 else
18015 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018016 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018017 list_unref(s);
18018 s = NULL;
18019 }
18020
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018021 li = li->li_next;
18022 }
18023 rettv->vval.v_number = 0;
18024 }
18025#endif
18026}
18027
18028/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 * "setpos()" function
18030 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018032f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033{
18034 pos_T pos;
18035 int fnum;
18036 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018037 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018038
Bram Moolenaar08250432008-02-13 11:42:46 +000018039 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018040 name = get_tv_string_chk(argvars);
18041 if (name != NULL)
18042 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018043 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018044 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018045 if (--pos.col < 0)
18046 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018047 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018048 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018049 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018050 if (fnum == curbuf->b_fnum)
18051 {
18052 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018053 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018054 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018055 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018056 curwin->w_set_curswant = FALSE;
18057 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018058 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018059 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018060 }
18061 else
18062 EMSG(_(e_invarg));
18063 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018064 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18065 {
18066 /* set mark */
18067 if (setmark_pos(name[1], &pos, fnum) == OK)
18068 rettv->vval.v_number = 0;
18069 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018070 else
18071 EMSG(_(e_invarg));
18072 }
18073 }
18074}
18075
18076/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018077 * "setqflist()" function
18078 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018080f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018081{
18082 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18083}
18084
18085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 * "setreg()" function
18087 */
18088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018089f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090{
18091 int regname;
18092 char_u *strregname;
18093 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018094 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 int append;
18096 char_u yank_type;
18097 long block_len;
18098
18099 block_len = -1;
18100 yank_type = MAUTO;
18101 append = FALSE;
18102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018104 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018106 if (strregname == NULL)
18107 return; /* type error; errmsg already given */
18108 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 if (regname == 0 || regname == '@')
18110 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018112 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018114 stropt = get_tv_string_chk(&argvars[2]);
18115 if (stropt == NULL)
18116 return; /* type error */
18117 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 switch (*stropt)
18119 {
18120 case 'a': case 'A': /* append */
18121 append = TRUE;
18122 break;
18123 case 'v': case 'c': /* character-wise selection */
18124 yank_type = MCHAR;
18125 break;
18126 case 'V': case 'l': /* line-wise selection */
18127 yank_type = MLINE;
18128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 case 'b': case Ctrl_V: /* block-wise selection */
18130 yank_type = MBLOCK;
18131 if (VIM_ISDIGIT(stropt[1]))
18132 {
18133 ++stropt;
18134 block_len = getdigits(&stropt) - 1;
18135 --stropt;
18136 }
18137 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 }
18139 }
18140
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018141 if (argvars[1].v_type == VAR_LIST)
18142 {
18143 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018144 char_u **allocval;
18145 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018146 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018147 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018148 int len = argvars[1].vval.v_list->lv_len;
18149 listitem_T *li;
18150
Bram Moolenaar7d647822014-04-05 21:28:56 +020018151 /* First half: use for pointers to result lines; second half: use for
18152 * pointers to allocated copies. */
18153 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018154 if (lstval == NULL)
18155 return;
18156 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018157 allocval = lstval + len + 2;
18158 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018159
18160 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18161 li = li->li_next)
18162 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018163 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018164 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018165 goto free_lstval;
18166 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018167 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018168 /* Need to make a copy, next get_tv_string_buf_chk() will
18169 * overwrite the string. */
18170 strval = vim_strsave(buf);
18171 if (strval == NULL)
18172 goto free_lstval;
18173 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018174 }
18175 *curval++ = strval;
18176 }
18177 *curval++ = NULL;
18178
18179 write_reg_contents_lst(regname, lstval, -1,
18180 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018181free_lstval:
18182 while (curallocval > allocval)
18183 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018184 vim_free(lstval);
18185 }
18186 else
18187 {
18188 strval = get_tv_string_chk(&argvars[1]);
18189 if (strval == NULL)
18190 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018191 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018193 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018194 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195}
18196
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018197/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018198 * "settabvar()" function
18199 */
18200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018201f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018202{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018203#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018204 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018205 tabpage_T *tp;
18206#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018207 char_u *varname, *tabvarname;
18208 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018209
18210 rettv->vval.v_number = 0;
18211
18212 if (check_restricted() || check_secure())
18213 return;
18214
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018215#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018216 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018217#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018218 varname = get_tv_string_chk(&argvars[1]);
18219 varp = &argvars[2];
18220
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018221 if (varname != NULL && varp != NULL
18222#ifdef FEAT_WINDOWS
18223 && tp != NULL
18224#endif
18225 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018226 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018227#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018228 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018229 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018230#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018231
18232 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18233 if (tabvarname != NULL)
18234 {
18235 STRCPY(tabvarname, "t:");
18236 STRCPY(tabvarname + 2, varname);
18237 set_var(tabvarname, varp, TRUE);
18238 vim_free(tabvarname);
18239 }
18240
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018241#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018242 /* Restore current tabpage */
18243 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018244 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018245#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018246 }
18247}
18248
18249/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018250 * "settabwinvar()" function
18251 */
18252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018253f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018254{
18255 setwinvar(argvars, rettv, 1);
18256}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257
18258/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018259 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018262f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018264 setwinvar(argvars, rettv, 0);
18265}
18266
18267/*
18268 * "setwinvar()" and "settabwinvar()" functions
18269 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018270
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018272setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018273{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 win_T *win;
18275#ifdef FEAT_WINDOWS
18276 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018277 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018278 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279#endif
18280 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018281 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018283 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284
18285 if (check_restricted() || check_secure())
18286 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018287
18288#ifdef FEAT_WINDOWS
18289 if (off == 1)
18290 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18291 else
18292 tp = curtab;
18293#endif
18294 win = find_win_by_nr(&argvars[off], tp);
18295 varname = get_tv_string_chk(&argvars[off + 1]);
18296 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297
18298 if (win != NULL && varname != NULL && varp != NULL)
18299 {
18300#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018301 need_switch_win = !(tp == curtab && win == curwin);
18302 if (!need_switch_win
18303 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018306 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018308 long numval;
18309 char_u *strval;
18310 int error = FALSE;
18311
18312 ++varname;
18313 numval = get_tv_number_chk(varp, &error);
18314 strval = get_tv_string_buf_chk(varp, nbuf);
18315 if (!error && strval != NULL)
18316 set_option_value(varname, numval, strval, OPT_LOCAL);
18317 }
18318 else
18319 {
18320 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18321 if (winvarname != NULL)
18322 {
18323 STRCPY(winvarname, "w:");
18324 STRCPY(winvarname + 2, varname);
18325 set_var(winvarname, varp, TRUE);
18326 vim_free(winvarname);
18327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 }
18329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018331 if (need_switch_win)
18332 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333#endif
18334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335}
18336
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018337#ifdef FEAT_CRYPT
18338/*
18339 * "sha256({string})" function
18340 */
18341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018342f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018343{
18344 char_u *p;
18345
18346 p = get_tv_string(&argvars[0]);
18347 rettv->vval.v_string = vim_strsave(
18348 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18349 rettv->v_type = VAR_STRING;
18350}
18351#endif /* FEAT_CRYPT */
18352
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018354 * "shellescape({string})" function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018358{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018359 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018360 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018361 rettv->v_type = VAR_STRING;
18362}
18363
18364/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018365 * shiftwidth() function
18366 */
18367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018368f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018369{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018370 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018371}
18372
18373/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018374 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 */
18376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018377f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018379 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380
Bram Moolenaar0d660222005-01-07 21:51:51 +000018381 p = get_tv_string(&argvars[0]);
18382 rettv->vval.v_string = vim_strsave(p);
18383 simplify_filename(rettv->vval.v_string); /* simplify in place */
18384 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385}
18386
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018387#ifdef FEAT_FLOAT
18388/*
18389 * "sin()" function
18390 */
18391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018392f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018393{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018394 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018395
18396 rettv->v_type = VAR_FLOAT;
18397 if (get_float_arg(argvars, &f) == OK)
18398 rettv->vval.v_float = sin(f);
18399 else
18400 rettv->vval.v_float = 0.0;
18401}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018402
18403/*
18404 * "sinh()" function
18405 */
18406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018407f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018409 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018410
18411 rettv->v_type = VAR_FLOAT;
18412 if (get_float_arg(argvars, &f) == OK)
18413 rettv->vval.v_float = sinh(f);
18414 else
18415 rettv->vval.v_float = 0.0;
18416}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018417#endif
18418
Bram Moolenaar0d660222005-01-07 21:51:51 +000018419static int
18420#ifdef __BORLANDC__
18421 _RTLENTRYF
18422#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018423 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018424static int
18425#ifdef __BORLANDC__
18426 _RTLENTRYF
18427#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018428 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018429
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018430/* struct used in the array that's given to qsort() */
18431typedef struct
18432{
18433 listitem_T *item;
18434 int idx;
18435} sortItem_T;
18436
Bram Moolenaar0b962472016-02-22 22:51:33 +010018437/* struct storing information about current sort */
18438typedef struct
18439{
18440 int item_compare_ic;
18441 int item_compare_numeric;
18442 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018443#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018444 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018445#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018446 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018447 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018448 dict_T *item_compare_selfdict;
18449 int item_compare_func_err;
18450 int item_compare_keep_zero;
18451} sortinfo_T;
18452static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018453static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018454#define ITEM_COMPARE_FAIL 999
18455
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018457 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018459 static int
18460#ifdef __BORLANDC__
18461_RTLENTRYF
18462#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018463item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018465 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018466 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018468 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018469 int res;
18470 char_u numbuf1[NUMBUFLEN];
18471 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018473 si1 = (sortItem_T *)s1;
18474 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018475 tv1 = &si1->item->li_tv;
18476 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018477
Bram Moolenaar0b962472016-02-22 22:51:33 +010018478 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018479 {
18480 long v1 = get_tv_number(tv1);
18481 long v2 = get_tv_number(tv2);
18482
18483 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18484 }
18485
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018486#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018487 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018488 {
18489 float_T v1 = get_tv_float(tv1);
18490 float_T v2 = get_tv_float(tv2);
18491
18492 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18493 }
18494#endif
18495
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018496 /* tv2string() puts quotes around a string and allocates memory. Don't do
18497 * that for string variables. Use a single quote when comparing with a
18498 * non-string to do what the docs promise. */
18499 if (tv1->v_type == VAR_STRING)
18500 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018501 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018502 p1 = (char_u *)"'";
18503 else
18504 p1 = tv1->vval.v_string;
18505 }
18506 else
18507 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18508 if (tv2->v_type == VAR_STRING)
18509 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018510 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018511 p2 = (char_u *)"'";
18512 else
18513 p2 = tv2->vval.v_string;
18514 }
18515 else
18516 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018517 if (p1 == NULL)
18518 p1 = (char_u *)"";
18519 if (p2 == NULL)
18520 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018521 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018522 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018523 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018524 res = STRICMP(p1, p2);
18525 else
18526 res = STRCMP(p1, p2);
18527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018529 {
18530 double n1, n2;
18531 n1 = strtod((char *)p1, (char **)&p1);
18532 n2 = strtod((char *)p2, (char **)&p2);
18533 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18534 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018535
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018536 /* When the result would be zero, compare the item indexes. Makes the
18537 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018538 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018539 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018540
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541 vim_free(tofree1);
18542 vim_free(tofree2);
18543 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544}
18545
18546 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547#ifdef __BORLANDC__
18548_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018550item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018551{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018552 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018553 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018554 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018555 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018556 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018557 char_u *func_name;
18558 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018560 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018561 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 return 0;
18563
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018564 si1 = (sortItem_T *)s1;
18565 si2 = (sortItem_T *)s2;
18566
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018567 if (partial == NULL)
18568 func_name = sortinfo->item_compare_func;
18569 else
18570 func_name = partial->pt_name;
18571
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018572 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018573 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018574 copy_tv(&si1->item->li_tv, &argv[0]);
18575 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018576
18577 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018578 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018579 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018580 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 clear_tv(&argv[0]);
18582 clear_tv(&argv[1]);
18583
18584 if (res == FAIL)
18585 res = ITEM_COMPARE_FAIL;
18586 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018587 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18588 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018589 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018591
18592 /* When the result would be zero, compare the pointers themselves. Makes
18593 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018594 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018595 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018596
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 return res;
18598}
18599
18600/*
18601 * "sort({list})" function
18602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018604do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605{
Bram Moolenaar33570922005-01-25 22:26:29 +000018606 list_T *l;
18607 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018608 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018609 sortinfo_T *old_sortinfo;
18610 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 long len;
18612 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613
Bram Moolenaar0b962472016-02-22 22:51:33 +010018614 /* Pointer to current info struct used in compare function. Save and
18615 * restore the current one for nested calls. */
18616 old_sortinfo = sortinfo;
18617 sortinfo = &info;
18618
Bram Moolenaar0d660222005-01-07 21:51:51 +000018619 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018620 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 else
18622 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018623 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018624 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018625 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18626 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018627 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018628 rettv->vval.v_list = l;
18629 rettv->v_type = VAR_LIST;
18630 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631
Bram Moolenaar0d660222005-01-07 21:51:51 +000018632 len = list_len(l);
18633 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018634 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635
Bram Moolenaar0b962472016-02-22 22:51:33 +010018636 info.item_compare_ic = FALSE;
18637 info.item_compare_numeric = FALSE;
18638 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018639#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018640 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018641#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018642 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018643 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018644 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 if (argvars[1].v_type != VAR_UNKNOWN)
18646 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018647 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018649 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018650 else if (argvars[1].v_type == VAR_PARTIAL)
18651 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018652 else
18653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018654 int error = FALSE;
18655
18656 i = get_tv_number_chk(&argvars[1], &error);
18657 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018658 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018660 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018661 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018662 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018663 else if (i != 0)
18664 {
18665 EMSG(_(e_invarg));
18666 goto theend;
18667 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018668 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018669 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018670 if (*info.item_compare_func == NUL)
18671 {
18672 /* empty string means default sort */
18673 info.item_compare_func = NULL;
18674 }
18675 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018676 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018677 info.item_compare_func = NULL;
18678 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018679 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018680 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018681 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018682 info.item_compare_func = NULL;
18683 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018684 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018685#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018686 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018687 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018688 info.item_compare_func = NULL;
18689 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018690 }
18691#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018692 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018693 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018694 info.item_compare_func = NULL;
18695 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018696 }
18697 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018698 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018699
18700 if (argvars[2].v_type != VAR_UNKNOWN)
18701 {
18702 /* optional third argument: {dict} */
18703 if (argvars[2].v_type != VAR_DICT)
18704 {
18705 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018706 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018707 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018708 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
Bram Moolenaar0d660222005-01-07 21:51:51 +000018712 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018713 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018715 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
Bram Moolenaar327aa022014-03-25 18:24:23 +010018717 i = 0;
18718 if (sort)
18719 {
18720 /* sort(): ptrs will be the list to sort */
18721 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018722 {
18723 ptrs[i].item = li;
18724 ptrs[i].idx = i;
18725 ++i;
18726 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018727
Bram Moolenaar0b962472016-02-22 22:51:33 +010018728 info.item_compare_func_err = FALSE;
18729 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018730 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018731 if ((info.item_compare_func != NULL
18732 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018733 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018734 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018735 EMSG(_("E702: Sort compare function failed"));
18736 else
18737 {
18738 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018739 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018740 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018741 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018742 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018743
Bram Moolenaar0b962472016-02-22 22:51:33 +010018744 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018745 {
18746 /* Clear the List and append the items in sorted order. */
18747 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18748 l->lv_len = 0;
18749 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018750 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018751 }
18752 }
18753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018755 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018756 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018757
18758 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018759 info.item_compare_func_err = FALSE;
18760 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018761 item_compare_func_ptr = info.item_compare_func != NULL
18762 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018763 ? item_compare2 : item_compare;
18764
18765 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18766 li = li->li_next)
18767 {
18768 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18769 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018770 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018771 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018772 {
18773 EMSG(_("E882: Uniq compare function failed"));
18774 break;
18775 }
18776 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777
Bram Moolenaar0b962472016-02-22 22:51:33 +010018778 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018780 while (--i >= 0)
18781 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018782 li = ptrs[i].item->li_next;
18783 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018784 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018785 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018787 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018788 list_fix_watch(l, li);
18789 listitem_free(li);
18790 l->lv_len--;
18791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018792 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018793 }
18794
18795 vim_free(ptrs);
18796 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018797theend:
18798 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799}
18800
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018801/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018802 * "sort({list})" function
18803 */
18804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018805f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018806{
18807 do_sort_uniq(argvars, rettv, TRUE);
18808}
18809
18810/*
18811 * "uniq({list})" function
18812 */
18813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018814f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018815{
18816 do_sort_uniq(argvars, rettv, FALSE);
18817}
18818
18819/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018820 * "soundfold({word})" function
18821 */
18822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018823f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018824{
18825 char_u *s;
18826
18827 rettv->v_type = VAR_STRING;
18828 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018829#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018830 rettv->vval.v_string = eval_soundfold(s);
18831#else
18832 rettv->vval.v_string = vim_strsave(s);
18833#endif
18834}
18835
18836/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018837 * "spellbadword()" function
18838 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018840f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018842 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018843 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018844 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018846 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018847 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018848
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018849#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018850 if (argvars[0].v_type == VAR_UNKNOWN)
18851 {
18852 /* Find the start and length of the badly spelled word. */
18853 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18854 if (len != 0)
18855 word = ml_get_cursor();
18856 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018857 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018858 {
18859 char_u *str = get_tv_string_chk(&argvars[0]);
18860 int capcol = -1;
18861
18862 if (str != NULL)
18863 {
18864 /* Check the argument for spelling. */
18865 while (*str != NUL)
18866 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018867 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018868 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018869 {
18870 word = str;
18871 break;
18872 }
18873 str += len;
18874 }
18875 }
18876 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018877#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018878
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018879 list_append_string(rettv->vval.v_list, word, len);
18880 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018881 attr == HLF_SPB ? "bad" :
18882 attr == HLF_SPR ? "rare" :
18883 attr == HLF_SPL ? "local" :
18884 attr == HLF_SPC ? "caps" :
18885 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018886}
18887
18888/*
18889 * "spellsuggest()" function
18890 */
18891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018892f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018893{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018894#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018895 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018896 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018897 int maxcount;
18898 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018899 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018900 listitem_T *li;
18901 int need_capital = FALSE;
18902#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018904 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018905 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018906
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018907#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018908 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018909 {
18910 str = get_tv_string(&argvars[0]);
18911 if (argvars[1].v_type != VAR_UNKNOWN)
18912 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018913 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018914 if (maxcount <= 0)
18915 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018916 if (argvars[2].v_type != VAR_UNKNOWN)
18917 {
18918 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18919 if (typeerr)
18920 return;
18921 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018922 }
18923 else
18924 maxcount = 25;
18925
Bram Moolenaar4770d092006-01-12 23:22:24 +000018926 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018927
18928 for (i = 0; i < ga.ga_len; ++i)
18929 {
18930 str = ((char_u **)ga.ga_data)[i];
18931
18932 li = listitem_alloc();
18933 if (li == NULL)
18934 vim_free(str);
18935 else
18936 {
18937 li->li_tv.v_type = VAR_STRING;
18938 li->li_tv.v_lock = 0;
18939 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018940 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018941 }
18942 }
18943 ga_clear(&ga);
18944 }
18945#endif
18946}
18947
Bram Moolenaar0d660222005-01-07 21:51:51 +000018948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018949f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950{
18951 char_u *str;
18952 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018953 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954 regmatch_T regmatch;
18955 char_u patbuf[NUMBUFLEN];
18956 char_u *save_cpo;
18957 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018959 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018960 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961
18962 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18963 save_cpo = p_cpo;
18964 p_cpo = (char_u *)"";
18965
18966 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018967 if (argvars[1].v_type != VAR_UNKNOWN)
18968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018969 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18970 if (pat == NULL)
18971 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018972 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018973 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018974 }
18975 if (pat == NULL || *pat == NUL)
18976 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018978 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018980 if (typeerr)
18981 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18984 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018986 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018987 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018989 if (*str == NUL)
18990 match = FALSE; /* empty item at the end */
18991 else
18992 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018993 if (match)
18994 end = regmatch.startp[0];
18995 else
18996 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018997 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18998 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019000 if (list_append_string(rettv->vval.v_list, str,
19001 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019003 }
19004 if (!match)
19005 break;
19006 /* Advance to just after the match. */
19007 if (regmatch.endp[0] > str)
19008 col = 0;
19009 else
19010 {
19011 /* Don't get stuck at the same match. */
19012#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019013 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014#else
19015 col = 1;
19016#endif
19017 }
19018 str = regmatch.endp[0];
19019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
Bram Moolenaar473de612013-06-08 18:19:48 +020019021 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023
Bram Moolenaar0d660222005-01-07 21:51:51 +000019024 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025}
19026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019027#ifdef FEAT_FLOAT
19028/*
19029 * "sqrt()" function
19030 */
19031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019032f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019033{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019034 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019035
19036 rettv->v_type = VAR_FLOAT;
19037 if (get_float_arg(argvars, &f) == OK)
19038 rettv->vval.v_float = sqrt(f);
19039 else
19040 rettv->vval.v_float = 0.0;
19041}
19042
19043/*
19044 * "str2float()" function
19045 */
19046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019047f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019048{
19049 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19050
19051 if (*p == '+')
19052 p = skipwhite(p + 1);
19053 (void)string2float(p, &rettv->vval.v_float);
19054 rettv->v_type = VAR_FLOAT;
19055}
19056#endif
19057
Bram Moolenaar2c932302006-03-18 21:42:09 +000019058/*
19059 * "str2nr()" function
19060 */
19061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019062f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019063{
19064 int base = 10;
19065 char_u *p;
19066 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019067 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019068
19069 if (argvars[1].v_type != VAR_UNKNOWN)
19070 {
19071 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019072 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019073 {
19074 EMSG(_(e_invarg));
19075 return;
19076 }
19077 }
19078
19079 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019080 if (*p == '+')
19081 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019082 switch (base)
19083 {
19084 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19085 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19086 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19087 default: what = 0;
19088 }
19089 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019090 rettv->vval.v_number = n;
19091}
19092
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093#ifdef HAVE_STRFTIME
19094/*
19095 * "strftime({format}[, {time}])" function
19096 */
19097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019098f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099{
19100 char_u result_buf[256];
19101 struct tm *curtime;
19102 time_t seconds;
19103 char_u *p;
19104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019105 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019107 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019108 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109 seconds = time(NULL);
19110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 curtime = localtime(&seconds);
19113 /* MSVC returns NULL for an invalid value of seconds. */
19114 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 else
19117 {
19118# ifdef FEAT_MBYTE
19119 vimconv_T conv;
19120 char_u *enc;
19121
19122 conv.vc_type = CONV_NONE;
19123 enc = enc_locale();
19124 convert_setup(&conv, p_enc, enc);
19125 if (conv.vc_type != CONV_NONE)
19126 p = string_convert(&conv, p, NULL);
19127# endif
19128 if (p != NULL)
19129 (void)strftime((char *)result_buf, sizeof(result_buf),
19130 (char *)p, curtime);
19131 else
19132 result_buf[0] = NUL;
19133
19134# ifdef FEAT_MBYTE
19135 if (conv.vc_type != CONV_NONE)
19136 vim_free(p);
19137 convert_setup(&conv, enc, p_enc);
19138 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 else
19141# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019142 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143
19144# ifdef FEAT_MBYTE
19145 /* Release conversion descriptors */
19146 convert_setup(&conv, NULL, NULL);
19147 vim_free(enc);
19148# endif
19149 }
19150}
19151#endif
19152
19153/*
19154 * "stridx()" function
19155 */
19156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019157f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158{
19159 char_u buf[NUMBUFLEN];
19160 char_u *needle;
19161 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019162 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019164 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019166 needle = get_tv_string_chk(&argvars[1]);
19167 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019169 if (needle == NULL || haystack == NULL)
19170 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaar33570922005-01-25 22:26:29 +000019172 if (argvars[2].v_type != VAR_UNKNOWN)
19173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019174 int error = FALSE;
19175
19176 start_idx = get_tv_number_chk(&argvars[2], &error);
19177 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019179 if (start_idx >= 0)
19180 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 }
19182
19183 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19184 if (pos != NULL)
19185 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186}
19187
19188/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 * "string()" function
19190 */
19191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019192f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019193{
19194 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019195 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019197 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019198 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019199 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019200 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202}
19203
19204/*
19205 * "strlen()" function
19206 */
19207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019208f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019210 rettv->vval.v_number = (varnumber_T)(STRLEN(
19211 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019212}
19213
19214/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019215 * "strchars()" function
19216 */
19217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019218f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019219{
19220 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019221 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019222#ifdef FEAT_MBYTE
19223 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019224 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019225#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019226
19227 if (argvars[1].v_type != VAR_UNKNOWN)
19228 skipcc = get_tv_number_chk(&argvars[1], NULL);
19229 if (skipcc < 0 || skipcc > 1)
19230 EMSG(_(e_invarg));
19231 else
19232 {
19233#ifdef FEAT_MBYTE
19234 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19235 while (*s != NUL)
19236 {
19237 func_mb_ptr2char_adv(&s);
19238 ++len;
19239 }
19240 rettv->vval.v_number = len;
19241#else
19242 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19243#endif
19244 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019245}
19246
19247/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019248 * "strdisplaywidth()" function
19249 */
19250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019251f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019252{
19253 char_u *s = get_tv_string(&argvars[0]);
19254 int col = 0;
19255
19256 if (argvars[1].v_type != VAR_UNKNOWN)
19257 col = get_tv_number(&argvars[1]);
19258
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019259 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019260}
19261
19262/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019263 * "strwidth()" function
19264 */
19265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019266f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019267{
19268 char_u *s = get_tv_string(&argvars[0]);
19269
19270 rettv->vval.v_number = (varnumber_T)(
19271#ifdef FEAT_MBYTE
19272 mb_string2cells(s, -1)
19273#else
19274 STRLEN(s)
19275#endif
19276 );
19277}
19278
19279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 * "strpart()" function
19281 */
19282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019283f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284{
19285 char_u *p;
19286 int n;
19287 int len;
19288 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019289 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 slen = (int)STRLEN(p);
19293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019294 n = get_tv_number_chk(&argvars[1], &error);
19295 if (error)
19296 len = 0;
19297 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019298 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 else
19300 len = slen - n; /* default len: all bytes that are available. */
19301
19302 /*
19303 * Only return the overlap between the specified part and the actual
19304 * string.
19305 */
19306 if (n < 0)
19307 {
19308 len += n;
19309 n = 0;
19310 }
19311 else if (n > slen)
19312 n = slen;
19313 if (len < 0)
19314 len = 0;
19315 else if (n + len > slen)
19316 len = slen - n;
19317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 rettv->v_type = VAR_STRING;
19319 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320}
19321
19322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323 * "strridx()" function
19324 */
19325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019326f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019327{
19328 char_u buf[NUMBUFLEN];
19329 char_u *needle;
19330 char_u *haystack;
19331 char_u *rest;
19332 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019333 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 needle = get_tv_string_chk(&argvars[1]);
19336 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019337
19338 rettv->vval.v_number = -1;
19339 if (needle == NULL || haystack == NULL)
19340 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019341
19342 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019343 if (argvars[2].v_type != VAR_UNKNOWN)
19344 {
19345 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019346 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019347 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019348 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019349 }
19350 else
19351 end_idx = haystack_len;
19352
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019354 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019355 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019356 lastmatch = haystack + end_idx;
19357 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019358 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019359 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019360 for (rest = haystack; *rest != '\0'; ++rest)
19361 {
19362 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019363 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019364 break;
19365 lastmatch = rest;
19366 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019367 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019368
19369 if (lastmatch == NULL)
19370 rettv->vval.v_number = -1;
19371 else
19372 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19373}
19374
19375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 * "strtrans()" function
19377 */
19378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381 rettv->v_type = VAR_STRING;
19382 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383}
19384
19385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 * "submatch()" function
19387 */
19388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390{
Bram Moolenaar41571762014-04-02 19:00:58 +020019391 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019392 int no;
19393 int retList = 0;
19394
19395 no = (int)get_tv_number_chk(&argvars[0], &error);
19396 if (error)
19397 return;
19398 error = FALSE;
19399 if (argvars[1].v_type != VAR_UNKNOWN)
19400 retList = get_tv_number_chk(&argvars[1], &error);
19401 if (error)
19402 return;
19403
19404 if (retList == 0)
19405 {
19406 rettv->v_type = VAR_STRING;
19407 rettv->vval.v_string = reg_submatch(no);
19408 }
19409 else
19410 {
19411 rettv->v_type = VAR_LIST;
19412 rettv->vval.v_list = reg_submatch_list(no);
19413 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414}
19415
19416/*
19417 * "substitute()" function
19418 */
19419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019420f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421{
19422 char_u patbuf[NUMBUFLEN];
19423 char_u subbuf[NUMBUFLEN];
19424 char_u flagsbuf[NUMBUFLEN];
19425
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019426 char_u *str = get_tv_string_chk(&argvars[0]);
19427 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19428 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19429 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19430
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019432 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19433 rettv->vval.v_string = NULL;
19434 else
19435 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019436}
19437
19438/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019439 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019442f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443{
19444 int id = 0;
19445#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019446 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 long col;
19448 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019449 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019451 lnum = get_tv_lnum(argvars); /* -1 on type error */
19452 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19453 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019455 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019456 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019457 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458#endif
19459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461}
19462
19463/*
19464 * "synIDattr(id, what [, mode])" function
19465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019467f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468{
19469 char_u *p = NULL;
19470#ifdef FEAT_SYN_HL
19471 int id;
19472 char_u *what;
19473 char_u *mode;
19474 char_u modebuf[NUMBUFLEN];
19475 int modec;
19476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 id = get_tv_number(&argvars[0]);
19478 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019479 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019483 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 modec = 0; /* replace invalid with current */
19485 }
19486 else
19487 {
19488#ifdef FEAT_GUI
19489 if (gui.in_use)
19490 modec = 'g';
19491 else
19492#endif
19493 if (t_colors > 1)
19494 modec = 'c';
19495 else
19496 modec = 't';
19497 }
19498
19499
19500 switch (TOLOWER_ASC(what[0]))
19501 {
19502 case 'b':
19503 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19504 p = highlight_color(id, what, modec);
19505 else /* bold */
19506 p = highlight_has_attr(id, HL_BOLD, modec);
19507 break;
19508
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019509 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 p = highlight_color(id, what, modec);
19511 break;
19512
19513 case 'i':
19514 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19515 p = highlight_has_attr(id, HL_INVERSE, modec);
19516 else /* italic */
19517 p = highlight_has_attr(id, HL_ITALIC, modec);
19518 break;
19519
19520 case 'n': /* name */
19521 p = get_highlight_name(NULL, id - 1);
19522 break;
19523
19524 case 'r': /* reverse */
19525 p = highlight_has_attr(id, HL_INVERSE, modec);
19526 break;
19527
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019528 case 's':
19529 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19530 p = highlight_color(id, what, modec);
19531 else /* standout */
19532 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 break;
19534
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019535 case 'u':
19536 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19537 /* underline */
19538 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19539 else
19540 /* undercurl */
19541 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 break;
19543 }
19544
19545 if (p != NULL)
19546 p = vim_strsave(p);
19547#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 rettv->v_type = VAR_STRING;
19549 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
19553 * "synIDtrans(id)" function
19554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557{
19558 int id;
19559
19560#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562
19563 if (id > 0)
19564 id = syn_get_final_id(id);
19565 else
19566#endif
19567 id = 0;
19568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019569 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570}
19571
19572/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019573 * "synconcealed(lnum, col)" function
19574 */
19575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019576f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019577{
19578#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19579 long lnum;
19580 long col;
19581 int syntax_flags = 0;
19582 int cchar;
19583 int matchid = 0;
19584 char_u str[NUMBUFLEN];
19585#endif
19586
19587 rettv->v_type = VAR_LIST;
19588 rettv->vval.v_list = NULL;
19589
19590#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19591 lnum = get_tv_lnum(argvars); /* -1 on type error */
19592 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19593
19594 vim_memset(str, NUL, sizeof(str));
19595
19596 if (rettv_list_alloc(rettv) != FAIL)
19597 {
19598 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19599 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19600 && curwin->w_p_cole > 0)
19601 {
19602 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19603 syntax_flags = get_syntax_info(&matchid);
19604
19605 /* get the conceal character */
19606 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19607 {
19608 cchar = syn_get_sub_char();
19609 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19610 cchar = lcs_conceal;
19611 if (cchar != NUL)
19612 {
19613# ifdef FEAT_MBYTE
19614 if (has_mbyte)
19615 (*mb_char2bytes)(cchar, str);
19616 else
19617# endif
19618 str[0] = cchar;
19619 }
19620 }
19621 }
19622
19623 list_append_number(rettv->vval.v_list,
19624 (syntax_flags & HL_CONCEAL) != 0);
19625 /* -1 to auto-determine strlen */
19626 list_append_string(rettv->vval.v_list, str, -1);
19627 list_append_number(rettv->vval.v_list, matchid);
19628 }
19629#endif
19630}
19631
19632/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019633 * "synstack(lnum, col)" function
19634 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019636f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019637{
19638#ifdef FEAT_SYN_HL
19639 long lnum;
19640 long col;
19641 int i;
19642 int id;
19643#endif
19644
19645 rettv->v_type = VAR_LIST;
19646 rettv->vval.v_list = NULL;
19647
19648#ifdef FEAT_SYN_HL
19649 lnum = get_tv_lnum(argvars); /* -1 on type error */
19650 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19651
19652 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019653 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019654 && rettv_list_alloc(rettv) != FAIL)
19655 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019656 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019657 for (i = 0; ; ++i)
19658 {
19659 id = syn_get_stack_item(i);
19660 if (id < 0)
19661 break;
19662 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19663 break;
19664 }
19665 }
19666#endif
19667}
19668
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019670get_cmd_output_as_rettv(
19671 typval_T *argvars,
19672 typval_T *rettv,
19673 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019675 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019677 char_u *infile = NULL;
19678 char_u buf[NUMBUFLEN];
19679 int err = FALSE;
19680 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019681 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019682 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019684 rettv->v_type = VAR_STRING;
19685 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019686 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019687 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019688
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019689 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019690 {
19691 /*
19692 * Write the string to a temp file, to be used for input of the shell
19693 * command.
19694 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019695 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019696 {
19697 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019698 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019699 }
19700
19701 fd = mch_fopen((char *)infile, WRITEBIN);
19702 if (fd == NULL)
19703 {
19704 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019705 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019706 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019707 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019708 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019709 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19710 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019711 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019712 else
19713 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019714 size_t len;
19715
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019716 p = get_tv_string_buf_chk(&argvars[1], buf);
19717 if (p == NULL)
19718 {
19719 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019720 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019721 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019722 len = STRLEN(p);
19723 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019724 err = TRUE;
19725 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019726 if (fclose(fd) != 0)
19727 err = TRUE;
19728 if (err)
19729 {
19730 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019732 }
19733 }
19734
Bram Moolenaar52a72462014-08-29 15:53:52 +020019735 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19736 * echoes typeahead, that messes up the display. */
19737 if (!msg_silent)
19738 flags += SHELL_COOKED;
19739
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019740 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742 int len;
19743 listitem_T *li;
19744 char_u *s = NULL;
19745 char_u *start;
19746 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019747 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748
Bram Moolenaar52a72462014-08-29 15:53:52 +020019749 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019750 if (res == NULL)
19751 goto errret;
19752
19753 list = list_alloc();
19754 if (list == NULL)
19755 goto errret;
19756
19757 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019759 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019760 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019761 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019762 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019763
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019764 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765 if (s == NULL)
19766 goto errret;
19767
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019768 for (p = s; start < end; ++p, ++start)
19769 *p = *start == NUL ? NL : *start;
19770 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019771
19772 li = listitem_alloc();
19773 if (li == NULL)
19774 {
19775 vim_free(s);
19776 goto errret;
19777 }
19778 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019779 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019780 li->li_tv.vval.v_string = s;
19781 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019783
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019784 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019785 rettv->v_type = VAR_LIST;
19786 rettv->vval.v_list = list;
19787 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019789 else
19790 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019791 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019792#ifdef USE_CR
19793 /* translate <CR> into <NL> */
19794 if (res != NULL)
19795 {
19796 char_u *s;
19797
19798 for (s = res; *s; ++s)
19799 {
19800 if (*s == CAR)
19801 *s = NL;
19802 }
19803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804#else
19805# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019806 /* translate <CR><NL> into <NL> */
19807 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019809 char_u *s, *d;
19810
19811 d = res;
19812 for (s = res; *s; ++s)
19813 {
19814 if (s[0] == CAR && s[1] == NL)
19815 ++s;
19816 *d++ = *s;
19817 }
19818 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820# endif
19821#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019822 rettv->vval.v_string = res;
19823 res = NULL;
19824 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019825
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019826errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019827 if (infile != NULL)
19828 {
19829 mch_remove(infile);
19830 vim_free(infile);
19831 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019832 if (res != NULL)
19833 vim_free(res);
19834 if (list != NULL)
19835 list_free(list, TRUE);
19836}
19837
19838/*
19839 * "system()" function
19840 */
19841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019842f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019843{
19844 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19845}
19846
19847/*
19848 * "systemlist()" function
19849 */
19850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019851f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019852{
19853 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854}
19855
19856/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019857 * "tabpagebuflist()" function
19858 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019860f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019861{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019862#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019863 tabpage_T *tp;
19864 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019865
19866 if (argvars[0].v_type == VAR_UNKNOWN)
19867 wp = firstwin;
19868 else
19869 {
19870 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19871 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019872 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019873 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019874 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019875 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019876 for (; wp != NULL; wp = wp->w_next)
19877 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019878 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019879 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019880 }
19881#endif
19882}
19883
19884
19885/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019886 * "tabpagenr()" function
19887 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019889f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019890{
19891 int nr = 1;
19892#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019893 char_u *arg;
19894
19895 if (argvars[0].v_type != VAR_UNKNOWN)
19896 {
19897 arg = get_tv_string_chk(&argvars[0]);
19898 nr = 0;
19899 if (arg != NULL)
19900 {
19901 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019902 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019903 else
19904 EMSG2(_(e_invexpr2), arg);
19905 }
19906 }
19907 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019908 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019909#endif
19910 rettv->vval.v_number = nr;
19911}
19912
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019913
19914#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019915static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019916
19917/*
19918 * Common code for tabpagewinnr() and winnr().
19919 */
19920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019921get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019922{
19923 win_T *twin;
19924 int nr = 1;
19925 win_T *wp;
19926 char_u *arg;
19927
19928 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19929 if (argvar->v_type != VAR_UNKNOWN)
19930 {
19931 arg = get_tv_string_chk(argvar);
19932 if (arg == NULL)
19933 nr = 0; /* type error; errmsg already given */
19934 else if (STRCMP(arg, "$") == 0)
19935 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19936 else if (STRCMP(arg, "#") == 0)
19937 {
19938 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19939 if (twin == NULL)
19940 nr = 0;
19941 }
19942 else
19943 {
19944 EMSG2(_(e_invexpr2), arg);
19945 nr = 0;
19946 }
19947 }
19948
19949 if (nr > 0)
19950 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19951 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019952 {
19953 if (wp == NULL)
19954 {
19955 /* didn't find it in this tabpage */
19956 nr = 0;
19957 break;
19958 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019959 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019960 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019961 return nr;
19962}
19963#endif
19964
19965/*
19966 * "tabpagewinnr()" function
19967 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019970{
19971 int nr = 1;
19972#ifdef FEAT_WINDOWS
19973 tabpage_T *tp;
19974
19975 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19976 if (tp == NULL)
19977 nr = 0;
19978 else
19979 nr = get_winnr(tp, &argvars[1]);
19980#endif
19981 rettv->vval.v_number = nr;
19982}
19983
19984
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019985/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019986 * "tagfiles()" function
19987 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019989f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019990{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019991 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019992 tagname_T tn;
19993 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019994
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019995 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019996 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019997 fname = alloc(MAXPATHL);
19998 if (fname == NULL)
19999 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020000
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020001 for (first = TRUE; ; first = FALSE)
20002 if (get_tagfname(&tn, first, fname) == FAIL
20003 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020004 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020005 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020006 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020007}
20008
20009/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020010 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020011 */
20012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020013f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020014{
20015 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020016
20017 tag_pattern = get_tv_string(&argvars[0]);
20018
20019 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020020 if (*tag_pattern == NUL)
20021 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020022
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020023 if (rettv_list_alloc(rettv) == OK)
20024 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020025}
20026
20027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 * "tempname()" function
20029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020031f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032{
20033 static int x = 'A';
20034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020035 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020036 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037
20038 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20039 * names. Skip 'I' and 'O', they are used for shell redirection. */
20040 do
20041 {
20042 if (x == 'Z')
20043 x = '0';
20044 else if (x == '9')
20045 x = 'A';
20046 else
20047 {
20048#ifdef EBCDIC
20049 if (x == 'I')
20050 x = 'J';
20051 else if (x == 'R')
20052 x = 'S';
20053 else
20054#endif
20055 ++x;
20056 }
20057 } while (x == 'I' || x == 'O');
20058}
20059
20060/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020061 * "test(list)" function: Just checking the walls...
20062 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020064f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020065{
20066 /* Used for unit testing. Change the code below to your liking. */
20067#if 0
20068 listitem_T *li;
20069 list_T *l;
20070 char_u *bad, *good;
20071
20072 if (argvars[0].v_type != VAR_LIST)
20073 return;
20074 l = argvars[0].vval.v_list;
20075 if (l == NULL)
20076 return;
20077 li = l->lv_first;
20078 if (li == NULL)
20079 return;
20080 bad = get_tv_string(&li->li_tv);
20081 li = li->li_next;
20082 if (li == NULL)
20083 return;
20084 good = get_tv_string(&li->li_tv);
20085 rettv->vval.v_number = test_edit_score(bad, good);
20086#endif
20087}
20088
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020089#ifdef FEAT_FLOAT
20090/*
20091 * "tan()" function
20092 */
20093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020094f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020095{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020096 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020097
20098 rettv->v_type = VAR_FLOAT;
20099 if (get_float_arg(argvars, &f) == OK)
20100 rettv->vval.v_float = tan(f);
20101 else
20102 rettv->vval.v_float = 0.0;
20103}
20104
20105/*
20106 * "tanh()" function
20107 */
20108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020109f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020110{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020111 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020112
20113 rettv->v_type = VAR_FLOAT;
20114 if (get_float_arg(argvars, &f) == OK)
20115 rettv->vval.v_float = tanh(f);
20116 else
20117 rettv->vval.v_float = 0.0;
20118}
20119#endif
20120
Bram Moolenaar975b5272016-03-15 23:10:59 +010020121#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20122/*
20123 * Get a callback from "arg". It can be a Funcref or a function name.
20124 * When "arg" is zero return an empty string.
20125 * Return NULL for an invalid argument.
20126 */
20127 char_u *
20128get_callback(typval_T *arg, partial_T **pp)
20129{
20130 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20131 {
20132 *pp = arg->vval.v_partial;
20133 return (*pp)->pt_name;
20134 }
20135 *pp = NULL;
20136 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20137 return arg->vval.v_string;
20138 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20139 return (char_u *)"";
20140 EMSG(_("E921: Invalid callback argument"));
20141 return NULL;
20142}
20143#endif
20144
20145#ifdef FEAT_TIMERS
20146/*
20147 * "timer_start(time, callback [, options])" function
20148 */
20149 static void
20150f_timer_start(typval_T *argvars, typval_T *rettv)
20151{
20152 long msec = get_tv_number(&argvars[0]);
20153 timer_T *timer;
20154 int repeat = 0;
20155 char_u *callback;
20156 dict_T *dict;
20157
20158 if (argvars[2].v_type != VAR_UNKNOWN)
20159 {
20160 if (argvars[2].v_type != VAR_DICT
20161 || (dict = argvars[2].vval.v_dict) == NULL)
20162 {
20163 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20164 return;
20165 }
20166 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20167 repeat = get_dict_number(dict, (char_u *)"repeat");
20168 }
20169
20170 timer = create_timer(msec, repeat);
20171 callback = get_callback(&argvars[1], &timer->tr_partial);
20172 if (callback == NULL)
20173 {
20174 stop_timer(timer);
20175 rettv->vval.v_number = -1;
20176 }
20177 else
20178 {
20179 timer->tr_callback = vim_strsave(callback);
20180 rettv->vval.v_number = timer->tr_id;
20181 }
20182}
20183
20184/*
20185 * "timer_stop(timer)" function
20186 */
20187 static void
20188f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20189{
20190 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20191
20192 if (timer != NULL)
20193 stop_timer(timer);
20194}
20195#endif
20196
Bram Moolenaard52d9742005-08-21 22:20:28 +000020197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 * "tolower(string)" function
20199 */
20200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020201f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020202{
20203 char_u *p;
20204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020205 p = vim_strsave(get_tv_string(&argvars[0]));
20206 rettv->v_type = VAR_STRING;
20207 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208
20209 if (p != NULL)
20210 while (*p != NUL)
20211 {
20212#ifdef FEAT_MBYTE
20213 int l;
20214
20215 if (enc_utf8)
20216 {
20217 int c, lc;
20218
20219 c = utf_ptr2char(p);
20220 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020221 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 /* TODO: reallocate string when byte count changes. */
20223 if (utf_char2len(lc) == l)
20224 utf_char2bytes(lc, p);
20225 p += l;
20226 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020227 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 p += l; /* skip multi-byte character */
20229 else
20230#endif
20231 {
20232 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20233 ++p;
20234 }
20235 }
20236}
20237
20238/*
20239 * "toupper(string)" function
20240 */
20241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020242f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020244 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020245 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246}
20247
20248/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020249 * "tr(string, fromstr, tostr)" function
20250 */
20251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020252f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020253{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020254 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020255 char_u *fromstr;
20256 char_u *tostr;
20257 char_u *p;
20258#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020259 int inlen;
20260 int fromlen;
20261 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020262 int idx;
20263 char_u *cpstr;
20264 int cplen;
20265 int first = TRUE;
20266#endif
20267 char_u buf[NUMBUFLEN];
20268 char_u buf2[NUMBUFLEN];
20269 garray_T ga;
20270
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020271 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020272 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20273 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020274
20275 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020276 rettv->v_type = VAR_STRING;
20277 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020278 if (fromstr == NULL || tostr == NULL)
20279 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020280 ga_init2(&ga, (int)sizeof(char), 80);
20281
20282#ifdef FEAT_MBYTE
20283 if (!has_mbyte)
20284#endif
20285 /* not multi-byte: fromstr and tostr must be the same length */
20286 if (STRLEN(fromstr) != STRLEN(tostr))
20287 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020288#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020289error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020290#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020291 EMSG2(_(e_invarg2), fromstr);
20292 ga_clear(&ga);
20293 return;
20294 }
20295
20296 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020297 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020298 {
20299#ifdef FEAT_MBYTE
20300 if (has_mbyte)
20301 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020302 inlen = (*mb_ptr2len)(in_str);
20303 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020304 cplen = inlen;
20305 idx = 0;
20306 for (p = fromstr; *p != NUL; p += fromlen)
20307 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020308 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020309 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020310 {
20311 for (p = tostr; *p != NUL; p += tolen)
20312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020313 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020314 if (idx-- == 0)
20315 {
20316 cplen = tolen;
20317 cpstr = p;
20318 break;
20319 }
20320 }
20321 if (*p == NUL) /* tostr is shorter than fromstr */
20322 goto error;
20323 break;
20324 }
20325 ++idx;
20326 }
20327
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020328 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020329 {
20330 /* Check that fromstr and tostr have the same number of
20331 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020332 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020333 first = FALSE;
20334 for (p = tostr; *p != NUL; p += tolen)
20335 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020336 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020337 --idx;
20338 }
20339 if (idx != 0)
20340 goto error;
20341 }
20342
Bram Moolenaarcde88542015-08-11 19:14:00 +020020343 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020344 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020345 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020346
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020347 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020348 }
20349 else
20350#endif
20351 {
20352 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020353 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020354 if (p != NULL)
20355 ga_append(&ga, tostr[p - fromstr]);
20356 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020357 ga_append(&ga, *in_str);
20358 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020359 }
20360 }
20361
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020362 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020363 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020364 ga_append(&ga, NUL);
20365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020366 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020367}
20368
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020369#ifdef FEAT_FLOAT
20370/*
20371 * "trunc({float})" function
20372 */
20373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020375{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020376 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020377
20378 rettv->v_type = VAR_FLOAT;
20379 if (get_float_arg(argvars, &f) == OK)
20380 /* trunc() is not in C90, use floor() or ceil() instead. */
20381 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20382 else
20383 rettv->vval.v_float = 0.0;
20384}
20385#endif
20386
Bram Moolenaar8299df92004-07-10 09:47:34 +000020387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 * "type(expr)" function
20389 */
20390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020391f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020393 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020394
20395 switch (argvars[0].v_type)
20396 {
20397 case VAR_NUMBER: n = 0; break;
20398 case VAR_STRING: n = 1; break;
20399 case VAR_FUNC: n = 2; break;
20400 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020401 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020402 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020403 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020404 if (argvars[0].vval.v_number == VVAL_FALSE
20405 || argvars[0].vval.v_number == VVAL_TRUE)
20406 n = 6;
20407 else
20408 n = 7;
20409 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020410 case VAR_JOB: n = 8; break;
20411 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020412 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020413 case VAR_UNKNOWN:
20414 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20415 n = -1;
20416 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020417 }
20418 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419}
20420
20421/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020422 * "undofile(name)" function
20423 */
20424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020425f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020426{
20427 rettv->v_type = VAR_STRING;
20428#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020429 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020430 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020431
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020432 if (*fname == NUL)
20433 {
20434 /* If there is no file name there will be no undo file. */
20435 rettv->vval.v_string = NULL;
20436 }
20437 else
20438 {
20439 char_u *ffname = FullName_save(fname, FALSE);
20440
20441 if (ffname != NULL)
20442 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20443 vim_free(ffname);
20444 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020445 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020446#else
20447 rettv->vval.v_string = NULL;
20448#endif
20449}
20450
20451/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020452 * "undotree()" function
20453 */
20454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020455f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020456{
20457 if (rettv_dict_alloc(rettv) == OK)
20458 {
20459 dict_T *dict = rettv->vval.v_dict;
20460 list_T *list;
20461
Bram Moolenaar730cde92010-06-27 05:18:54 +020020462 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020463 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020464 dict_add_nr_str(dict, "save_last",
20465 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020466 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20467 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020468 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020469
20470 list = list_alloc();
20471 if (list != NULL)
20472 {
20473 u_eval_tree(curbuf->b_u_oldhead, list);
20474 dict_add_list(dict, "entries", list);
20475 }
20476 }
20477}
20478
20479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020480 * "values(dict)" function
20481 */
20482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020483f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020484{
20485 dict_list(argvars, rettv, 1);
20486}
20487
20488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 * "virtcol(string)" function
20490 */
20491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020492f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493{
20494 colnr_T vcol = 0;
20495 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020496 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020498 fp = var2fpos(&argvars[0], FALSE, &fnum);
20499 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20500 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 {
20502 getvvcol(curwin, fp, NULL, NULL, &vcol);
20503 ++vcol;
20504 }
20505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020506 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507}
20508
20509/*
20510 * "visualmode()" function
20511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020513f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 char_u str[2];
20516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 str[0] = curbuf->b_visual_mode_eval;
20519 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020520 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521
20522 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020523 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525}
20526
20527/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020528 * "wildmenumode()" function
20529 */
20530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020531f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020532{
20533#ifdef FEAT_WILDMENU
20534 if (wild_menu_showing)
20535 rettv->vval.v_number = 1;
20536#endif
20537}
20538
20539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540 * "winbufnr(nr)" function
20541 */
20542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020543f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544{
20545 win_T *wp;
20546
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020547 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020549 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552}
20553
20554/*
20555 * "wincol()" function
20556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559{
20560 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020561 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562}
20563
20564/*
20565 * "winheight(nr)" function
20566 */
20567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569{
20570 win_T *wp;
20571
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020572 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020574 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020576 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577}
20578
20579/*
20580 * "winline()" function
20581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020583f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584{
20585 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020586 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587}
20588
20589/*
20590 * "winnr()" function
20591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594{
20595 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020596
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020598 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020600 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601}
20602
20603/*
20604 * "winrestcmd()" function
20605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020607f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608{
20609#ifdef FEAT_WINDOWS
20610 win_T *wp;
20611 int winnr = 1;
20612 garray_T ga;
20613 char_u buf[50];
20614
20615 ga_init2(&ga, (int)sizeof(char), 70);
20616 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20617 {
20618 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20619 ga_concat(&ga, buf);
20620# ifdef FEAT_VERTSPLIT
20621 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20622 ga_concat(&ga, buf);
20623# endif
20624 ++winnr;
20625 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020626 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020628 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633}
20634
20635/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020636 * "winrestview()" function
20637 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020639f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020640{
20641 dict_T *dict;
20642
20643 if (argvars[0].v_type != VAR_DICT
20644 || (dict = argvars[0].vval.v_dict) == NULL)
20645 EMSG(_(e_invarg));
20646 else
20647 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020648 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20649 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20650 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20651 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020652#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020653 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20654 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020655#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020656 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20657 {
20658 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20659 curwin->w_set_curswant = FALSE;
20660 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020661
Bram Moolenaar82c25852014-05-28 16:47:16 +020020662 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20663 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020664#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020665 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20666 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020667#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020668 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20669 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20670 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20671 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020672
20673 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020674 win_new_height(curwin, curwin->w_height);
20675# ifdef FEAT_VERTSPLIT
20676 win_new_width(curwin, W_WIDTH(curwin));
20677# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020678 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020679
Bram Moolenaarb851a962014-10-31 15:45:52 +010020680 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020681 curwin->w_topline = 1;
20682 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20683 curwin->w_topline = curbuf->b_ml.ml_line_count;
20684#ifdef FEAT_DIFF
20685 check_topfill(curwin, TRUE);
20686#endif
20687 }
20688}
20689
20690/*
20691 * "winsaveview()" function
20692 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020694f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020695{
20696 dict_T *dict;
20697
Bram Moolenaara800b422010-06-27 01:15:55 +020020698 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020699 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020700 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020701
20702 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20703 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20704#ifdef FEAT_VIRTUALEDIT
20705 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20706#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020707 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020708 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20709
20710 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20711#ifdef FEAT_DIFF
20712 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20713#endif
20714 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20715 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20716}
20717
20718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 * "winwidth(nr)" function
20720 */
20721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020722f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723{
20724 win_T *wp;
20725
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020726 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 else
20730#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020731 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734#endif
20735}
20736
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020738 * "wordcount()" function
20739 */
20740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020741f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020742{
20743 if (rettv_dict_alloc(rettv) == FAIL)
20744 return;
20745 cursor_pos_info(rettv->vval.v_dict);
20746}
20747
20748/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020749 * Write list of strings to file
20750 */
20751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020752write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020753{
20754 listitem_T *li;
20755 int c;
20756 int ret = OK;
20757 char_u *s;
20758
20759 for (li = list->lv_first; li != NULL; li = li->li_next)
20760 {
20761 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20762 {
20763 if (*s == '\n')
20764 c = putc(NUL, fd);
20765 else
20766 c = putc(*s, fd);
20767 if (c == EOF)
20768 {
20769 ret = FAIL;
20770 break;
20771 }
20772 }
20773 if (!binary || li->li_next != NULL)
20774 if (putc('\n', fd) == EOF)
20775 {
20776 ret = FAIL;
20777 break;
20778 }
20779 if (ret == FAIL)
20780 {
20781 EMSG(_(e_write));
20782 break;
20783 }
20784 }
20785 return ret;
20786}
20787
20788/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020789 * "writefile()" function
20790 */
20791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020793{
20794 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020795 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020796 char_u *fname;
20797 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020798 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020799
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020800 if (check_restricted() || check_secure())
20801 return;
20802
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020803 if (argvars[0].v_type != VAR_LIST)
20804 {
20805 EMSG2(_(e_listarg), "writefile()");
20806 return;
20807 }
20808 if (argvars[0].vval.v_list == NULL)
20809 return;
20810
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020811 if (argvars[2].v_type != VAR_UNKNOWN)
20812 {
20813 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20814 binary = TRUE;
20815 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20816 append = TRUE;
20817 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020818
20819 /* Always open the file in binary mode, library functions have a mind of
20820 * their own about CR-LF conversion. */
20821 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020822 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20823 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020824 {
20825 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20826 ret = -1;
20827 }
20828 else
20829 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020830 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20831 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020832 fclose(fd);
20833 }
20834
20835 rettv->vval.v_number = ret;
20836}
20837
20838/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020839 * "xor(expr, expr)" function
20840 */
20841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020842f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020843{
20844 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20845 ^ get_tv_number_chk(&argvars[1], NULL);
20846}
20847
20848
20849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020851 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 */
20853 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020854var2fpos(
20855 typval_T *varp,
20856 int dollar_lnum, /* TRUE when $ is last line */
20857 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020859 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020861 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862
Bram Moolenaara5525202006-03-02 22:52:09 +000020863 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020864 if (varp->v_type == VAR_LIST)
20865 {
20866 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020867 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020868 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020869 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020870
20871 l = varp->vval.v_list;
20872 if (l == NULL)
20873 return NULL;
20874
20875 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020876 pos.lnum = list_find_nr(l, 0L, &error);
20877 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020878 return NULL; /* invalid line number */
20879
20880 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020881 pos.col = list_find_nr(l, 1L, &error);
20882 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020883 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020884 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020885
20886 /* We accept "$" for the column number: last column. */
20887 li = list_find(l, 1L);
20888 if (li != NULL && li->li_tv.v_type == VAR_STRING
20889 && li->li_tv.vval.v_string != NULL
20890 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20891 pos.col = len + 1;
20892
Bram Moolenaara5525202006-03-02 22:52:09 +000020893 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020894 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020895 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020896 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020897
Bram Moolenaara5525202006-03-02 22:52:09 +000020898#ifdef FEAT_VIRTUALEDIT
20899 /* Get the virtual offset. Defaults to zero. */
20900 pos.coladd = list_find_nr(l, 2L, &error);
20901 if (error)
20902 pos.coladd = 0;
20903#endif
20904
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020905 return &pos;
20906 }
20907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020908 name = get_tv_string_chk(varp);
20909 if (name == NULL)
20910 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020911 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020913 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20914 {
20915 if (VIsual_active)
20916 return &VIsual;
20917 return &curwin->w_cursor;
20918 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020919 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020921 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20923 return NULL;
20924 return pp;
20925 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020926
20927#ifdef FEAT_VIRTUALEDIT
20928 pos.coladd = 0;
20929#endif
20930
Bram Moolenaar477933c2007-07-17 14:32:23 +000020931 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020932 {
20933 pos.col = 0;
20934 if (name[1] == '0') /* "w0": first visible line */
20935 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020936 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020937 pos.lnum = curwin->w_topline;
20938 return &pos;
20939 }
20940 else if (name[1] == '$') /* "w$": last visible line */
20941 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020942 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020943 pos.lnum = curwin->w_botline - 1;
20944 return &pos;
20945 }
20946 }
20947 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020949 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 {
20951 pos.lnum = curbuf->b_ml.ml_line_count;
20952 pos.col = 0;
20953 }
20954 else
20955 {
20956 pos.lnum = curwin->w_cursor.lnum;
20957 pos.col = (colnr_T)STRLEN(ml_get_curline());
20958 }
20959 return &pos;
20960 }
20961 return NULL;
20962}
20963
20964/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020965 * Convert list in "arg" into a position and optional file number.
20966 * When "fnump" is NULL there is no file number, only 3 items.
20967 * Note that the column is passed on as-is, the caller may want to decrement
20968 * it to use 1 for the first column.
20969 * Return FAIL when conversion is not possible, doesn't check the position for
20970 * validity.
20971 */
20972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020973list2fpos(
20974 typval_T *arg,
20975 pos_T *posp,
20976 int *fnump,
20977 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020978{
20979 list_T *l = arg->vval.v_list;
20980 long i = 0;
20981 long n;
20982
Bram Moolenaar493c1782014-05-28 14:34:46 +020020983 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20984 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020985 if (arg->v_type != VAR_LIST
20986 || l == NULL
20987 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020988 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020989 return FAIL;
20990
20991 if (fnump != NULL)
20992 {
20993 n = list_find_nr(l, i++, NULL); /* fnum */
20994 if (n < 0)
20995 return FAIL;
20996 if (n == 0)
20997 n = curbuf->b_fnum; /* current buffer */
20998 *fnump = n;
20999 }
21000
21001 n = list_find_nr(l, i++, NULL); /* lnum */
21002 if (n < 0)
21003 return FAIL;
21004 posp->lnum = n;
21005
21006 n = list_find_nr(l, i++, NULL); /* col */
21007 if (n < 0)
21008 return FAIL;
21009 posp->col = n;
21010
21011#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021012 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021013 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021014 posp->coladd = 0;
21015 else
21016 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021017#endif
21018
Bram Moolenaar493c1782014-05-28 14:34:46 +020021019 if (curswantp != NULL)
21020 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21021
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021022 return OK;
21023}
21024
21025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 * Get the length of an environment variable name.
21027 * Advance "arg" to the first character after the name.
21028 * Return 0 for error.
21029 */
21030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021031get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032{
21033 char_u *p;
21034 int len;
21035
21036 for (p = *arg; vim_isIDc(*p); ++p)
21037 ;
21038 if (p == *arg) /* no name found */
21039 return 0;
21040
21041 len = (int)(p - *arg);
21042 *arg = p;
21043 return len;
21044}
21045
21046/*
21047 * Get the length of the name of a function or internal variable.
21048 * "arg" is advanced to the first non-white character after the name.
21049 * Return 0 if something is wrong.
21050 */
21051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021052get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053{
21054 char_u *p;
21055 int len;
21056
21057 /* Find the end of the name. */
21058 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021059 {
21060 if (*p == ':')
21061 {
21062 /* "s:" is start of "s:var", but "n:" is not and can be used in
21063 * slice "[n:]". Also "xx:" is not a namespace. */
21064 len = (int)(p - *arg);
21065 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21066 || len > 1)
21067 break;
21068 }
21069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 if (p == *arg) /* no name found */
21071 return 0;
21072
21073 len = (int)(p - *arg);
21074 *arg = skipwhite(p);
21075
21076 return len;
21077}
21078
21079/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021080 * Get the length of the name of a variable or function.
21081 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021083 * Return -1 if curly braces expansion failed.
21084 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 * If the name contains 'magic' {}'s, expand them and return the
21086 * expanded name in an allocated string via 'alias' - caller must free.
21087 */
21088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021089get_name_len(
21090 char_u **arg,
21091 char_u **alias,
21092 int evaluate,
21093 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094{
21095 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 char_u *p;
21097 char_u *expr_start;
21098 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099
21100 *alias = NULL; /* default to no alias */
21101
21102 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21103 && (*arg)[2] == (int)KE_SNR)
21104 {
21105 /* hard coded <SNR>, already translated */
21106 *arg += 3;
21107 return get_id_len(arg) + 3;
21108 }
21109 len = eval_fname_script(*arg);
21110 if (len > 0)
21111 {
21112 /* literal "<SID>", "s:" or "<SNR>" */
21113 *arg += len;
21114 }
21115
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021117 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021119 p = find_name_end(*arg, &expr_start, &expr_end,
21120 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 if (expr_start != NULL)
21122 {
21123 char_u *temp_string;
21124
21125 if (!evaluate)
21126 {
21127 len += (int)(p - *arg);
21128 *arg = skipwhite(p);
21129 return len;
21130 }
21131
21132 /*
21133 * Include any <SID> etc in the expanded string:
21134 * Thus the -len here.
21135 */
21136 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21137 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021138 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 *alias = temp_string;
21140 *arg = skipwhite(p);
21141 return (int)STRLEN(temp_string);
21142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143
21144 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021145 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 EMSG2(_(e_invexpr2), *arg);
21147
21148 return len;
21149}
21150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021151/*
21152 * Find the end of a variable or function name, taking care of magic braces.
21153 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21154 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021155 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021156 * Return a pointer to just after the name. Equal to "arg" if there is no
21157 * valid name.
21158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021160find_name_end(
21161 char_u *arg,
21162 char_u **expr_start,
21163 char_u **expr_end,
21164 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 int mb_nest = 0;
21167 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021169 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021171 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021173 *expr_start = NULL;
21174 *expr_end = NULL;
21175 }
21176
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021177 /* Quick check for valid starting character. */
21178 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21179 return arg;
21180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021181 for (p = arg; *p != NUL
21182 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021183 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021184 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021186 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021188 if (*p == '\'')
21189 {
21190 /* skip over 'string' to avoid counting [ and ] inside it. */
21191 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21192 ;
21193 if (*p == NUL)
21194 break;
21195 }
21196 else if (*p == '"')
21197 {
21198 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21199 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21200 if (*p == '\\' && p[1] != NUL)
21201 ++p;
21202 if (*p == NUL)
21203 break;
21204 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021205 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21206 {
21207 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021208 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021209 len = (int)(p - arg);
21210 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021211 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021212 break;
21213 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021215 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 if (*p == '[')
21218 ++br_nest;
21219 else if (*p == ']')
21220 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021223 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021225 if (*p == '{')
21226 {
21227 mb_nest++;
21228 if (expr_start != NULL && *expr_start == NULL)
21229 *expr_start = p;
21230 }
21231 else if (*p == '}')
21232 {
21233 mb_nest--;
21234 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21235 *expr_end = p;
21236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 }
21239
21240 return p;
21241}
21242
21243/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021244 * Expands out the 'magic' {}'s in a variable/function name.
21245 * Note that this can call itself recursively, to deal with
21246 * constructs like foo{bar}{baz}{bam}
21247 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21248 * "in_start" ^
21249 * "expr_start" ^
21250 * "expr_end" ^
21251 * "in_end" ^
21252 *
21253 * Returns a new allocated string, which the caller must free.
21254 * Returns NULL for failure.
21255 */
21256 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257make_expanded_name(
21258 char_u *in_start,
21259 char_u *expr_start,
21260 char_u *expr_end,
21261 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021262{
21263 char_u c1;
21264 char_u *retval = NULL;
21265 char_u *temp_result;
21266 char_u *nextcmd = NULL;
21267
21268 if (expr_end == NULL || in_end == NULL)
21269 return NULL;
21270 *expr_start = NUL;
21271 *expr_end = NUL;
21272 c1 = *in_end;
21273 *in_end = NUL;
21274
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021275 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021276 if (temp_result != NULL && nextcmd == NULL)
21277 {
21278 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21279 + (in_end - expr_end) + 1));
21280 if (retval != NULL)
21281 {
21282 STRCPY(retval, in_start);
21283 STRCAT(retval, temp_result);
21284 STRCAT(retval, expr_end + 1);
21285 }
21286 }
21287 vim_free(temp_result);
21288
21289 *in_end = c1; /* put char back for error messages */
21290 *expr_start = '{';
21291 *expr_end = '}';
21292
21293 if (retval != NULL)
21294 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021295 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021296 if (expr_start != NULL)
21297 {
21298 /* Further expansion! */
21299 temp_result = make_expanded_name(retval, expr_start,
21300 expr_end, temp_result);
21301 vim_free(retval);
21302 retval = temp_result;
21303 }
21304 }
21305
21306 return retval;
21307}
21308
21309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021311 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 */
21313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021314eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021316 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21317}
21318
21319/*
21320 * Return TRUE if character "c" can be used as the first character in a
21321 * variable or function name (excluding '{' and '}').
21322 */
21323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021324eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021325{
21326 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327}
21328
21329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 * Set number v: variable to "val".
21331 */
21332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021333set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021335 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336}
21337
21338/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021339 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340 */
21341 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021344 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345}
21346
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021347/*
21348 * Get string v: variable value. Uses a static buffer, can only be used once.
21349 */
21350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021351get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021352{
21353 return get_tv_string(&vimvars[idx].vv_tv);
21354}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021355
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021357 * Get List v: variable value. Caller must take care of reference count when
21358 * needed.
21359 */
21360 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021361get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021362{
21363 return vimvars[idx].vv_list;
21364}
21365
21366/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021367 * Set v:char to character "c".
21368 */
21369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021370set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021371{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021372 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021373
21374#ifdef FEAT_MBYTE
21375 if (has_mbyte)
21376 buf[(*mb_char2bytes)(c, buf)] = NUL;
21377 else
21378#endif
21379 {
21380 buf[0] = c;
21381 buf[1] = NUL;
21382 }
21383 set_vim_var_string(VV_CHAR, buf, -1);
21384}
21385
21386/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021387 * Set v:count to "count" and v:count1 to "count1".
21388 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 */
21390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021391set_vcount(
21392 long count,
21393 long count1,
21394 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021396 if (set_prevcount)
21397 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021398 vimvars[VV_COUNT].vv_nr = count;
21399 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400}
21401
21402/*
21403 * Set string v: variable to a copy of "val".
21404 */
21405 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021406set_vim_var_string(
21407 int idx,
21408 char_u *val,
21409 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410{
Bram Moolenaara542c682016-01-31 16:28:04 +010021411 clear_tv(&vimvars[idx].vv_di.di_tv);
21412 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021414 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021416 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021418 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419}
21420
21421/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021422 * Set List v: variable to "val".
21423 */
21424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021425set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021426{
Bram Moolenaara542c682016-01-31 16:28:04 +010021427 clear_tv(&vimvars[idx].vv_di.di_tv);
21428 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021429 vimvars[idx].vv_list = val;
21430 if (val != NULL)
21431 ++val->lv_refcount;
21432}
21433
21434/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021435 * Set Dictionary v: variable to "val".
21436 */
21437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021438set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021439{
21440 int todo;
21441 hashitem_T *hi;
21442
Bram Moolenaara542c682016-01-31 16:28:04 +010021443 clear_tv(&vimvars[idx].vv_di.di_tv);
21444 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021445 vimvars[idx].vv_dict = val;
21446 if (val != NULL)
21447 {
21448 ++val->dv_refcount;
21449
21450 /* Set readonly */
21451 todo = (int)val->dv_hashtab.ht_used;
21452 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21453 {
21454 if (HASHITEM_EMPTY(hi))
21455 continue;
21456 --todo;
21457 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21458 }
21459 }
21460}
21461
21462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 * Set v:register if needed.
21464 */
21465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021466set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467{
21468 char_u regname;
21469
21470 if (c == 0 || c == ' ')
21471 regname = '"';
21472 else
21473 regname = c;
21474 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 set_vim_var_string(VV_REG, &regname, 1);
21477}
21478
21479/*
21480 * Get or set v:exception. If "oldval" == NULL, return the current value.
21481 * Otherwise, restore the value to "oldval" and return NULL.
21482 * Must always be called in pairs to save and restore v:exception! Does not
21483 * take care of memory allocations.
21484 */
21485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021486v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487{
21488 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021489 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 return NULL;
21493}
21494
21495/*
21496 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21497 * Otherwise, restore the value to "oldval" and return NULL.
21498 * Must always be called in pairs to save and restore v:throwpoint! Does not
21499 * take care of memory allocations.
21500 */
21501 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503{
21504 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021505 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506
Bram Moolenaare9a41262005-01-15 22:18:47 +000021507 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 return NULL;
21509}
21510
21511#if defined(FEAT_AUTOCMD) || defined(PROTO)
21512/*
21513 * Set v:cmdarg.
21514 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21515 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21516 * Must always be called in pairs!
21517 */
21518 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021519set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520{
21521 char_u *oldval;
21522 char_u *newval;
21523 unsigned len;
21524
Bram Moolenaare9a41262005-01-15 22:18:47 +000021525 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021526 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021528 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021529 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021530 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 }
21532
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021533 if (eap->force_bin == FORCE_BIN)
21534 len = 6;
21535 else if (eap->force_bin == FORCE_NOBIN)
21536 len = 8;
21537 else
21538 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021539
21540 if (eap->read_edit)
21541 len += 7;
21542
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021543 if (eap->force_ff != 0)
21544 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21545# ifdef FEAT_MBYTE
21546 if (eap->force_enc != 0)
21547 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021548 if (eap->bad_char != 0)
21549 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021550# endif
21551
21552 newval = alloc(len + 1);
21553 if (newval == NULL)
21554 return NULL;
21555
21556 if (eap->force_bin == FORCE_BIN)
21557 sprintf((char *)newval, " ++bin");
21558 else if (eap->force_bin == FORCE_NOBIN)
21559 sprintf((char *)newval, " ++nobin");
21560 else
21561 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021562
21563 if (eap->read_edit)
21564 STRCAT(newval, " ++edit");
21565
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021566 if (eap->force_ff != 0)
21567 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21568 eap->cmd + eap->force_ff);
21569# ifdef FEAT_MBYTE
21570 if (eap->force_enc != 0)
21571 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21572 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021573 if (eap->bad_char == BAD_KEEP)
21574 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21575 else if (eap->bad_char == BAD_DROP)
21576 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21577 else if (eap->bad_char != 0)
21578 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021579# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021580 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021581 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582}
21583#endif
21584
21585/*
21586 * Get the value of internal variable "name".
21587 * Return OK or FAIL.
21588 */
21589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590get_var_tv(
21591 char_u *name,
21592 int len, /* length of "name" */
21593 typval_T *rettv, /* NULL when only checking existence */
21594 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21595 int verbose, /* may give error message */
21596 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597{
21598 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021599 typval_T *tv = NULL;
21600 typval_T atv;
21601 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603
21604 /* truncate the name, so that we can use strcmp() */
21605 cc = name[len];
21606 name[len] = NUL;
21607
21608 /*
21609 * Check for "b:changedtick".
21610 */
21611 if (STRCMP(name, "b:changedtick") == 0)
21612 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021613 atv.v_type = VAR_NUMBER;
21614 atv.vval.v_number = curbuf->b_changedtick;
21615 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 }
21617
21618 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619 * Check for user-defined variables.
21620 */
21621 else
21622 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021623 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021627 if (dip != NULL)
21628 *dip = v;
21629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 }
21631
Bram Moolenaare9a41262005-01-15 22:18:47 +000021632 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021634 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021635 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 ret = FAIL;
21637 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021639 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640
21641 name[len] = cc;
21642
21643 return ret;
21644}
21645
21646/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021647 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21648 * Also handle function call with Funcref variable: func(expr)
21649 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21650 */
21651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021652handle_subscript(
21653 char_u **arg,
21654 typval_T *rettv,
21655 int evaluate, /* do more than finding the end */
21656 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021657{
21658 int ret = OK;
21659 dict_T *selfdict = NULL;
21660 char_u *s;
21661 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021662 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021663 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021664
21665 while (ret == OK
21666 && (**arg == '['
21667 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021668 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21669 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021670 && !vim_iswhite(*(*arg - 1)))
21671 {
21672 if (**arg == '(')
21673 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021674 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021675 if (evaluate)
21676 {
21677 functv = *rettv;
21678 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021679
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021680 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021681 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021682 {
21683 pt = functv.vval.v_partial;
21684 s = pt->pt_name;
21685 }
21686 else
21687 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021688 }
21689 else
21690 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021691 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021692 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021693 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021694
21695 /* Clear the funcref afterwards, so that deleting it while
21696 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021697 if (evaluate)
21698 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021699
21700 /* Stop the expression evaluation when immediately aborting on
21701 * error, or when an interrupt occurred or an exception was thrown
21702 * but not caught. */
21703 if (aborting())
21704 {
21705 if (ret == OK)
21706 clear_tv(rettv);
21707 ret = FAIL;
21708 }
21709 dict_unref(selfdict);
21710 selfdict = NULL;
21711 }
21712 else /* **arg == '[' || **arg == '.' */
21713 {
21714 dict_unref(selfdict);
21715 if (rettv->v_type == VAR_DICT)
21716 {
21717 selfdict = rettv->vval.v_dict;
21718 if (selfdict != NULL)
21719 ++selfdict->dv_refcount;
21720 }
21721 else
21722 selfdict = NULL;
21723 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21724 {
21725 clear_tv(rettv);
21726 ret = FAIL;
21727 }
21728 }
21729 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021730
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021731 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21732 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021733 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021734 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21735 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021736 char_u *tofree = NULL;
21737 ufunc_T *fp;
21738 char_u fname_buf[FLEN_FIXED + 1];
21739 int error;
21740
21741 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021742 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021743 fp = find_func(fname);
21744 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021745
21746 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021747 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021748 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021749 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21750
21751 if (pt != NULL)
21752 {
21753 pt->pt_refcount = 1;
21754 pt->pt_dict = selfdict;
21755 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021756 if (rettv->v_type == VAR_FUNC)
21757 {
21758 /* just a function: use selfdict */
21759 pt->pt_name = rettv->vval.v_string;
21760 }
21761 else
21762 {
21763 partial_T *ret_pt = rettv->vval.v_partial;
21764 int i;
21765
21766 /* partial: use selfdict and copy args */
21767 pt->pt_name = vim_strsave(ret_pt->pt_name);
21768 if (ret_pt->pt_argc > 0)
21769 {
21770 pt->pt_argv = (typval_T *)alloc(
21771 sizeof(typval_T) * ret_pt->pt_argc);
21772 if (pt->pt_argv == NULL)
21773 /* out of memory: drop the arguments */
21774 pt->pt_argc = 0;
21775 else
21776 {
21777 pt->pt_argc = ret_pt->pt_argc;
21778 for (i = 0; i < pt->pt_argc; i++)
21779 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21780 }
21781 }
21782 partial_unref(ret_pt);
21783 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021784 func_ref(pt->pt_name);
21785 rettv->v_type = VAR_PARTIAL;
21786 rettv->vval.v_partial = pt;
21787 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021788 }
21789 }
21790
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021791 dict_unref(selfdict);
21792 return ret;
21793}
21794
21795/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021796 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021797 * value).
21798 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021799 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021800alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021801{
Bram Moolenaar33570922005-01-25 22:26:29 +000021802 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021803}
21804
21805/*
21806 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 * The string "s" must have been allocated, it is consumed.
21808 * Return NULL for out of memory, the variable otherwise.
21809 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021811alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812{
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021815 rettv = alloc_tv();
21816 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021818 rettv->v_type = VAR_STRING;
21819 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 }
21821 else
21822 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824}
21825
21826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021827 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831{
21832 if (varp != NULL)
21833 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021834 switch (varp->v_type)
21835 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021836 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021837 func_unref(varp->vval.v_string);
21838 /*FALLTHROUGH*/
21839 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021840 vim_free(varp->vval.v_string);
21841 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021842 case VAR_PARTIAL:
21843 partial_unref(varp->vval.v_partial);
21844 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021845 case VAR_LIST:
21846 list_unref(varp->vval.v_list);
21847 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 case VAR_DICT:
21849 dict_unref(varp->vval.v_dict);
21850 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021851 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021852#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021853 job_unref(varp->vval.v_job);
21854 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021855#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021856 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021857#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021858 channel_unref(varp->vval.v_channel);
21859 break;
21860#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021861 case VAR_NUMBER:
21862 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021863 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021864 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021865 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 vim_free(varp);
21868 }
21869}
21870
21871/*
21872 * Free the memory for a variable value and set the value to NULL or 0.
21873 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876{
21877 if (varp != NULL)
21878 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021879 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021881 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021882 func_unref(varp->vval.v_string);
21883 /*FALLTHROUGH*/
21884 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021885 vim_free(varp->vval.v_string);
21886 varp->vval.v_string = NULL;
21887 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021888 case VAR_PARTIAL:
21889 partial_unref(varp->vval.v_partial);
21890 varp->vval.v_partial = NULL;
21891 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 case VAR_LIST:
21893 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021894 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021895 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021896 case VAR_DICT:
21897 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021898 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021899 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021900 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021901 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021902 varp->vval.v_number = 0;
21903 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021904 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021905#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021906 varp->vval.v_float = 0.0;
21907 break;
21908#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021909 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021910#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021911 job_unref(varp->vval.v_job);
21912 varp->vval.v_job = NULL;
21913#endif
21914 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021915 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021916#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021917 channel_unref(varp->vval.v_channel);
21918 varp->vval.v_channel = NULL;
21919#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021920 case VAR_UNKNOWN:
21921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021923 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 }
21925}
21926
21927/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021928 * Set the value of a variable to NULL without freeing items.
21929 */
21930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021931init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021932{
21933 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021934 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935}
21936
21937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 * Get the number value of a variable.
21939 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021940 * For incompatible types, return 0.
21941 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21942 * caller of incompatible types: it sets *denote to TRUE if "denote"
21943 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021945 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021948 int error = FALSE;
21949
21950 return get_tv_number_chk(varp, &error); /* return 0L on error */
21951}
21952
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021953 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021954get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021955{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021956 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021958 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021961 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021962 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021963#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021964 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021965 break;
21966#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021968 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021969 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021970 break;
21971 case VAR_STRING:
21972 if (varp->vval.v_string != NULL)
21973 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021974 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021975 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021976 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021977 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021978 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021979 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021980 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021981 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021982 case VAR_SPECIAL:
21983 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21984 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021985 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021986#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021987 EMSG(_("E910: Using a Job as a Number"));
21988 break;
21989#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021990 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021991#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021992 EMSG(_("E913: Using a Channel as a Number"));
21993 break;
21994#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021995 case VAR_UNKNOWN:
21996 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021999 if (denote == NULL) /* useful for values that must be unsigned */
22000 n = -1;
22001 else
22002 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022003 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004}
22005
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022006#ifdef FEAT_FLOAT
22007 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022009{
22010 switch (varp->v_type)
22011 {
22012 case VAR_NUMBER:
22013 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022014 case VAR_FLOAT:
22015 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022016 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022017 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022018 EMSG(_("E891: Using a Funcref as a Float"));
22019 break;
22020 case VAR_STRING:
22021 EMSG(_("E892: Using a String as a Float"));
22022 break;
22023 case VAR_LIST:
22024 EMSG(_("E893: Using a List as a Float"));
22025 break;
22026 case VAR_DICT:
22027 EMSG(_("E894: Using a Dictionary as a Float"));
22028 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022029 case VAR_SPECIAL:
22030 EMSG(_("E907: Using a special value as a Float"));
22031 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022032 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022033# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022034 EMSG(_("E911: Using a Job as a Float"));
22035 break;
22036# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022037 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022038# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022039 EMSG(_("E914: Using a Channel as a Float"));
22040 break;
22041# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022042 case VAR_UNKNOWN:
22043 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022044 break;
22045 }
22046 return 0;
22047}
22048#endif
22049
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022051 * Get the lnum from the first argument.
22052 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022053 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 */
22055 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022056get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057{
Bram Moolenaar33570922005-01-25 22:26:29 +000022058 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 linenr_T lnum;
22060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022061 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 if (lnum == 0) /* no valid number, try using line() */
22063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 rettv.v_type = VAR_NUMBER;
22065 f_line(argvars, &rettv);
22066 lnum = rettv.vval.v_number;
22067 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068 }
22069 return lnum;
22070}
22071
22072/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022073 * Get the lnum from the first argument.
22074 * Also accepts "$", then "buf" is used.
22075 * Returns 0 on error.
22076 */
22077 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022078get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022079{
22080 if (argvars[0].v_type == VAR_STRING
22081 && argvars[0].vval.v_string != NULL
22082 && argvars[0].vval.v_string[0] == '$'
22083 && buf != NULL)
22084 return buf->b_ml.ml_line_count;
22085 return get_tv_number_chk(&argvars[0], NULL);
22086}
22087
22088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 * Get the string value of a variable.
22090 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022091 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22092 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 * If the String variable has never been set, return an empty string.
22094 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022095 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22096 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022098 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100{
22101 static char_u mybuf[NUMBUFLEN];
22102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022103 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104}
22105
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022106 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022107get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022109 char_u *res = get_tv_string_buf_chk(varp, buf);
22110
22111 return res != NULL ? res : (char_u *)"";
22112}
22113
Bram Moolenaar7d647822014-04-05 21:28:56 +020022114/*
22115 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22116 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022117 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022118get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022119{
22120 static char_u mybuf[NUMBUFLEN];
22121
22122 return get_tv_string_buf_chk(varp, mybuf);
22123}
22124
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022125 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022126get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022127{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022128 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022130 case VAR_NUMBER:
22131 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22132 return buf;
22133 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022134 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022135 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022136 break;
22137 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022138 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022139 break;
22140 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022141 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022143 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022144#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022145 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022146 break;
22147#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022148 case VAR_STRING:
22149 if (varp->vval.v_string != NULL)
22150 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022151 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022152 case VAR_SPECIAL:
22153 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22154 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022155 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022157 {
22158 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022159 char *status;
22160
22161 if (job == NULL)
22162 return (char_u *)"no process";
22163 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022164 : job->jv_status == JOB_ENDED ? "dead"
22165 : "run";
22166# ifdef UNIX
22167 vim_snprintf((char *)buf, NUMBUFLEN,
22168 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022169# elif defined(WIN32)
22170 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022171 "process %ld %s",
22172 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022173 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022174# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022175 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022176 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22177# endif
22178 return buf;
22179 }
22180#endif
22181 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022182 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022183#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022184 {
22185 channel_T *channel = varp->vval.v_channel;
22186 char *status = channel_status(channel);
22187
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022188 if (channel == NULL)
22189 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22190 else
22191 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022192 "channel %d %s", channel->ch_id, status);
22193 return buf;
22194 }
22195#endif
22196 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022197 case VAR_UNKNOWN:
22198 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022199 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022201 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202}
22203
22204/*
22205 * Find variable "name" in the list of variables.
22206 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022207 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022208 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022209 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022211 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022212find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216
Bram Moolenaara7043832005-01-21 11:56:39 +000022217 ht = find_var_ht(name, &varname);
22218 if (htp != NULL)
22219 *htp = ht;
22220 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022222 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223}
22224
22225/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022226 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022227 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022230find_var_in_ht(
22231 hashtab_T *ht,
22232 int htname,
22233 char_u *varname,
22234 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022235{
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 hashitem_T *hi;
22237
22238 if (*varname == NUL)
22239 {
22240 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022241 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022242 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022243 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022244 case 'g': return &globvars_var;
22245 case 'v': return &vimvars_var;
22246 case 'b': return &curbuf->b_bufvar;
22247 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022248#ifdef FEAT_WINDOWS
22249 case 't': return &curtab->tp_winvar;
22250#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022251 case 'l': return current_funccal == NULL
22252 ? NULL : &current_funccal->l_vars_var;
22253 case 'a': return current_funccal == NULL
22254 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 }
22256 return NULL;
22257 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022258
22259 hi = hash_find(ht, varname);
22260 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022261 {
22262 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022263 * worked find the variable again. Don't auto-load a script if it was
22264 * loaded already, otherwise it would be loaded every time when
22265 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022266 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022267 {
22268 /* Note: script_autoload() may make "hi" invalid. It must either
22269 * be obtained again or not used. */
22270 if (!script_autoload(varname, FALSE) || aborting())
22271 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022272 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022273 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022274 if (HASHITEM_EMPTY(hi))
22275 return NULL;
22276 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022277 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022278}
22279
22280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022282 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022283 * Set "varname" to the start of name without ':'.
22284 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022286find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022288 hashitem_T *hi;
22289
Bram Moolenaar73627d02015-08-11 15:46:09 +020022290 if (name[0] == NUL)
22291 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 if (name[1] != ':')
22293 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022294 /* The name must not start with a colon or #. */
22295 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 return NULL;
22297 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022298
22299 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022300 hi = hash_find(&compat_hashtab, name);
22301 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022302 return &compat_hashtab;
22303
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022305 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022306 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 }
22308 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022309 if (*name == 'g') /* global variable */
22310 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022311 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22312 */
22313 if (vim_strchr(name + 2, ':') != NULL
22314 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022315 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022317 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022319 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022320#ifdef FEAT_WINDOWS
22321 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022322 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022323#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022324 if (*name == 'v') /* v: variable */
22325 return &vimvarht;
22326 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022327 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022328 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022329 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 if (*name == 's' /* script variable */
22331 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22332 return &SCRIPT_VARS(current_SID);
22333 return NULL;
22334}
22335
22336/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022337 * Get function call environment based on bactrace debug level
22338 */
22339 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022340get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022341{
22342 int i;
22343 funccall_T *funccal;
22344 funccall_T *temp_funccal;
22345
22346 funccal = current_funccal;
22347 if (debug_backtrace_level > 0)
22348 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022349 for (i = 0; i < debug_backtrace_level; i++)
22350 {
22351 temp_funccal = funccal->caller;
22352 if (temp_funccal)
22353 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022354 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022355 /* backtrace level overflow. reset to max */
22356 debug_backtrace_level = i;
22357 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022358 }
22359 return funccal;
22360}
22361
22362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022363 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022364 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 * Returns NULL when it doesn't exist.
22366 */
22367 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022368get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369{
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022372 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 if (v == NULL)
22374 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376}
22377
22378/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 * sourcing this script and when executing functions defined in the script.
22381 */
22382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022383new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384{
Bram Moolenaara7043832005-01-21 11:56:39 +000022385 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 hashtab_T *ht;
22387 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022388
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22390 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022391 /* Re-allocating ga_data means that an ht_array pointing to
22392 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022394 for (i = 1; i <= ga_scripts.ga_len; ++i)
22395 {
22396 ht = &SCRIPT_VARS(i);
22397 if (ht->ht_mask == HT_INIT_SIZE - 1)
22398 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022399 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022400 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022401 }
22402
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 while (ga_scripts.ga_len < id)
22404 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022405 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022406 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022407 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 }
22410 }
22411}
22412
22413/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22415 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416 */
22417 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022418init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419{
Bram Moolenaar33570922005-01-25 22:26:29 +000022420 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022421 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022422 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022423 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022424 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022425 dict_var->di_tv.vval.v_dict = dict;
22426 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022427 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022428 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22429 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430}
22431
22432/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022433 * Unreference a dictionary initialized by init_var_dict().
22434 */
22435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022436unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022437{
22438 /* Now the dict needs to be freed if no one else is using it, go back to
22439 * normal reference counting. */
22440 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22441 dict_unref(dict);
22442}
22443
22444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 * Frees all allocated variables and the value they contain.
22447 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448 */
22449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022450vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022451{
22452 vars_clear_ext(ht, TRUE);
22453}
22454
22455/*
22456 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22457 */
22458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022459vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460{
Bram Moolenaara7043832005-01-21 11:56:39 +000022461 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 hashitem_T *hi;
22463 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022466 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022467 for (hi = ht->ht_array; todo > 0; ++hi)
22468 {
22469 if (!HASHITEM_EMPTY(hi))
22470 {
22471 --todo;
22472
Bram Moolenaar33570922005-01-25 22:26:29 +000022473 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022474 * ht_array might change then. hash_clear() takes care of it
22475 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 v = HI2DI(hi);
22477 if (free_val)
22478 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022479 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022480 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022481 }
22482 }
22483 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022484 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485}
22486
Bram Moolenaara7043832005-01-21 11:56:39 +000022487/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022488 * Delete a variable from hashtab "ht" at item "hi".
22489 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022492delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493{
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022495
22496 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 clear_tv(&di->di_tv);
22498 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499}
22500
22501/*
22502 * List the value of one internal variable.
22503 */
22504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022505list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022507 char_u *tofree;
22508 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022509 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022511 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022512 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022513 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022514 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515}
22516
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022518list_one_var_a(
22519 char_u *prefix,
22520 char_u *name,
22521 int type,
22522 char_u *string,
22523 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524{
Bram Moolenaar31859182007-08-14 20:41:13 +000022525 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22526 msg_start();
22527 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 if (name != NULL) /* "a:" vars don't have a name stored */
22529 msg_puts(name);
22530 msg_putchar(' ');
22531 msg_advance(22);
22532 if (type == VAR_NUMBER)
22533 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022534 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022535 msg_putchar('*');
22536 else if (type == VAR_LIST)
22537 {
22538 msg_putchar('[');
22539 if (*string == '[')
22540 ++string;
22541 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022542 else if (type == VAR_DICT)
22543 {
22544 msg_putchar('{');
22545 if (*string == '{')
22546 ++string;
22547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 else
22549 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022550
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022552
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022553 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022554 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022555 if (*first)
22556 {
22557 msg_clr_eos();
22558 *first = FALSE;
22559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560}
22561
22562/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022563 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 * If the variable already exists, the value is updated.
22565 * Otherwise the variable is created.
22566 */
22567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022568set_var(
22569 char_u *name,
22570 typval_T *tv,
22571 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572{
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022577 ht = find_var_ht(name, &varname);
22578 if (ht == NULL || *varname == NUL)
22579 {
22580 EMSG2(_(e_illvar), name);
22581 return;
22582 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022583 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022584
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022585 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22586 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022587 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022592 if (var_check_ro(v->di_flags, name, FALSE)
22593 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022595
22596 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022597 * Handle setting internal v: variables separately where needed to
22598 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 */
22600 if (ht == &vimvarht)
22601 {
22602 if (v->di_tv.v_type == VAR_STRING)
22603 {
22604 vim_free(v->di_tv.vval.v_string);
22605 if (copy || tv->v_type != VAR_STRING)
22606 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22607 else
22608 {
22609 /* Take over the string to avoid an extra alloc/free. */
22610 v->di_tv.vval.v_string = tv->vval.v_string;
22611 tv->vval.v_string = NULL;
22612 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022613 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022615 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022616 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022617 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022618 if (STRCMP(varname, "searchforward") == 0)
22619 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022620#ifdef FEAT_SEARCH_EXTRA
22621 else if (STRCMP(varname, "hlsearch") == 0)
22622 {
22623 no_hlsearch = !v->di_tv.vval.v_number;
22624 redraw_all_later(SOME_VALID);
22625 }
22626#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022627 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022628 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022629 else if (v->di_tv.v_type != tv->v_type)
22630 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022631 }
22632
22633 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 }
22635 else /* add a new variable */
22636 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022637 /* Can't add "v:" variable. */
22638 if (ht == &vimvarht)
22639 {
22640 EMSG2(_(e_illvar), name);
22641 return;
22642 }
22643
Bram Moolenaar92124a32005-06-17 22:03:40 +000022644 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022645 if (!valid_varname(varname))
22646 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022647
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022648 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22649 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022650 if (v == NULL)
22651 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022653 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022655 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 return;
22657 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022658 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022660
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022661 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022662 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022663 else
22664 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022666 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022667 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669}
22670
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022671/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022672 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 * Also give an error message.
22674 */
22675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022676var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022677{
22678 if (flags & DI_FLAGS_RO)
22679 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022680 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 return TRUE;
22682 }
22683 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22684 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022685 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 return TRUE;
22687 }
22688 return FALSE;
22689}
22690
22691/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022692 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22693 * Also give an error message.
22694 */
22695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022696var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022697{
22698 if (flags & DI_FLAGS_FIX)
22699 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022700 EMSG2(_("E795: Cannot delete variable %s"),
22701 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022702 return TRUE;
22703 }
22704 return FALSE;
22705}
22706
22707/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022708 * Check if a funcref is assigned to a valid variable name.
22709 * Return TRUE and give an error if not.
22710 */
22711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022712var_check_func_name(
22713 char_u *name, /* points to start of variable name */
22714 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022715{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022716 /* Allow for w: b: s: and t:. */
22717 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022718 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22719 ? name[2] : name[0]))
22720 {
22721 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22722 name);
22723 return TRUE;
22724 }
22725 /* Don't allow hiding a function. When "v" is not NULL we might be
22726 * assigning another function to the same var, the type is checked
22727 * below. */
22728 if (new_var && function_exists(name))
22729 {
22730 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22731 name);
22732 return TRUE;
22733 }
22734 return FALSE;
22735}
22736
22737/*
22738 * Check if a variable name is valid.
22739 * Return FALSE and give an error if not.
22740 */
22741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022743{
22744 char_u *p;
22745
22746 for (p = varname; *p != NUL; ++p)
22747 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22748 && *p != AUTOLOAD_CHAR)
22749 {
22750 EMSG2(_(e_illvar), varname);
22751 return FALSE;
22752 }
22753 return TRUE;
22754}
22755
22756/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022757 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022758 * Also give an error message, using "name" or _("name") when use_gettext is
22759 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022760 */
22761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022762tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022763{
22764 if (lock & VAR_LOCKED)
22765 {
22766 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022767 name == NULL ? (char_u *)_("Unknown")
22768 : use_gettext ? (char_u *)_(name)
22769 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022770 return TRUE;
22771 }
22772 if (lock & VAR_FIXED)
22773 {
22774 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022775 name == NULL ? (char_u *)_("Unknown")
22776 : use_gettext ? (char_u *)_(name)
22777 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022778 return TRUE;
22779 }
22780 return FALSE;
22781}
22782
22783/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022786 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022787 * It is OK for "from" and "to" to point to the same item. This is used to
22788 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022791copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022793 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022794 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795 switch (from->v_type)
22796 {
22797 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022798 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022799 to->vval.v_number = from->vval.v_number;
22800 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022801 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022802#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022803 to->vval.v_float = from->vval.v_float;
22804 break;
22805#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022806 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022807#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022808 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022809 if (to->vval.v_job != NULL)
22810 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022811 break;
22812#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022813 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022814#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022815 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022816 if (to->vval.v_channel != NULL)
22817 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022818 break;
22819#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 case VAR_STRING:
22821 case VAR_FUNC:
22822 if (from->vval.v_string == NULL)
22823 to->vval.v_string = NULL;
22824 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022825 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022826 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827 if (from->v_type == VAR_FUNC)
22828 func_ref(to->vval.v_string);
22829 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022830 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022831 case VAR_PARTIAL:
22832 if (from->vval.v_partial == NULL)
22833 to->vval.v_partial = NULL;
22834 else
22835 {
22836 to->vval.v_partial = from->vval.v_partial;
22837 ++to->vval.v_partial->pt_refcount;
22838 }
22839 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022840 case VAR_LIST:
22841 if (from->vval.v_list == NULL)
22842 to->vval.v_list = NULL;
22843 else
22844 {
22845 to->vval.v_list = from->vval.v_list;
22846 ++to->vval.v_list->lv_refcount;
22847 }
22848 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022849 case VAR_DICT:
22850 if (from->vval.v_dict == NULL)
22851 to->vval.v_dict = NULL;
22852 else
22853 {
22854 to->vval.v_dict = from->vval.v_dict;
22855 ++to->vval.v_dict->dv_refcount;
22856 }
22857 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022858 case VAR_UNKNOWN:
22859 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022860 break;
22861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862}
22863
22864/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022865 * Make a copy of an item.
22866 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022867 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22868 * reference to an already copied list/dict can be used.
22869 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022870 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022872item_copy(
22873 typval_T *from,
22874 typval_T *to,
22875 int deep,
22876 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022877{
22878 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022879 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022880
Bram Moolenaar33570922005-01-25 22:26:29 +000022881 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022882 {
22883 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022884 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022885 }
22886 ++recurse;
22887
22888 switch (from->v_type)
22889 {
22890 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022891 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022892 case VAR_STRING:
22893 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022894 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022895 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022896 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022897 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022898 copy_tv(from, to);
22899 break;
22900 case VAR_LIST:
22901 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022902 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022903 if (from->vval.v_list == NULL)
22904 to->vval.v_list = NULL;
22905 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22906 {
22907 /* use the copy made earlier */
22908 to->vval.v_list = from->vval.v_list->lv_copylist;
22909 ++to->vval.v_list->lv_refcount;
22910 }
22911 else
22912 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22913 if (to->vval.v_list == NULL)
22914 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022915 break;
22916 case VAR_DICT:
22917 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022918 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022919 if (from->vval.v_dict == NULL)
22920 to->vval.v_dict = NULL;
22921 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22922 {
22923 /* use the copy made earlier */
22924 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22925 ++to->vval.v_dict->dv_refcount;
22926 }
22927 else
22928 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22929 if (to->vval.v_dict == NULL)
22930 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022931 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022932 case VAR_UNKNOWN:
22933 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022934 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022935 }
22936 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022937 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022938}
22939
22940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 * ":echo expr1 ..." print each argument separated with a space, add a
22942 * newline at the end.
22943 * ":echon expr1 ..." print each argument plain.
22944 */
22945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022946ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947{
22948 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022949 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022950 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 char_u *p;
22952 int needclr = TRUE;
22953 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022954 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955
22956 if (eap->skip)
22957 ++emsg_skip;
22958 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022960 /* If eval1() causes an error message the text from the command may
22961 * still need to be cleared. E.g., "echo 22,44". */
22962 need_clr_eos = needclr;
22963
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022965 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 {
22967 /*
22968 * Report the invalid expression unless the expression evaluation
22969 * has been cancelled due to an aborting error, an interrupt, or an
22970 * exception.
22971 */
22972 if (!aborting())
22973 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022974 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 break;
22976 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022977 need_clr_eos = FALSE;
22978
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 if (!eap->skip)
22980 {
22981 if (atstart)
22982 {
22983 atstart = FALSE;
22984 /* Call msg_start() after eval1(), evaluating the expression
22985 * may cause a message to appear. */
22986 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022987 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022988 /* Mark the saved text as finishing the line, so that what
22989 * follows is displayed on a new line when scrolling back
22990 * at the more prompt. */
22991 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 }
22995 else if (eap->cmdidx == CMD_echo)
22996 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022997 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022998 if (p != NULL)
22999 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023003 if (*p != TAB && needclr)
23004 {
23005 /* remove any text still there from the command */
23006 msg_clr_eos();
23007 needclr = FALSE;
23008 }
23009 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 }
23011 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023012 {
23013#ifdef FEAT_MBYTE
23014 if (has_mbyte)
23015 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023016 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023017
23018 (void)msg_outtrans_len_attr(p, i, echo_attr);
23019 p += i - 1;
23020 }
23021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023023 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023026 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 arg = skipwhite(arg);
23030 }
23031 eap->nextcmd = check_nextcmd(arg);
23032
23033 if (eap->skip)
23034 --emsg_skip;
23035 else
23036 {
23037 /* remove text that may still be there from the command */
23038 if (needclr)
23039 msg_clr_eos();
23040 if (eap->cmdidx == CMD_echo)
23041 msg_end();
23042 }
23043}
23044
23045/*
23046 * ":echohl {name}".
23047 */
23048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023049ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050{
23051 int id;
23052
23053 id = syn_name2id(eap->arg);
23054 if (id == 0)
23055 echo_attr = 0;
23056 else
23057 echo_attr = syn_id2attr(id);
23058}
23059
23060/*
23061 * ":execute expr1 ..." execute the result of an expression.
23062 * ":echomsg expr1 ..." Print a message
23063 * ":echoerr expr1 ..." Print an error
23064 * Each gets spaces around each argument and a newline at the end for
23065 * echo commands
23066 */
23067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023068ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069{
23070 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 int ret = OK;
23073 char_u *p;
23074 garray_T ga;
23075 int len;
23076 int save_did_emsg;
23077
23078 ga_init2(&ga, 1, 80);
23079
23080 if (eap->skip)
23081 ++emsg_skip;
23082 while (*arg != NUL && *arg != '|' && *arg != '\n')
23083 {
23084 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023085 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 {
23087 /*
23088 * Report the invalid expression unless the expression evaluation
23089 * has been cancelled due to an aborting error, an interrupt, or an
23090 * exception.
23091 */
23092 if (!aborting())
23093 EMSG2(_(e_invexpr2), p);
23094 ret = FAIL;
23095 break;
23096 }
23097
23098 if (!eap->skip)
23099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023100 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 len = (int)STRLEN(p);
23102 if (ga_grow(&ga, len + 2) == FAIL)
23103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023105 ret = FAIL;
23106 break;
23107 }
23108 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 ga.ga_len += len;
23112 }
23113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 arg = skipwhite(arg);
23116 }
23117
23118 if (ret != FAIL && ga.ga_data != NULL)
23119 {
23120 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023123 out_flush();
23124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 else if (eap->cmdidx == CMD_echoerr)
23126 {
23127 /* We don't want to abort following commands, restore did_emsg. */
23128 save_did_emsg = did_emsg;
23129 EMSG((char_u *)ga.ga_data);
23130 if (!force_abort)
23131 did_emsg = save_did_emsg;
23132 }
23133 else if (eap->cmdidx == CMD_execute)
23134 do_cmdline((char_u *)ga.ga_data,
23135 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23136 }
23137
23138 ga_clear(&ga);
23139
23140 if (eap->skip)
23141 --emsg_skip;
23142
23143 eap->nextcmd = check_nextcmd(arg);
23144}
23145
23146/*
23147 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23148 * "arg" points to the "&" or '+' when called, to "option" when returning.
23149 * Returns NULL when no option name found. Otherwise pointer to the char
23150 * after the option name.
23151 */
23152 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023153find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154{
23155 char_u *p = *arg;
23156
23157 ++p;
23158 if (*p == 'g' && p[1] == ':')
23159 {
23160 *opt_flags = OPT_GLOBAL;
23161 p += 2;
23162 }
23163 else if (*p == 'l' && p[1] == ':')
23164 {
23165 *opt_flags = OPT_LOCAL;
23166 p += 2;
23167 }
23168 else
23169 *opt_flags = 0;
23170
23171 if (!ASCII_ISALPHA(*p))
23172 return NULL;
23173 *arg = p;
23174
23175 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23176 p += 4; /* termcap option */
23177 else
23178 while (ASCII_ISALPHA(*p))
23179 ++p;
23180 return p;
23181}
23182
23183/*
23184 * ":function"
23185 */
23186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023187ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188{
23189 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023190 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 int j;
23192 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023194 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 char_u *name = NULL;
23196 char_u *p;
23197 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023198 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 garray_T newargs;
23200 garray_T newlines;
23201 int varargs = FALSE;
23202 int mustend = FALSE;
23203 int flags = 0;
23204 ufunc_T *fp;
23205 int indent;
23206 int nesting;
23207 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023208 dictitem_T *v;
23209 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023210 static int func_nr = 0; /* number for nameless function */
23211 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023212 hashtab_T *ht;
23213 int todo;
23214 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023215 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216
23217 /*
23218 * ":function" without argument: list functions.
23219 */
23220 if (ends_excmd(*eap->arg))
23221 {
23222 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023224 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023225 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023226 {
23227 if (!HASHITEM_EMPTY(hi))
23228 {
23229 --todo;
23230 fp = HI2UF(hi);
23231 if (!isdigit(*fp->uf_name))
23232 list_func_head(fp, FALSE);
23233 }
23234 }
23235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 eap->nextcmd = check_nextcmd(eap->arg);
23237 return;
23238 }
23239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023240 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023241 * ":function /pat": list functions matching pattern.
23242 */
23243 if (*eap->arg == '/')
23244 {
23245 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23246 if (!eap->skip)
23247 {
23248 regmatch_T regmatch;
23249
23250 c = *p;
23251 *p = NUL;
23252 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23253 *p = c;
23254 if (regmatch.regprog != NULL)
23255 {
23256 regmatch.rm_ic = p_ic;
23257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023258 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023259 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23260 {
23261 if (!HASHITEM_EMPTY(hi))
23262 {
23263 --todo;
23264 fp = HI2UF(hi);
23265 if (!isdigit(*fp->uf_name)
23266 && vim_regexec(&regmatch, fp->uf_name, 0))
23267 list_func_head(fp, FALSE);
23268 }
23269 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023270 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023271 }
23272 }
23273 if (*p == '/')
23274 ++p;
23275 eap->nextcmd = check_nextcmd(p);
23276 return;
23277 }
23278
23279 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023280 * Get the function name. There are these situations:
23281 * func normal function name
23282 * "name" == func, "fudi.fd_dict" == NULL
23283 * dict.func new dictionary entry
23284 * "name" == NULL, "fudi.fd_dict" set,
23285 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23286 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023287 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023288 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23289 * dict.func existing dict entry that's not a Funcref
23290 * "name" == NULL, "fudi.fd_dict" set,
23291 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023292 * s:func script-local function name
23293 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023296 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023297 paren = (vim_strchr(p, '(') != NULL);
23298 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 {
23300 /*
23301 * Return on an invalid expression in braces, unless the expression
23302 * evaluation has been cancelled due to an aborting error, an
23303 * interrupt, or an exception.
23304 */
23305 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023307 if (!eap->skip && fudi.fd_newkey != NULL)
23308 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023309 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 else
23313 eap->skip = TRUE;
23314 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023315
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 /* An error in a function call during evaluation of an expression in magic
23317 * braces should not cause the function not to be defined. */
23318 saved_did_emsg = did_emsg;
23319 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320
23321 /*
23322 * ":function func" with only function name: list function.
23323 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 {
23326 if (!ends_excmd(*skipwhite(p)))
23327 {
23328 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023329 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 }
23331 eap->nextcmd = check_nextcmd(p);
23332 if (eap->nextcmd != NULL)
23333 *p = NUL;
23334 if (!eap->skip && !got_int)
23335 {
23336 fp = find_func(name);
23337 if (fp != NULL)
23338 {
23339 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023340 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023342 if (FUNCLINE(fp, j) == NULL)
23343 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023344 msg_putchar('\n');
23345 msg_outnum((long)(j + 1));
23346 if (j < 9)
23347 msg_putchar(' ');
23348 if (j < 99)
23349 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023350 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 out_flush(); /* show a line at a time */
23352 ui_breakcheck();
23353 }
23354 if (!got_int)
23355 {
23356 msg_putchar('\n');
23357 msg_puts((char_u *)" endfunction");
23358 }
23359 }
23360 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023361 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023363 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 }
23365
23366 /*
23367 * ":function name(arg1, arg2)" Define function.
23368 */
23369 p = skipwhite(p);
23370 if (*p != '(')
23371 {
23372 if (!eap->skip)
23373 {
23374 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023375 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
23377 /* attempt to continue by skipping some text */
23378 if (vim_strchr(p, '(') != NULL)
23379 p = vim_strchr(p, '(');
23380 }
23381 p = skipwhite(p + 1);
23382
23383 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23384 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23385
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023386 if (!eap->skip)
23387 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023388 /* Check the name of the function. Unless it's a dictionary function
23389 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023390 if (name != NULL)
23391 arg = name;
23392 else
23393 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023394 if (arg != NULL && (fudi.fd_di == NULL
23395 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023396 {
23397 if (*arg == K_SPECIAL)
23398 j = 3;
23399 else
23400 j = 0;
23401 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23402 : eval_isnamec(arg[j])))
23403 ++j;
23404 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023405 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023406 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023407 /* Disallow using the g: dict. */
23408 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23409 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023410 }
23411
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 /*
23413 * Isolate the arguments: "arg1, arg2, ...)"
23414 */
23415 while (*p != ')')
23416 {
23417 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23418 {
23419 varargs = TRUE;
23420 p += 3;
23421 mustend = TRUE;
23422 }
23423 else
23424 {
23425 arg = p;
23426 while (ASCII_ISALNUM(*p) || *p == '_')
23427 ++p;
23428 if (arg == p || isdigit(*arg)
23429 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23430 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23431 {
23432 if (!eap->skip)
23433 EMSG2(_("E125: Illegal argument: %s"), arg);
23434 break;
23435 }
23436 if (ga_grow(&newargs, 1) == FAIL)
23437 goto erret;
23438 c = *p;
23439 *p = NUL;
23440 arg = vim_strsave(arg);
23441 if (arg == NULL)
23442 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023443
23444 /* Check for duplicate argument name. */
23445 for (i = 0; i < newargs.ga_len; ++i)
23446 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23447 {
23448 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023449 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023450 goto erret;
23451 }
23452
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23454 *p = c;
23455 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 if (*p == ',')
23457 ++p;
23458 else
23459 mustend = TRUE;
23460 }
23461 p = skipwhite(p);
23462 if (mustend && *p != ')')
23463 {
23464 if (!eap->skip)
23465 EMSG2(_(e_invarg2), eap->arg);
23466 break;
23467 }
23468 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023469 if (*p != ')')
23470 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 ++p; /* skip the ')' */
23472
Bram Moolenaare9a41262005-01-15 22:18:47 +000023473 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 for (;;)
23475 {
23476 p = skipwhite(p);
23477 if (STRNCMP(p, "range", 5) == 0)
23478 {
23479 flags |= FC_RANGE;
23480 p += 5;
23481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023482 else if (STRNCMP(p, "dict", 4) == 0)
23483 {
23484 flags |= FC_DICT;
23485 p += 4;
23486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 else if (STRNCMP(p, "abort", 5) == 0)
23488 {
23489 flags |= FC_ABORT;
23490 p += 5;
23491 }
23492 else
23493 break;
23494 }
23495
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023496 /* When there is a line break use what follows for the function body.
23497 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23498 if (*p == '\n')
23499 line_arg = p + 1;
23500 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 EMSG(_(e_trailing));
23502
23503 /*
23504 * Read the body of the function, until ":endfunction" is found.
23505 */
23506 if (KeyTyped)
23507 {
23508 /* Check if the function already exists, don't let the user type the
23509 * whole function before telling him it doesn't work! For a script we
23510 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511 if (!eap->skip && !eap->forceit)
23512 {
23513 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23514 EMSG(_(e_funcdict));
23515 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023516 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023519 if (!eap->skip && did_emsg)
23520 goto erret;
23521
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 msg_putchar('\n'); /* don't overwrite the function name */
23523 cmdline_row = msg_row;
23524 }
23525
23526 indent = 2;
23527 nesting = 0;
23528 for (;;)
23529 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023530 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023531 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023532 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023533 saved_wait_return = FALSE;
23534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023536 sourcing_lnum_off = sourcing_lnum;
23537
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023538 if (line_arg != NULL)
23539 {
23540 /* Use eap->arg, split up in parts by line breaks. */
23541 theline = line_arg;
23542 p = vim_strchr(theline, '\n');
23543 if (p == NULL)
23544 line_arg += STRLEN(line_arg);
23545 else
23546 {
23547 *p = NUL;
23548 line_arg = p + 1;
23549 }
23550 }
23551 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 theline = getcmdline(':', 0L, indent);
23553 else
23554 theline = eap->getline(':', eap->cookie, indent);
23555 if (KeyTyped)
23556 lines_left = Rows - 1;
23557 if (theline == NULL)
23558 {
23559 EMSG(_("E126: Missing :endfunction"));
23560 goto erret;
23561 }
23562
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023563 /* Detect line continuation: sourcing_lnum increased more than one. */
23564 if (sourcing_lnum > sourcing_lnum_off + 1)
23565 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23566 else
23567 sourcing_lnum_off = 0;
23568
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 if (skip_until != NULL)
23570 {
23571 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23572 * don't check for ":endfunc". */
23573 if (STRCMP(theline, skip_until) == 0)
23574 {
23575 vim_free(skip_until);
23576 skip_until = NULL;
23577 }
23578 }
23579 else
23580 {
23581 /* skip ':' and blanks*/
23582 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23583 ;
23584
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023585 /* Check for "endfunction". */
23586 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023588 if (line_arg == NULL)
23589 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 break;
23591 }
23592
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023593 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 * at "end". */
23595 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23596 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023597 else if (STRNCMP(p, "if", 2) == 0
23598 || STRNCMP(p, "wh", 2) == 0
23599 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 || STRNCMP(p, "try", 3) == 0)
23601 indent += 2;
23602
23603 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023604 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023606 if (*p == '!')
23607 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023609 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023610 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023612 ++nesting;
23613 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 }
23615 }
23616
23617 /* Check for ":append" or ":insert". */
23618 p = skip_range(p, NULL);
23619 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23620 || (p[0] == 'i'
23621 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23622 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23623 skip_until = vim_strsave((char_u *)".");
23624
23625 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23626 arg = skipwhite(skiptowhite(p));
23627 if (arg[0] == '<' && arg[1] =='<'
23628 && ((p[0] == 'p' && p[1] == 'y'
23629 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23630 || (p[0] == 'p' && p[1] == 'e'
23631 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23632 || (p[0] == 't' && p[1] == 'c'
23633 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023634 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23635 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23637 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023638 || (p[0] == 'm' && p[1] == 'z'
23639 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 ))
23641 {
23642 /* ":python <<" continues until a dot, like ":append" */
23643 p = skipwhite(arg + 2);
23644 if (*p == NUL)
23645 skip_until = vim_strsave((char_u *)".");
23646 else
23647 skip_until = vim_strsave(p);
23648 }
23649 }
23650
23651 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023652 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023653 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023654 if (line_arg == NULL)
23655 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023657 }
23658
23659 /* Copy the line to newly allocated memory. get_one_sourceline()
23660 * allocates 250 bytes per line, this saves 80% on average. The cost
23661 * is an extra alloc/free. */
23662 p = vim_strsave(theline);
23663 if (p != NULL)
23664 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023665 if (line_arg == NULL)
23666 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023667 theline = p;
23668 }
23669
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023670 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23671
23672 /* Add NULL lines for continuation lines, so that the line count is
23673 * equal to the index in the growarray. */
23674 while (sourcing_lnum_off-- > 0)
23675 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023676
23677 /* Check for end of eap->arg. */
23678 if (line_arg != NULL && *line_arg == NUL)
23679 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 }
23681
23682 /* Don't define the function when skipping commands or when an error was
23683 * detected. */
23684 if (eap->skip || did_emsg)
23685 goto erret;
23686
23687 /*
23688 * If there are no errors, add the function
23689 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023690 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023691 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023692 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023693 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023694 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023695 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023696 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023697 goto erret;
23698 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023699
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023700 fp = find_func(name);
23701 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023703 if (!eap->forceit)
23704 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023705 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023706 goto erret;
23707 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023708 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023709 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023710 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023711 name);
23712 goto erret;
23713 }
23714 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023715 ga_clear_strings(&(fp->uf_args));
23716 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023717 vim_free(name);
23718 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 }
23721 else
23722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023723 char numbuf[20];
23724
23725 fp = NULL;
23726 if (fudi.fd_newkey == NULL && !eap->forceit)
23727 {
23728 EMSG(_(e_funcdict));
23729 goto erret;
23730 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023731 if (fudi.fd_di == NULL)
23732 {
23733 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023734 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023735 goto erret;
23736 }
23737 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023738 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023739 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023740
23741 /* Give the function a sequential number. Can only be used with a
23742 * Funcref! */
23743 vim_free(name);
23744 sprintf(numbuf, "%d", ++func_nr);
23745 name = vim_strsave((char_u *)numbuf);
23746 if (name == NULL)
23747 goto erret;
23748 }
23749
23750 if (fp == NULL)
23751 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023752 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023753 {
23754 int slen, plen;
23755 char_u *scriptname;
23756
23757 /* Check that the autoload name matches the script name. */
23758 j = FAIL;
23759 if (sourcing_name != NULL)
23760 {
23761 scriptname = autoload_name(name);
23762 if (scriptname != NULL)
23763 {
23764 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023765 plen = (int)STRLEN(p);
23766 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023767 if (slen > plen && fnamecmp(p,
23768 sourcing_name + slen - plen) == 0)
23769 j = OK;
23770 vim_free(scriptname);
23771 }
23772 }
23773 if (j == FAIL)
23774 {
23775 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23776 goto erret;
23777 }
23778 }
23779
23780 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023781 if (fp == NULL)
23782 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023783
23784 if (fudi.fd_dict != NULL)
23785 {
23786 if (fudi.fd_di == NULL)
23787 {
23788 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023789 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023790 if (fudi.fd_di == NULL)
23791 {
23792 vim_free(fp);
23793 goto erret;
23794 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023795 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23796 {
23797 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023798 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023799 goto erret;
23800 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023801 }
23802 else
23803 /* overwrite existing dict entry */
23804 clear_tv(&fudi.fd_di->di_tv);
23805 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023806 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023807 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023808 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023809
23810 /* behave like "dict" was used */
23811 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023812 }
23813
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023815 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023816 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23817 {
23818 vim_free(fp);
23819 goto erret;
23820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023822 fp->uf_args = newargs;
23823 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023824#ifdef FEAT_PROFILE
23825 fp->uf_tml_count = NULL;
23826 fp->uf_tml_total = NULL;
23827 fp->uf_tml_self = NULL;
23828 fp->uf_profiling = FALSE;
23829 if (prof_def_func())
23830 func_do_profile(fp);
23831#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023832 fp->uf_varargs = varargs;
23833 fp->uf_flags = flags;
23834 fp->uf_calls = 0;
23835 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837
23838erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 ga_clear_strings(&newargs);
23840 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023841ret_free:
23842 vim_free(skip_until);
23843 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023846 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847}
23848
23849/*
23850 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023851 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023854 * TFN_INT: internal function name OK
23855 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023856 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 * Advances "pp" to just after the function name (if no error).
23858 */
23859 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023860trans_function_name(
23861 char_u **pp,
23862 int skip, /* only find the end, don't evaluate */
23863 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023864 funcdict_T *fdp, /* return: info about dictionary used */
23865 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023867 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 char_u *start;
23869 char_u *end;
23870 int lead;
23871 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023873 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023874
23875 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023876 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023878
23879 /* Check for hard coded <SNR>: already translated function ID (from a user
23880 * command). */
23881 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23882 && (*pp)[2] == (int)KE_SNR)
23883 {
23884 *pp += 3;
23885 len = get_id_len(pp) + 3;
23886 return vim_strnsave(start, len);
23887 }
23888
23889 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23890 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023892 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023894
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023895 /* Note that TFN_ flags use the same values as GLV_ flags. */
23896 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023897 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023898 if (end == start)
23899 {
23900 if (!skip)
23901 EMSG(_("E129: Function name required"));
23902 goto theend;
23903 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023904 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023905 {
23906 /*
23907 * Report an invalid expression in braces, unless the expression
23908 * evaluation has been cancelled due to an aborting error, an
23909 * interrupt, or an exception.
23910 */
23911 if (!aborting())
23912 {
23913 if (end != NULL)
23914 EMSG2(_(e_invarg2), start);
23915 }
23916 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023917 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023918 goto theend;
23919 }
23920
23921 if (lv.ll_tv != NULL)
23922 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023923 if (fdp != NULL)
23924 {
23925 fdp->fd_dict = lv.ll_dict;
23926 fdp->fd_newkey = lv.ll_newkey;
23927 lv.ll_newkey = NULL;
23928 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023930 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23931 {
23932 name = vim_strsave(lv.ll_tv->vval.v_string);
23933 *pp = end;
23934 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023935 else if (lv.ll_tv->v_type == VAR_PARTIAL
23936 && lv.ll_tv->vval.v_partial != NULL)
23937 {
23938 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23939 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010023940 if (partial != NULL)
23941 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010023942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023943 else
23944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023945 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23946 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023947 EMSG(_(e_funcref));
23948 else
23949 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023950 name = NULL;
23951 }
23952 goto theend;
23953 }
23954
23955 if (lv.ll_name == NULL)
23956 {
23957 /* Error found, but continue after the function name. */
23958 *pp = end;
23959 goto theend;
23960 }
23961
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023962 /* Check if the name is a Funcref. If so, use the value. */
23963 if (lv.ll_exp_name != NULL)
23964 {
23965 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023966 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023967 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023968 if (name == lv.ll_exp_name)
23969 name = NULL;
23970 }
23971 else
23972 {
23973 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023974 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023975 if (name == *pp)
23976 name = NULL;
23977 }
23978 if (name != NULL)
23979 {
23980 name = vim_strsave(name);
23981 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023982 if (STRNCMP(name, "<SNR>", 5) == 0)
23983 {
23984 /* Change "<SNR>" to the byte sequence. */
23985 name[0] = K_SPECIAL;
23986 name[1] = KS_EXTRA;
23987 name[2] = (int)KE_SNR;
23988 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23989 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023990 goto theend;
23991 }
23992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023993 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023994 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023995 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023996 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23997 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23998 {
23999 /* When there was "s:" already or the name expanded to get a
24000 * leading "s:" then remove it. */
24001 lv.ll_name += 2;
24002 len -= 2;
24003 lead = 2;
24004 }
24005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024006 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024007 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024008 /* skip over "s:" and "g:" */
24009 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024010 lv.ll_name += 2;
24011 len = (int)(end - lv.ll_name);
24012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024013
24014 /*
24015 * Copy the function name to allocated memory.
24016 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24017 * Accept <SNR>123_name() outside a script.
24018 */
24019 if (skip)
24020 lead = 0; /* do nothing */
24021 else if (lead > 0)
24022 {
24023 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024024 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24025 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024026 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024027 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024028 if (current_SID <= 0)
24029 {
24030 EMSG(_(e_usingsid));
24031 goto theend;
24032 }
24033 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24034 lead += (int)STRLEN(sid_buf);
24035 }
24036 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024037 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024038 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024039 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024040 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024041 goto theend;
24042 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024043 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024044 {
24045 char_u *cp = vim_strchr(lv.ll_name, ':');
24046
24047 if (cp != NULL && cp < end)
24048 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024049 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024050 goto theend;
24051 }
24052 }
24053
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024054 name = alloc((unsigned)(len + lead + 1));
24055 if (name != NULL)
24056 {
24057 if (lead > 0)
24058 {
24059 name[0] = K_SPECIAL;
24060 name[1] = KS_EXTRA;
24061 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024062 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024063 STRCPY(name + 3, sid_buf);
24064 }
24065 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024066 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024067 }
24068 *pp = end;
24069
24070theend:
24071 clear_lval(&lv);
24072 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073}
24074
24075/*
24076 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24077 * Return 2 if "p" starts with "s:".
24078 * Return 0 otherwise.
24079 */
24080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024081eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024083 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24084 * the standard library function. */
24085 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24086 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 return 5;
24088 if (p[0] == 's' && p[1] == ':')
24089 return 2;
24090 return 0;
24091}
24092
24093/*
24094 * Return TRUE if "p" starts with "<SID>" or "s:".
24095 * Only works if eval_fname_script() returned non-zero for "p"!
24096 */
24097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024098eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099{
24100 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24101}
24102
24103/*
24104 * List the head of the function: "name(arg1, arg2)".
24105 */
24106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024107list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108{
24109 int j;
24110
24111 msg_start();
24112 if (indent)
24113 MSG_PUTS(" ");
24114 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024115 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 {
24117 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 }
24120 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024121 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024123 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 {
24125 if (j)
24126 MSG_PUTS(", ");
24127 msg_puts(FUNCARG(fp, j));
24128 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024129 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 {
24131 if (j)
24132 MSG_PUTS(", ");
24133 MSG_PUTS("...");
24134 }
24135 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024136 if (fp->uf_flags & FC_ABORT)
24137 MSG_PUTS(" abort");
24138 if (fp->uf_flags & FC_RANGE)
24139 MSG_PUTS(" range");
24140 if (fp->uf_flags & FC_DICT)
24141 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024142 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024143 if (p_verbose > 0)
24144 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145}
24146
24147/*
24148 * Find a function by name, return pointer to it in ufuncs.
24149 * Return NULL for unknown function.
24150 */
24151 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024152find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024154 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024156 hi = hash_find(&func_hashtab, name);
24157 if (!HASHITEM_EMPTY(hi))
24158 return HI2UF(hi);
24159 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160}
24161
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024162#if defined(EXITFREE) || defined(PROTO)
24163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024164free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024165{
24166 hashitem_T *hi;
24167
24168 /* Need to start all over every time, because func_free() may change the
24169 * hash table. */
24170 while (func_hashtab.ht_used > 0)
24171 for (hi = func_hashtab.ht_array; ; ++hi)
24172 if (!HASHITEM_EMPTY(hi))
24173 {
24174 func_free(HI2UF(hi));
24175 break;
24176 }
24177}
24178#endif
24179
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024180 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024181translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024182{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024183 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024184 return find_internal_func(name) >= 0;
24185 return find_func(name) != NULL;
24186}
24187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024188/*
24189 * Return TRUE if a function "name" exists.
24190 */
24191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024192function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024193{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024194 char_u *nm = name;
24195 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024196 int n = FALSE;
24197
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024198 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024199 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024200 nm = skipwhite(nm);
24201
24202 /* Only accept "funcname", "funcname ", "funcname (..." and
24203 * "funcname(...", not "funcname!...". */
24204 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024205 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024206 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024207 return n;
24208}
24209
Bram Moolenaara1544c02013-05-30 12:35:52 +020024210 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024211get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024212{
24213 char_u *nm = name;
24214 char_u *p;
24215
Bram Moolenaar65639032016-03-16 21:40:30 +010024216 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024217
24218 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024219 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024220 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024221
Bram Moolenaara1544c02013-05-30 12:35:52 +020024222 vim_free(p);
24223 return NULL;
24224}
24225
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024226/*
24227 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024228 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24229 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024230 */
24231 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024232builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024233{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024234 char_u *p;
24235
24236 if (!ASCII_ISLOWER(name[0]))
24237 return FALSE;
24238 p = vim_strchr(name, AUTOLOAD_CHAR);
24239 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024240}
24241
Bram Moolenaar05159a02005-02-26 23:04:13 +000024242#if defined(FEAT_PROFILE) || defined(PROTO)
24243/*
24244 * Start profiling function "fp".
24245 */
24246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024247func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024248{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024249 int len = fp->uf_lines.ga_len;
24250
24251 if (len == 0)
24252 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024253 fp->uf_tm_count = 0;
24254 profile_zero(&fp->uf_tm_self);
24255 profile_zero(&fp->uf_tm_total);
24256 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024257 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024258 if (fp->uf_tml_total == NULL)
24259 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024260 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024261 if (fp->uf_tml_self == NULL)
24262 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
24265 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24266 || fp->uf_tml_self == NULL)
24267 return; /* out of memory */
24268
24269 fp->uf_profiling = TRUE;
24270}
24271
24272/*
24273 * Dump the profiling results for all functions in file "fd".
24274 */
24275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024277{
24278 hashitem_T *hi;
24279 int todo;
24280 ufunc_T *fp;
24281 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024282 ufunc_T **sorttab;
24283 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024285 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024286 if (todo == 0)
24287 return; /* nothing to dump */
24288
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024289 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024290
Bram Moolenaar05159a02005-02-26 23:04:13 +000024291 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24292 {
24293 if (!HASHITEM_EMPTY(hi))
24294 {
24295 --todo;
24296 fp = HI2UF(hi);
24297 if (fp->uf_profiling)
24298 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024299 if (sorttab != NULL)
24300 sorttab[st_len++] = fp;
24301
Bram Moolenaar05159a02005-02-26 23:04:13 +000024302 if (fp->uf_name[0] == K_SPECIAL)
24303 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24304 else
24305 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24306 if (fp->uf_tm_count == 1)
24307 fprintf(fd, "Called 1 time\n");
24308 else
24309 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24310 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24311 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24312 fprintf(fd, "\n");
24313 fprintf(fd, "count total (s) self (s)\n");
24314
24315 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24316 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024317 if (FUNCLINE(fp, i) == NULL)
24318 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024319 prof_func_line(fd, fp->uf_tml_count[i],
24320 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024321 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24322 }
24323 fprintf(fd, "\n");
24324 }
24325 }
24326 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024327
24328 if (sorttab != NULL && st_len > 0)
24329 {
24330 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24331 prof_total_cmp);
24332 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24333 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24334 prof_self_cmp);
24335 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24336 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024337
24338 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024339}
Bram Moolenaar73830342005-02-28 22:48:19 +000024340
24341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024342prof_sort_list(
24343 FILE *fd,
24344 ufunc_T **sorttab,
24345 int st_len,
24346 char *title,
24347 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024348{
24349 int i;
24350 ufunc_T *fp;
24351
24352 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24353 fprintf(fd, "count total (s) self (s) function\n");
24354 for (i = 0; i < 20 && i < st_len; ++i)
24355 {
24356 fp = sorttab[i];
24357 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24358 prefer_self);
24359 if (fp->uf_name[0] == K_SPECIAL)
24360 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24361 else
24362 fprintf(fd, " %s()\n", fp->uf_name);
24363 }
24364 fprintf(fd, "\n");
24365}
24366
24367/*
24368 * Print the count and times for one function or function line.
24369 */
24370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024371prof_func_line(
24372 FILE *fd,
24373 int count,
24374 proftime_T *total,
24375 proftime_T *self,
24376 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024377{
24378 if (count > 0)
24379 {
24380 fprintf(fd, "%5d ", count);
24381 if (prefer_self && profile_equal(total, self))
24382 fprintf(fd, " ");
24383 else
24384 fprintf(fd, "%s ", profile_msg(total));
24385 if (!prefer_self && profile_equal(total, self))
24386 fprintf(fd, " ");
24387 else
24388 fprintf(fd, "%s ", profile_msg(self));
24389 }
24390 else
24391 fprintf(fd, " ");
24392}
24393
24394/*
24395 * Compare function for total time sorting.
24396 */
24397 static int
24398#ifdef __BORLANDC__
24399_RTLENTRYF
24400#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024401prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024402{
24403 ufunc_T *p1, *p2;
24404
24405 p1 = *(ufunc_T **)s1;
24406 p2 = *(ufunc_T **)s2;
24407 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24408}
24409
24410/*
24411 * Compare function for self time sorting.
24412 */
24413 static int
24414#ifdef __BORLANDC__
24415_RTLENTRYF
24416#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024417prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024418{
24419 ufunc_T *p1, *p2;
24420
24421 p1 = *(ufunc_T **)s1;
24422 p2 = *(ufunc_T **)s2;
24423 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24424}
24425
Bram Moolenaar05159a02005-02-26 23:04:13 +000024426#endif
24427
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024428/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024429 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024430 * Return TRUE if a package was loaded.
24431 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024433script_autoload(
24434 char_u *name,
24435 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024436{
24437 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024438 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024439 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024440 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024441
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024442 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024443 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024444 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024445 return FALSE;
24446
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024447 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024448
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024449 /* Find the name in the list of previously loaded package names. Skip
24450 * "autoload/", it's always the same. */
24451 for (i = 0; i < ga_loaded.ga_len; ++i)
24452 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24453 break;
24454 if (!reload && i < ga_loaded.ga_len)
24455 ret = FALSE; /* was loaded already */
24456 else
24457 {
24458 /* Remember the name if it wasn't loaded already. */
24459 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24460 {
24461 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24462 tofree = NULL;
24463 }
24464
24465 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024466 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024467 ret = TRUE;
24468 }
24469
24470 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024471 return ret;
24472}
24473
24474/*
24475 * Return the autoload script name for a function or variable name.
24476 * Returns NULL when out of memory.
24477 */
24478 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024479autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024480{
24481 char_u *p;
24482 char_u *scriptname;
24483
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024484 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024485 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24486 if (scriptname == NULL)
24487 return FALSE;
24488 STRCPY(scriptname, "autoload/");
24489 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024490 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024491 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024492 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024493 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024495}
24496
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24498
24499/*
24500 * Function given to ExpandGeneric() to obtain the list of user defined
24501 * function names.
24502 */
24503 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024504get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024506 static long_u done;
24507 static hashitem_T *hi;
24508 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509
24510 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024512 done = 0;
24513 hi = func_hashtab.ht_array;
24514 }
24515 if (done < func_hashtab.ht_used)
24516 {
24517 if (done++ > 0)
24518 ++hi;
24519 while (HASHITEM_EMPTY(hi))
24520 ++hi;
24521 fp = HI2UF(hi);
24522
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024523 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024524 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024525
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024526 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24527 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528
24529 cat_func_name(IObuff, fp);
24530 if (xp->xp_context != EXPAND_USER_FUNC)
24531 {
24532 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024533 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 STRCAT(IObuff, ")");
24535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 return IObuff;
24537 }
24538 return NULL;
24539}
24540
24541#endif /* FEAT_CMDL_COMPL */
24542
24543/*
24544 * Copy the function name of "fp" to buffer "buf".
24545 * "buf" must be able to hold the function name plus three bytes.
24546 * Takes care of script-local function names.
24547 */
24548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024549cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024551 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024552 {
24553 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024554 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 }
24556 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024557 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558}
24559
24560/*
24561 * ":delfunction {name}"
24562 */
24563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024564ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 char_u *p;
24568 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024569 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570
24571 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024572 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024573 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024575 {
24576 if (fudi.fd_dict != NULL && !eap->skip)
24577 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 if (!ends_excmd(*skipwhite(p)))
24581 {
24582 vim_free(name);
24583 EMSG(_(e_trailing));
24584 return;
24585 }
24586 eap->nextcmd = check_nextcmd(p);
24587 if (eap->nextcmd != NULL)
24588 *p = NUL;
24589
24590 if (!eap->skip)
24591 fp = find_func(name);
24592 vim_free(name);
24593
24594 if (!eap->skip)
24595 {
24596 if (fp == NULL)
24597 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024598 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 return;
24600 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024601 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602 {
24603 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24604 return;
24605 }
24606
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024607 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024609 /* Delete the dict item that refers to the function, it will
24610 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024611 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024613 else
24614 func_free(fp);
24615 }
24616}
24617
24618/*
24619 * Free a function and remove it from the list of functions.
24620 */
24621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024622func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024623{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024624 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024625
24626 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024627 ga_clear_strings(&(fp->uf_args));
24628 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024629#ifdef FEAT_PROFILE
24630 vim_free(fp->uf_tml_count);
24631 vim_free(fp->uf_tml_total);
24632 vim_free(fp->uf_tml_self);
24633#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024634
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024635 /* remove the function from the function hashtable */
24636 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24637 if (HASHITEM_EMPTY(hi))
24638 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024639 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024640 hash_remove(&func_hashtab, hi);
24641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642 vim_free(fp);
24643}
24644
24645/*
24646 * Unreference a Function: decrement the reference count and free it when it
24647 * becomes zero. Only for numbered functions.
24648 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024650func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024651{
24652 ufunc_T *fp;
24653
24654 if (name != NULL && isdigit(*name))
24655 {
24656 fp = find_func(name);
24657 if (fp == NULL)
24658 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024659 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024660 {
24661 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024662 * when "uf_calls" becomes zero. */
24663 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024664 func_free(fp);
24665 }
24666 }
24667}
24668
24669/*
24670 * Count a reference to a Function.
24671 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024673func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024674{
24675 ufunc_T *fp;
24676
24677 if (name != NULL && isdigit(*name))
24678 {
24679 fp = find_func(name);
24680 if (fp == NULL)
24681 EMSG2(_(e_intern2), "func_ref()");
24682 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024683 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 }
24685}
24686
24687/*
24688 * Call a user function.
24689 */
24690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024691call_user_func(
24692 ufunc_T *fp, /* pointer to function */
24693 int argcount, /* nr of args */
24694 typval_T *argvars, /* arguments */
24695 typval_T *rettv, /* return value */
24696 linenr_T firstline, /* first line of range */
24697 linenr_T lastline, /* last line of range */
24698 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699{
Bram Moolenaar33570922005-01-25 22:26:29 +000024700 char_u *save_sourcing_name;
24701 linenr_T save_sourcing_lnum;
24702 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024703 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024704 int save_did_emsg;
24705 static int depth = 0;
24706 dictitem_T *v;
24707 int fixvar_idx = 0; /* index in fixvar[] */
24708 int i;
24709 int ai;
24710 char_u numbuf[NUMBUFLEN];
24711 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024712 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024713#ifdef FEAT_PROFILE
24714 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024715 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717
24718 /* If depth of calling is getting too high, don't execute the function */
24719 if (depth >= p_mfd)
24720 {
24721 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024722 rettv->v_type = VAR_NUMBER;
24723 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724 return;
24725 }
24726 ++depth;
24727
24728 line_breakcheck(); /* check for CTRL-C hit */
24729
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024730 fc = (funccall_T *)alloc(sizeof(funccall_T));
24731 fc->caller = current_funccal;
24732 current_funccal = fc;
24733 fc->func = fp;
24734 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024735 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024736 fc->linenr = 0;
24737 fc->returned = FALSE;
24738 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024740 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24741 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742
Bram Moolenaar33570922005-01-25 22:26:29 +000024743 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024744 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024745 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24746 * each argument variable and saves a lot of time.
24747 */
24748 /*
24749 * Init l: variables.
24750 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024751 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024752 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024753 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024754 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24755 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024756 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024757 name = v->di_key;
24758 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024759 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024760 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024761 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024762 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024763 v->di_tv.vval.v_dict = selfdict;
24764 ++selfdict->dv_refcount;
24765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024766
Bram Moolenaar33570922005-01-25 22:26:29 +000024767 /*
24768 * Init a: variables.
24769 * Set a:0 to "argcount".
24770 * Set a:000 to a list with room for the "..." arguments.
24771 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024772 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024773 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024774 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024775 /* Use "name" to avoid a warning from some compiler that checks the
24776 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024777 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024778 name = v->di_key;
24779 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024780 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024781 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024782 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024783 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024784 v->di_tv.vval.v_list = &fc->l_varlist;
24785 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24786 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24787 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024788
24789 /*
24790 * Set a:firstline to "firstline" and a:lastline to "lastline".
24791 * Set a:name to named arguments.
24792 * Set a:N to the "..." arguments.
24793 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024794 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024795 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024796 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024797 (varnumber_T)lastline);
24798 for (i = 0; i < argcount; ++i)
24799 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024800 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024801 if (ai < 0)
24802 /* named argument a:name */
24803 name = FUNCARG(fp, i);
24804 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024805 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024806 /* "..." argument a:1, a:2, etc. */
24807 sprintf((char *)numbuf, "%d", ai + 1);
24808 name = numbuf;
24809 }
24810 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24811 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024812 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024813 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24814 }
24815 else
24816 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024817 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24818 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024819 if (v == NULL)
24820 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024821 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024822 }
24823 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024824 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024825
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024826 /* Note: the values are copied directly to avoid alloc/free.
24827 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024829 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024830
24831 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24832 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024833 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24834 fc->l_listitems[ai].li_tv = argvars[i];
24835 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024836 }
24837 }
24838
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 /* Don't redraw while executing the function. */
24840 ++RedrawingDisabled;
24841 save_sourcing_name = sourcing_name;
24842 save_sourcing_lnum = sourcing_lnum;
24843 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024844 /* need space for function name + ("function " + 3) or "[number]" */
24845 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24846 + STRLEN(fp->uf_name) + 20;
24847 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 if (sourcing_name != NULL)
24849 {
24850 if (save_sourcing_name != NULL
24851 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024852 sprintf((char *)sourcing_name, "%s[%d]..",
24853 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 else
24855 STRCPY(sourcing_name, "function ");
24856 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24857
24858 if (p_verbose >= 12)
24859 {
24860 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024861 verbose_enter_scroll();
24862
Bram Moolenaar555b2802005-05-19 21:08:39 +000024863 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 if (p_verbose >= 14)
24865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024867 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024868 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024869 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870
24871 msg_puts((char_u *)"(");
24872 for (i = 0; i < argcount; ++i)
24873 {
24874 if (i > 0)
24875 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024876 if (argvars[i].v_type == VAR_NUMBER)
24877 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 else
24879 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024880 /* Do not want errors such as E724 here. */
24881 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024882 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024883 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024884 if (s != NULL)
24885 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024886 if (vim_strsize(s) > MSG_BUF_CLEN)
24887 {
24888 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24889 s = buf;
24890 }
24891 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024892 vim_free(tofree);
24893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 }
24895 }
24896 msg_puts((char_u *)")");
24897 }
24898 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024899
24900 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 --no_wait_return;
24902 }
24903 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024904#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024905 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024906 {
24907 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24908 func_do_profile(fp);
24909 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024910 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024911 {
24912 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024913 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024914 profile_zero(&fp->uf_tm_children);
24915 }
24916 script_prof_save(&wait_start);
24917 }
24918#endif
24919
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024921 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922 save_did_emsg = did_emsg;
24923 did_emsg = FALSE;
24924
24925 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024926 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024927 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24928
24929 --RedrawingDisabled;
24930
24931 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024932 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024934 clear_tv(rettv);
24935 rettv->v_type = VAR_NUMBER;
24936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 }
24938
Bram Moolenaar05159a02005-02-26 23:04:13 +000024939#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024940 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024941 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024942 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024943 profile_end(&call_start);
24944 profile_sub_wait(&wait_start, &call_start);
24945 profile_add(&fp->uf_tm_total, &call_start);
24946 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024947 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024948 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024949 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24950 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951 }
24952 }
24953#endif
24954
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 /* when being verbose, mention the return value */
24956 if (p_verbose >= 12)
24957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024959 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024962 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024963 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024964 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024965 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024968 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024969 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024970 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024971 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024972
Bram Moolenaar555b2802005-05-19 21:08:39 +000024973 /* The value may be very long. Skip the middle part, so that we
24974 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024975 * truncate it at the end. Don't want errors such as E724 here. */
24976 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024977 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024978 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024979 if (s != NULL)
24980 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024981 if (vim_strsize(s) > MSG_BUF_CLEN)
24982 {
24983 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24984 s = buf;
24985 }
24986 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024987 vim_free(tofree);
24988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989 }
24990 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024991
24992 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993 --no_wait_return;
24994 }
24995
24996 vim_free(sourcing_name);
24997 sourcing_name = save_sourcing_name;
24998 sourcing_lnum = save_sourcing_lnum;
24999 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025000#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025001 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025002 script_prof_restore(&wait_start);
25003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004
25005 if (p_verbose >= 12 && sourcing_name != NULL)
25006 {
25007 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025008 verbose_enter_scroll();
25009
Bram Moolenaar555b2802005-05-19 21:08:39 +000025010 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025012
25013 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025014 --no_wait_return;
25015 }
25016
25017 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025018 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025020
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025021 /* If the a:000 list and the l: and a: dicts are not referenced we can
25022 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025023 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25024 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25025 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25026 {
25027 free_funccal(fc, FALSE);
25028 }
25029 else
25030 {
25031 hashitem_T *hi;
25032 listitem_T *li;
25033 int todo;
25034
25035 /* "fc" is still in use. This can happen when returning "a:000" or
25036 * assigning "l:" to a global variable.
25037 * Link "fc" in the list for garbage collection later. */
25038 fc->caller = previous_funccal;
25039 previous_funccal = fc;
25040
25041 /* Make a copy of the a: variables, since we didn't do that above. */
25042 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25043 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25044 {
25045 if (!HASHITEM_EMPTY(hi))
25046 {
25047 --todo;
25048 v = HI2DI(hi);
25049 copy_tv(&v->di_tv, &v->di_tv);
25050 }
25051 }
25052
25053 /* Make a copy of the a:000 items, since we didn't do that above. */
25054 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25055 copy_tv(&li->li_tv, &li->li_tv);
25056 }
25057}
25058
25059/*
25060 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025061 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025062 */
25063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025064can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025065{
25066 return (fc->l_varlist.lv_copyID != copyID
25067 && fc->l_vars.dv_copyID != copyID
25068 && fc->l_avars.dv_copyID != copyID);
25069}
25070
25071/*
25072 * Free "fc" and what it contains.
25073 */
25074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025075free_funccal(
25076 funccall_T *fc,
25077 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025078{
25079 listitem_T *li;
25080
25081 /* The a: variables typevals may not have been allocated, only free the
25082 * allocated variables. */
25083 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25084
25085 /* free all l: variables */
25086 vars_clear(&fc->l_vars.dv_hashtab);
25087
25088 /* Free the a:000 variables if they were allocated. */
25089 if (free_val)
25090 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25091 clear_tv(&li->li_tv);
25092
25093 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094}
25095
25096/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025097 * Add a number variable "name" to dict "dp" with value "nr".
25098 */
25099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025100add_nr_var(
25101 dict_T *dp,
25102 dictitem_T *v,
25103 char *name,
25104 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025105{
25106 STRCPY(v->di_key, name);
25107 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25108 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25109 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025110 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025111 v->di_tv.vval.v_number = nr;
25112}
25113
25114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 * ":return [expr]"
25116 */
25117 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025118ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119{
25120 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025121 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 int returning = FALSE;
25123
25124 if (current_funccal == NULL)
25125 {
25126 EMSG(_("E133: :return not inside a function"));
25127 return;
25128 }
25129
25130 if (eap->skip)
25131 ++emsg_skip;
25132
25133 eap->nextcmd = NULL;
25134 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025135 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 {
25137 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025138 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025140 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 }
25142 /* It's safer to return also on error. */
25143 else if (!eap->skip)
25144 {
25145 /*
25146 * Return unless the expression evaluation has been cancelled due to an
25147 * aborting error, an interrupt, or an exception.
25148 */
25149 if (!aborting())
25150 returning = do_return(eap, FALSE, TRUE, NULL);
25151 }
25152
25153 /* When skipping or the return gets pending, advance to the next command
25154 * in this line (!returning). Otherwise, ignore the rest of the line.
25155 * Following lines will be ignored by get_func_line(). */
25156 if (returning)
25157 eap->nextcmd = NULL;
25158 else if (eap->nextcmd == NULL) /* no argument */
25159 eap->nextcmd = check_nextcmd(arg);
25160
25161 if (eap->skip)
25162 --emsg_skip;
25163}
25164
25165/*
25166 * Return from a function. Possibly makes the return pending. Also called
25167 * for a pending return at the ":endtry" or after returning from an extra
25168 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025169 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025170 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 * FALSE when the return gets pending.
25172 */
25173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025174do_return(
25175 exarg_T *eap,
25176 int reanimate,
25177 int is_cmd,
25178 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179{
25180 int idx;
25181 struct condstack *cstack = eap->cstack;
25182
25183 if (reanimate)
25184 /* Undo the return. */
25185 current_funccal->returned = FALSE;
25186
25187 /*
25188 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25189 * not in its finally clause (which then is to be executed next) is found.
25190 * In this case, make the ":return" pending for execution at the ":endtry".
25191 * Otherwise, return normally.
25192 */
25193 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25194 if (idx >= 0)
25195 {
25196 cstack->cs_pending[idx] = CSTP_RETURN;
25197
25198 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025199 /* A pending return again gets pending. "rettv" points to an
25200 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025202 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 else
25204 {
25205 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025206 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025208 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025210 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 {
25212 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025213 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025214 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 else
25216 EMSG(_(e_outofmem));
25217 }
25218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025219 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220
25221 if (reanimate)
25222 {
25223 /* The pending return value could be overwritten by a ":return"
25224 * without argument in a finally clause; reset the default
25225 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025226 current_funccal->rettv->v_type = VAR_NUMBER;
25227 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 }
25229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025230 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 }
25232 else
25233 {
25234 current_funccal->returned = TRUE;
25235
25236 /* If the return is carried out now, store the return value. For
25237 * a return immediately after reanimation, the value is already
25238 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025239 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025241 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025242 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025244 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 }
25246 }
25247
25248 return idx < 0;
25249}
25250
25251/*
25252 * Free the variable with a pending return value.
25253 */
25254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025255discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256{
Bram Moolenaar33570922005-01-25 22:26:29 +000025257 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258}
25259
25260/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025261 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 * is an allocated string. Used by report_pending() for verbose messages.
25263 */
25264 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025267 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025268 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025269 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025271 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025272 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025273 if (s == NULL)
25274 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025275
25276 STRCPY(IObuff, ":return ");
25277 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25278 if (STRLEN(s) + 8 >= IOSIZE)
25279 STRCPY(IObuff + IOSIZE - 4, "...");
25280 vim_free(tofree);
25281 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282}
25283
25284/*
25285 * Get next function line.
25286 * Called by do_cmdline() to get the next line.
25287 * Returns allocated string, or NULL for end of function.
25288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025290get_func_line(
25291 int c UNUSED,
25292 void *cookie,
25293 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294{
Bram Moolenaar33570922005-01-25 22:26:29 +000025295 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025296 ufunc_T *fp = fcp->func;
25297 char_u *retval;
25298 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299
25300 /* If breakpoints have been added/deleted need to check for it. */
25301 if (fcp->dbg_tick != debug_tick)
25302 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025303 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025304 sourcing_lnum);
25305 fcp->dbg_tick = debug_tick;
25306 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025307#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025308 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025309 func_line_end(cookie);
25310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311
Bram Moolenaar05159a02005-02-26 23:04:13 +000025312 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025313 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25314 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315 retval = NULL;
25316 else
25317 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025318 /* Skip NULL lines (continuation lines). */
25319 while (fcp->linenr < gap->ga_len
25320 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25321 ++fcp->linenr;
25322 if (fcp->linenr >= gap->ga_len)
25323 retval = NULL;
25324 else
25325 {
25326 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25327 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025328#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025329 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025330 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025331#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 }
25334
25335 /* Did we encounter a breakpoint? */
25336 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25337 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025338 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025340 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341 sourcing_lnum);
25342 fcp->dbg_tick = debug_tick;
25343 }
25344
25345 return retval;
25346}
25347
Bram Moolenaar05159a02005-02-26 23:04:13 +000025348#if defined(FEAT_PROFILE) || defined(PROTO)
25349/*
25350 * Called when starting to read a function line.
25351 * "sourcing_lnum" must be correct!
25352 * When skipping lines it may not actually be executed, but we won't find out
25353 * until later and we need to store the time now.
25354 */
25355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025356func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025357{
25358 funccall_T *fcp = (funccall_T *)cookie;
25359 ufunc_T *fp = fcp->func;
25360
25361 if (fp->uf_profiling && sourcing_lnum >= 1
25362 && sourcing_lnum <= fp->uf_lines.ga_len)
25363 {
25364 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025365 /* Skip continuation lines. */
25366 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25367 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368 fp->uf_tml_execed = FALSE;
25369 profile_start(&fp->uf_tml_start);
25370 profile_zero(&fp->uf_tml_children);
25371 profile_get_wait(&fp->uf_tml_wait);
25372 }
25373}
25374
25375/*
25376 * Called when actually executing a function line.
25377 */
25378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025379func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025380{
25381 funccall_T *fcp = (funccall_T *)cookie;
25382 ufunc_T *fp = fcp->func;
25383
25384 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25385 fp->uf_tml_execed = TRUE;
25386}
25387
25388/*
25389 * Called when done with a function line.
25390 */
25391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025392func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025393{
25394 funccall_T *fcp = (funccall_T *)cookie;
25395 ufunc_T *fp = fcp->func;
25396
25397 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25398 {
25399 if (fp->uf_tml_execed)
25400 {
25401 ++fp->uf_tml_count[fp->uf_tml_idx];
25402 profile_end(&fp->uf_tml_start);
25403 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025404 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025405 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25406 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025407 }
25408 fp->uf_tml_idx = -1;
25409 }
25410}
25411#endif
25412
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413/*
25414 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025415 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 */
25417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025418func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419{
Bram Moolenaar33570922005-01-25 22:26:29 +000025420 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421
25422 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25423 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025424 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 || fcp->returned);
25426}
25427
25428/*
25429 * return TRUE if cookie indicates a function which "abort"s on errors.
25430 */
25431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025432func_has_abort(
25433 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025435 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436}
25437
25438#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25439typedef enum
25440{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025441 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25442 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25443 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444} var_flavour_T;
25445
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025446static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
25448 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025449var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450{
25451 char_u *p = varname;
25452
25453 if (ASCII_ISUPPER(*p))
25454 {
25455 while (*(++p))
25456 if (ASCII_ISLOWER(*p))
25457 return VAR_FLAVOUR_SESSION;
25458 return VAR_FLAVOUR_VIMINFO;
25459 }
25460 else
25461 return VAR_FLAVOUR_DEFAULT;
25462}
25463#endif
25464
25465#if defined(FEAT_VIMINFO) || defined(PROTO)
25466/*
25467 * Restore global vars that start with a capital from the viminfo file
25468 */
25469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025470read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471{
25472 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025473 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025474 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025475 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476
25477 if (!writing && (find_viminfo_parameter('!') != NULL))
25478 {
25479 tab = vim_strchr(virp->vir_line + 1, '\t');
25480 if (tab != NULL)
25481 {
25482 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025483 switch (*tab)
25484 {
25485 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025486#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025487 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025488#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025489 case 'D': type = VAR_DICT; break;
25490 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025491 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493
25494 tab = vim_strchr(tab, '\t');
25495 if (tab != NULL)
25496 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025497 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025498 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025499 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025501#ifdef FEAT_FLOAT
25502 else if (type == VAR_FLOAT)
25503 (void)string2float(tab + 1, &tv.vval.v_float);
25504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025506 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025507 if (type == VAR_DICT || type == VAR_LIST)
25508 {
25509 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25510
25511 if (etv == NULL)
25512 /* Failed to parse back the dict or list, use it as a
25513 * string. */
25514 tv.v_type = VAR_STRING;
25515 else
25516 {
25517 vim_free(tv.vval.v_string);
25518 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025519 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025520 }
25521 }
25522
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025523 /* when in a function use global variables */
25524 save_funccal = current_funccal;
25525 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025526 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025527 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025528
25529 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025530 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025531 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25532 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 }
25534 }
25535 }
25536
25537 return viminfo_readline(virp);
25538}
25539
25540/*
25541 * Write global vars that start with a capital to the viminfo file
25542 */
25543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025544write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545{
Bram Moolenaar33570922005-01-25 22:26:29 +000025546 hashitem_T *hi;
25547 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025548 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025549 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025550 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025551 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025552 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553
25554 if (find_viminfo_parameter('!') == NULL)
25555 return;
25556
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025557 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025558
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025559 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025560 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025562 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025564 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 this_var = HI2DI(hi);
25566 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025567 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025568 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025569 {
25570 case VAR_STRING: s = "STR"; break;
25571 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025572 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025573 case VAR_DICT: s = "DIC"; break;
25574 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025575 case VAR_SPECIAL: s = "XPL"; break;
25576
25577 case VAR_UNKNOWN:
25578 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025579 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025580 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025581 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025582 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025583 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025584 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025585 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025586 if (p != NULL)
25587 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025588 vim_free(tofree);
25589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 }
25591 }
25592}
25593#endif
25594
25595#if defined(FEAT_SESSION) || defined(PROTO)
25596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598{
Bram Moolenaar33570922005-01-25 22:26:29 +000025599 hashitem_T *hi;
25600 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025601 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 char_u *p, *t;
25603
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025604 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025605 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025607 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025609 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025610 this_var = HI2DI(hi);
25611 if ((this_var->di_tv.v_type == VAR_NUMBER
25612 || this_var->di_tv.v_type == VAR_STRING)
25613 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025614 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025615 /* Escape special characters with a backslash. Turn a LF and
25616 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025617 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025618 (char_u *)"\\\"\n\r");
25619 if (p == NULL) /* out of memory */
25620 break;
25621 for (t = p; *t != NUL; ++t)
25622 if (*t == '\n')
25623 *t = 'n';
25624 else if (*t == '\r')
25625 *t = 'r';
25626 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025627 this_var->di_key,
25628 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25629 : ' ',
25630 p,
25631 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25632 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025633 || put_eol(fd) == FAIL)
25634 {
25635 vim_free(p);
25636 return FAIL;
25637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 vim_free(p);
25639 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025640#ifdef FEAT_FLOAT
25641 else if (this_var->di_tv.v_type == VAR_FLOAT
25642 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25643 {
25644 float_T f = this_var->di_tv.vval.v_float;
25645 int sign = ' ';
25646
25647 if (f < 0)
25648 {
25649 f = -f;
25650 sign = '-';
25651 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025652 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025653 this_var->di_key, sign, f) < 0)
25654 || put_eol(fd) == FAIL)
25655 return FAIL;
25656 }
25657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 }
25659 }
25660 return OK;
25661}
25662#endif
25663
Bram Moolenaar661b1822005-07-28 22:36:45 +000025664/*
25665 * Display script name where an item was last set.
25666 * Should only be invoked when 'verbose' is non-zero.
25667 */
25668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025669last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025670{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025671 char_u *p;
25672
Bram Moolenaar661b1822005-07-28 22:36:45 +000025673 if (scriptID != 0)
25674 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025675 p = home_replace_save(NULL, get_scriptname(scriptID));
25676 if (p != NULL)
25677 {
25678 verbose_enter();
25679 MSG_PUTS(_("\n\tLast set from "));
25680 MSG_PUTS(p);
25681 vim_free(p);
25682 verbose_leave();
25683 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025684 }
25685}
25686
Bram Moolenaard812df62008-11-09 12:46:09 +000025687/*
25688 * List v:oldfiles in a nice way.
25689 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025690 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025691ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025692{
25693 list_T *l = vimvars[VV_OLDFILES].vv_list;
25694 listitem_T *li;
25695 int nr = 0;
25696
25697 if (l == NULL)
25698 msg((char_u *)_("No old files"));
25699 else
25700 {
25701 msg_start();
25702 msg_scroll = TRUE;
25703 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25704 {
25705 msg_outnum((long)++nr);
25706 MSG_PUTS(": ");
25707 msg_outtrans(get_tv_string(&li->li_tv));
25708 msg_putchar('\n');
25709 out_flush(); /* output one line at a time */
25710 ui_breakcheck();
25711 }
25712 /* Assume "got_int" was set to truncate the listing. */
25713 got_int = FALSE;
25714
25715#ifdef FEAT_BROWSE_CMD
25716 if (cmdmod.browse)
25717 {
25718 quit_more = FALSE;
25719 nr = prompt_for_number(FALSE);
25720 msg_starthere();
25721 if (nr > 0)
25722 {
25723 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25724 (long)nr);
25725
25726 if (p != NULL)
25727 {
25728 p = expand_env_save(p);
25729 eap->arg = p;
25730 eap->cmdidx = CMD_edit;
25731 cmdmod.browse = FALSE;
25732 do_exedit(eap, NULL);
25733 vim_free(p);
25734 }
25735 }
25736 }
25737#endif
25738 }
25739}
25740
Bram Moolenaar53744302015-07-17 17:38:22 +020025741/* reset v:option_new, v:option_old and v:option_type */
25742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025743reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025744{
25745 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25746 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25747 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25748}
25749
25750
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751#endif /* FEAT_EVAL */
25752
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025754#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755
25756#ifdef WIN3264
25757/*
25758 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25759 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025760static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25761static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25762static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763
25764/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025765 * Get the short path (8.3) for the filename in "fnamep".
25766 * Only works for a valid file name.
25767 * When the path gets longer "fnamep" is changed and the allocated buffer
25768 * is put in "bufp".
25769 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25770 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 */
25772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025775 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 char_u *newbuf;
25777
25778 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025779 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 if (l > len - 1)
25781 {
25782 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025783 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 newbuf = vim_strnsave(*fnamep, l);
25785 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025786 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787
25788 vim_free(*bufp);
25789 *fnamep = *bufp = newbuf;
25790
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025791 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025792 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 }
25794
25795 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025796 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797}
25798
25799/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025800 * Get the short path (8.3) for the filename in "fname". The converted
25801 * path is returned in "bufp".
25802 *
25803 * Some of the directories specified in "fname" may not exist. This function
25804 * will shorten the existing directories at the beginning of the path and then
25805 * append the remaining non-existing path.
25806 *
25807 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025808 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025809 * bufp - Pointer to an allocated buffer for the filename.
25810 * fnamelen - Length of the filename pointed to by fname
25811 *
25812 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813 */
25814 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025815shortpath_for_invalid_fname(
25816 char_u **fname,
25817 char_u **bufp,
25818 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025820 char_u *short_fname, *save_fname, *pbuf_unused;
25821 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025823 int old_len, len;
25824 int new_len, sfx_len;
25825 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826
25827 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025828 old_len = *fnamelen;
25829 save_fname = vim_strnsave(*fname, old_len);
25830 pbuf_unused = NULL;
25831 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025833 endp = save_fname + old_len - 1; /* Find the end of the copy */
25834 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025836 /*
25837 * Try shortening the supplied path till it succeeds by removing one
25838 * directory at a time from the tail of the path.
25839 */
25840 len = 0;
25841 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025842 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025843 /* go back one path-separator */
25844 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25845 --endp;
25846 if (endp <= save_fname)
25847 break; /* processed the complete path */
25848
25849 /*
25850 * Replace the path separator with a NUL and try to shorten the
25851 * resulting path.
25852 */
25853 ch = *endp;
25854 *endp = 0;
25855 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025856 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025857 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25858 {
25859 retval = FAIL;
25860 goto theend;
25861 }
25862 *endp = ch; /* preserve the string */
25863
25864 if (len > 0)
25865 break; /* successfully shortened the path */
25866
25867 /* failed to shorten the path. Skip the path separator */
25868 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025869 }
25870
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025871 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025873 /*
25874 * Succeeded in shortening the path. Now concatenate the shortened
25875 * path with the remaining path at the tail.
25876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025878 /* Compute the length of the new path. */
25879 sfx_len = (int)(save_endp - endp) + 1;
25880 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025882 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025884 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025886 /* There is not enough space in the currently allocated string,
25887 * copy it to a buffer big enough. */
25888 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025890 {
25891 retval = FAIL;
25892 goto theend;
25893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025894 }
25895 else
25896 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025897 /* Transfer short_fname to the main buffer (it's big enough),
25898 * unless get_short_pathname() did its work in-place. */
25899 *fname = *bufp = save_fname;
25900 if (short_fname != save_fname)
25901 vim_strncpy(save_fname, short_fname, len);
25902 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025904
25905 /* concat the not-shortened part of the path */
25906 vim_strncpy(*fname + len, endp, sfx_len);
25907 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025909
25910theend:
25911 vim_free(pbuf_unused);
25912 vim_free(save_fname);
25913
25914 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915}
25916
25917/*
25918 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025919 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 */
25921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025922shortpath_for_partial(
25923 char_u **fnamep,
25924 char_u **bufp,
25925 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926{
25927 int sepcount, len, tflen;
25928 char_u *p;
25929 char_u *pbuf, *tfname;
25930 int hasTilde;
25931
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025932 /* Count up the path separators from the RHS.. so we know which part
25933 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025935 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 if (vim_ispathsep(*p))
25937 ++sepcount;
25938
25939 /* Need full path first (use expand_env() to remove a "~/") */
25940 hasTilde = (**fnamep == '~');
25941 if (hasTilde)
25942 pbuf = tfname = expand_env_save(*fnamep);
25943 else
25944 pbuf = tfname = FullName_save(*fnamep, FALSE);
25945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025946 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025948 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25949 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950
25951 if (len == 0)
25952 {
25953 /* Don't have a valid filename, so shorten the rest of the
25954 * path if we can. This CAN give us invalid 8.3 filenames, but
25955 * there's not a lot of point in guessing what it might be.
25956 */
25957 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025958 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25959 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 }
25961
25962 /* Count the paths backward to find the beginning of the desired string. */
25963 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025964 {
25965#ifdef FEAT_MBYTE
25966 if (has_mbyte)
25967 p -= mb_head_off(tfname, p);
25968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 if (vim_ispathsep(*p))
25970 {
25971 if (sepcount == 0 || (hasTilde && sepcount == 1))
25972 break;
25973 else
25974 sepcount --;
25975 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977 if (hasTilde)
25978 {
25979 --p;
25980 if (p >= tfname)
25981 *p = '~';
25982 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025983 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 }
25985 else
25986 ++p;
25987
25988 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25989 vim_free(*bufp);
25990 *fnamelen = (int)STRLEN(p);
25991 *bufp = pbuf;
25992 *fnamep = p;
25993
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025994 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995}
25996#endif /* WIN3264 */
25997
25998/*
25999 * Adjust a filename, according to a string of modifiers.
26000 * *fnamep must be NUL terminated when called. When returning, the length is
26001 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026002 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003 * When there is an error, *fnamep is set to NULL.
26004 */
26005 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026006modify_fname(
26007 char_u *src, /* string with modifiers */
26008 int *usedlen, /* characters after src that are used */
26009 char_u **fnamep, /* file name so far */
26010 char_u **bufp, /* buffer for allocated file name or NULL */
26011 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012{
26013 int valid = 0;
26014 char_u *tail;
26015 char_u *s, *p, *pbuf;
26016 char_u dirname[MAXPATHL];
26017 int c;
26018 int has_fullname = 0;
26019#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026020 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 int has_shortname = 0;
26022#endif
26023
26024repeat:
26025 /* ":p" - full path/file_name */
26026 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26027 {
26028 has_fullname = 1;
26029
26030 valid |= VALID_PATH;
26031 *usedlen += 2;
26032
26033 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26034 if ((*fnamep)[0] == '~'
26035#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26036 && ((*fnamep)[1] == '/'
26037# ifdef BACKSLASH_IN_FILENAME
26038 || (*fnamep)[1] == '\\'
26039# endif
26040 || (*fnamep)[1] == NUL)
26041
26042#endif
26043 )
26044 {
26045 *fnamep = expand_env_save(*fnamep);
26046 vim_free(*bufp); /* free any allocated file name */
26047 *bufp = *fnamep;
26048 if (*fnamep == NULL)
26049 return -1;
26050 }
26051
26052 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026053 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054 {
26055 if (vim_ispathsep(*p)
26056 && p[1] == '.'
26057 && (p[2] == NUL
26058 || vim_ispathsep(p[2])
26059 || (p[2] == '.'
26060 && (p[3] == NUL || vim_ispathsep(p[3])))))
26061 break;
26062 }
26063
26064 /* FullName_save() is slow, don't use it when not needed. */
26065 if (*p != NUL || !vim_isAbsName(*fnamep))
26066 {
26067 *fnamep = FullName_save(*fnamep, *p != NUL);
26068 vim_free(*bufp); /* free any allocated file name */
26069 *bufp = *fnamep;
26070 if (*fnamep == NULL)
26071 return -1;
26072 }
26073
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026074#ifdef WIN3264
26075# if _WIN32_WINNT >= 0x0500
26076 if (vim_strchr(*fnamep, '~') != NULL)
26077 {
26078 /* Expand 8.3 filename to full path. Needed to make sure the same
26079 * file does not have two different names.
26080 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26081 p = alloc(_MAX_PATH + 1);
26082 if (p != NULL)
26083 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026084 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026085 {
26086 vim_free(*bufp);
26087 *bufp = *fnamep = p;
26088 }
26089 else
26090 vim_free(p);
26091 }
26092 }
26093# endif
26094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095 /* Append a path separator to a directory. */
26096 if (mch_isdir(*fnamep))
26097 {
26098 /* Make room for one or two extra characters. */
26099 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26100 vim_free(*bufp); /* free any allocated file name */
26101 *bufp = *fnamep;
26102 if (*fnamep == NULL)
26103 return -1;
26104 add_pathsep(*fnamep);
26105 }
26106 }
26107
26108 /* ":." - path relative to the current directory */
26109 /* ":~" - path relative to the home directory */
26110 /* ":8" - shortname path - postponed till after */
26111 while (src[*usedlen] == ':'
26112 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26113 {
26114 *usedlen += 2;
26115 if (c == '8')
26116 {
26117#ifdef WIN3264
26118 has_shortname = 1; /* Postpone this. */
26119#endif
26120 continue;
26121 }
26122 pbuf = NULL;
26123 /* Need full path first (use expand_env() to remove a "~/") */
26124 if (!has_fullname)
26125 {
26126 if (c == '.' && **fnamep == '~')
26127 p = pbuf = expand_env_save(*fnamep);
26128 else
26129 p = pbuf = FullName_save(*fnamep, FALSE);
26130 }
26131 else
26132 p = *fnamep;
26133
26134 has_fullname = 0;
26135
26136 if (p != NULL)
26137 {
26138 if (c == '.')
26139 {
26140 mch_dirname(dirname, MAXPATHL);
26141 s = shorten_fname(p, dirname);
26142 if (s != NULL)
26143 {
26144 *fnamep = s;
26145 if (pbuf != NULL)
26146 {
26147 vim_free(*bufp); /* free any allocated file name */
26148 *bufp = pbuf;
26149 pbuf = NULL;
26150 }
26151 }
26152 }
26153 else
26154 {
26155 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26156 /* Only replace it when it starts with '~' */
26157 if (*dirname == '~')
26158 {
26159 s = vim_strsave(dirname);
26160 if (s != NULL)
26161 {
26162 *fnamep = s;
26163 vim_free(*bufp);
26164 *bufp = s;
26165 }
26166 }
26167 }
26168 vim_free(pbuf);
26169 }
26170 }
26171
26172 tail = gettail(*fnamep);
26173 *fnamelen = (int)STRLEN(*fnamep);
26174
26175 /* ":h" - head, remove "/file_name", can be repeated */
26176 /* Don't remove the first "/" or "c:\" */
26177 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26178 {
26179 valid |= VALID_HEAD;
26180 *usedlen += 2;
26181 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026182 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026183 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184 *fnamelen = (int)(tail - *fnamep);
26185#ifdef VMS
26186 if (*fnamelen > 0)
26187 *fnamelen += 1; /* the path separator is part of the path */
26188#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026189 if (*fnamelen == 0)
26190 {
26191 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26192 p = vim_strsave((char_u *)".");
26193 if (p == NULL)
26194 return -1;
26195 vim_free(*bufp);
26196 *bufp = *fnamep = tail = p;
26197 *fnamelen = 1;
26198 }
26199 else
26200 {
26201 while (tail > s && !after_pathsep(s, tail))
26202 mb_ptr_back(*fnamep, tail);
26203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 }
26205
26206 /* ":8" - shortname */
26207 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26208 {
26209 *usedlen += 2;
26210#ifdef WIN3264
26211 has_shortname = 1;
26212#endif
26213 }
26214
26215#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026216 /*
26217 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218 */
26219 if (has_shortname)
26220 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026221 /* Copy the string if it is shortened by :h and when it wasn't copied
26222 * yet, because we are going to change it in place. Avoids changing
26223 * the buffer name for "%:8". */
26224 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 {
26226 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026227 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228 return -1;
26229 vim_free(*bufp);
26230 *bufp = *fnamep = p;
26231 }
26232
26233 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026234 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235 if (!has_fullname && !vim_isAbsName(*fnamep))
26236 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026237 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 return -1;
26239 }
26240 else
26241 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026242 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243
Bram Moolenaardc935552011-08-17 15:23:23 +020026244 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026246 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247 return -1;
26248
26249 if (l == 0)
26250 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026251 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026253 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026254 return -1;
26255 }
26256 *fnamelen = l;
26257 }
26258 }
26259#endif /* WIN3264 */
26260
26261 /* ":t" - tail, just the basename */
26262 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26263 {
26264 *usedlen += 2;
26265 *fnamelen -= (int)(tail - *fnamep);
26266 *fnamep = tail;
26267 }
26268
26269 /* ":e" - extension, can be repeated */
26270 /* ":r" - root, without extension, can be repeated */
26271 while (src[*usedlen] == ':'
26272 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26273 {
26274 /* find a '.' in the tail:
26275 * - for second :e: before the current fname
26276 * - otherwise: The last '.'
26277 */
26278 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26279 s = *fnamep - 2;
26280 else
26281 s = *fnamep + *fnamelen - 1;
26282 for ( ; s > tail; --s)
26283 if (s[0] == '.')
26284 break;
26285 if (src[*usedlen + 1] == 'e') /* :e */
26286 {
26287 if (s > tail)
26288 {
26289 *fnamelen += (int)(*fnamep - (s + 1));
26290 *fnamep = s + 1;
26291#ifdef VMS
26292 /* cut version from the extension */
26293 s = *fnamep + *fnamelen - 1;
26294 for ( ; s > *fnamep; --s)
26295 if (s[0] == ';')
26296 break;
26297 if (s > *fnamep)
26298 *fnamelen = s - *fnamep;
26299#endif
26300 }
26301 else if (*fnamep <= tail)
26302 *fnamelen = 0;
26303 }
26304 else /* :r */
26305 {
26306 if (s > tail) /* remove one extension */
26307 *fnamelen = (int)(s - *fnamep);
26308 }
26309 *usedlen += 2;
26310 }
26311
26312 /* ":s?pat?foo?" - substitute */
26313 /* ":gs?pat?foo?" - global substitute */
26314 if (src[*usedlen] == ':'
26315 && (src[*usedlen + 1] == 's'
26316 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26317 {
26318 char_u *str;
26319 char_u *pat;
26320 char_u *sub;
26321 int sep;
26322 char_u *flags;
26323 int didit = FALSE;
26324
26325 flags = (char_u *)"";
26326 s = src + *usedlen + 2;
26327 if (src[*usedlen + 1] == 'g')
26328 {
26329 flags = (char_u *)"g";
26330 ++s;
26331 }
26332
26333 sep = *s++;
26334 if (sep)
26335 {
26336 /* find end of pattern */
26337 p = vim_strchr(s, sep);
26338 if (p != NULL)
26339 {
26340 pat = vim_strnsave(s, (int)(p - s));
26341 if (pat != NULL)
26342 {
26343 s = p + 1;
26344 /* find end of substitution */
26345 p = vim_strchr(s, sep);
26346 if (p != NULL)
26347 {
26348 sub = vim_strnsave(s, (int)(p - s));
26349 str = vim_strnsave(*fnamep, *fnamelen);
26350 if (sub != NULL && str != NULL)
26351 {
26352 *usedlen = (int)(p + 1 - src);
26353 s = do_string_sub(str, pat, sub, flags);
26354 if (s != NULL)
26355 {
26356 *fnamep = s;
26357 *fnamelen = (int)STRLEN(s);
26358 vim_free(*bufp);
26359 *bufp = s;
26360 didit = TRUE;
26361 }
26362 }
26363 vim_free(sub);
26364 vim_free(str);
26365 }
26366 vim_free(pat);
26367 }
26368 }
26369 /* after using ":s", repeat all the modifiers */
26370 if (didit)
26371 goto repeat;
26372 }
26373 }
26374
Bram Moolenaar26df0922014-02-23 23:39:13 +010026375 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26376 {
26377 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26378 if (p == NULL)
26379 return -1;
26380 vim_free(*bufp);
26381 *bufp = *fnamep = p;
26382 *fnamelen = (int)STRLEN(p);
26383 *usedlen += 2;
26384 }
26385
Bram Moolenaar071d4272004-06-13 20:20:40 +000026386 return valid;
26387}
26388
26389/*
26390 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26391 * "flags" can be "g" to do a global substitute.
26392 * Returns an allocated string, NULL for error.
26393 */
26394 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026395do_string_sub(
26396 char_u *str,
26397 char_u *pat,
26398 char_u *sub,
26399 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400{
26401 int sublen;
26402 regmatch_T regmatch;
26403 int i;
26404 int do_all;
26405 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026406 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 garray_T ga;
26408 char_u *ret;
26409 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026410 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411
26412 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26413 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026414 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415
26416 ga_init2(&ga, 1, 200);
26417
26418 do_all = (flags[0] == 'g');
26419
26420 regmatch.rm_ic = p_ic;
26421 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26422 if (regmatch.regprog != NULL)
26423 {
26424 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026425 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26427 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026428 /* Skip empty match except for first match. */
26429 if (regmatch.startp[0] == regmatch.endp[0])
26430 {
26431 if (zero_width == regmatch.startp[0])
26432 {
26433 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026434 i = MB_PTR2LEN(tail);
26435 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26436 (size_t)i);
26437 ga.ga_len += i;
26438 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026439 continue;
26440 }
26441 zero_width = regmatch.startp[0];
26442 }
26443
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444 /*
26445 * Get some space for a temporary buffer to do the substitution
26446 * into. It will contain:
26447 * - The text up to where the match is.
26448 * - The substituted text.
26449 * - The text after the match.
26450 */
26451 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026452 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026453 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26454 {
26455 ga_clear(&ga);
26456 break;
26457 }
26458
26459 /* copy the text up to where the match is */
26460 i = (int)(regmatch.startp[0] - tail);
26461 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26462 /* add the substituted text */
26463 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26464 + ga.ga_len + i, TRUE, TRUE, FALSE);
26465 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026466 tail = regmatch.endp[0];
26467 if (*tail == NUL)
26468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 if (!do_all)
26470 break;
26471 }
26472
26473 if (ga.ga_data != NULL)
26474 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26475
Bram Moolenaar473de612013-06-08 18:19:48 +020026476 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477 }
26478
26479 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26480 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026481 if (p_cpo == empty_option)
26482 p_cpo = save_cpo;
26483 else
26484 /* Darn, evaluating {sub} expression changed the value. */
26485 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026486
26487 return ret;
26488}
26489
26490#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */