blob: 825a606a7019634d949b6c58e0207a7e05c0219d [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);
437static int list_append_number(list_T *l, varnumber_T n);
438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
456static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
457static 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, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100638# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100738static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_setline(typval_T *argvars, typval_T *rettv);
740static void f_setloclist(typval_T *argvars, typval_T *rettv);
741static void f_setmatches(typval_T *argvars, typval_T *rettv);
742static void f_setpos(typval_T *argvars, typval_T *rettv);
743static void f_setqflist(typval_T *argvars, typval_T *rettv);
744static void f_setreg(typval_T *argvars, typval_T *rettv);
745static void f_settabvar(typval_T *argvars, typval_T *rettv);
746static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
747static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100750#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_shellescape(typval_T *argvars, typval_T *rettv);
752static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
753static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sin(typval_T *argvars, typval_T *rettv);
756static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sort(typval_T *argvars, typval_T *rettv);
759static void f_soundfold(typval_T *argvars, typval_T *rettv);
760static void f_spellbadword(typval_T *argvars, typval_T *rettv);
761static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
762static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sqrt(typval_T *argvars, typval_T *rettv);
765static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_str2nr(typval_T *argvars, typval_T *rettv);
768static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_stridx(typval_T *argvars, typval_T *rettv);
773static void f_string(typval_T *argvars, typval_T *rettv);
774static void f_strlen(typval_T *argvars, typval_T *rettv);
775static void f_strpart(typval_T *argvars, typval_T *rettv);
776static void f_strridx(typval_T *argvars, typval_T *rettv);
777static void f_strtrans(typval_T *argvars, typval_T *rettv);
778static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
779static void f_strwidth(typval_T *argvars, typval_T *rettv);
780static void f_submatch(typval_T *argvars, typval_T *rettv);
781static void f_substitute(typval_T *argvars, typval_T *rettv);
782static void f_synID(typval_T *argvars, typval_T *rettv);
783static void f_synIDattr(typval_T *argvars, typval_T *rettv);
784static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
785static void f_synstack(typval_T *argvars, typval_T *rettv);
786static void f_synconcealed(typval_T *argvars, typval_T *rettv);
787static void f_system(typval_T *argvars, typval_T *rettv);
788static void f_systemlist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
790static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
791static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
792static void f_taglist(typval_T *argvars, typval_T *rettv);
793static void f_tagfiles(typval_T *argvars, typval_T *rettv);
794static void f_tempname(typval_T *argvars, typval_T *rettv);
795static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tan(typval_T *argvars, typval_T *rettv);
798static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#endif
Bram 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);
814static void f_winbufnr(typval_T *argvars, typval_T *rettv);
815static void f_wincol(typval_T *argvars, typval_T *rettv);
816static void f_winheight(typval_T *argvars, typval_T *rettv);
817static void f_winline(typval_T *argvars, typval_T *rettv);
818static void f_winnr(typval_T *argvars, typval_T *rettv);
819static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
820static void f_winrestview(typval_T *argvars, typval_T *rettv);
821static void f_winsaveview(typval_T *argvars, typval_T *rettv);
822static void f_winwidth(typval_T *argvars, typval_T *rettv);
823static void f_writefile(typval_T *argvars, typval_T *rettv);
824static void f_wordcount(typval_T *argvars, typval_T *rettv);
825static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
828static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
829static int get_env_len(char_u **arg);
830static int get_id_len(char_u **arg);
831static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
832static 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 +0000833#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
834#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
835 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
837static int eval_isnamec(int c);
838static int eval_isnamec1(int c);
839static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
840static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static typval_T *alloc_string_tv(char_u *string);
842static void init_tv(typval_T *varp);
843static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100844#ifdef FEAT_FLOAT
845static float_T get_tv_float(typval_T *varp);
846#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static linenr_T get_tv_lnum(typval_T *argvars);
848static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
849static char_u *get_tv_string(typval_T *varp);
850static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
851static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
852static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
853static hashtab_T *find_var_ht(char_u *name, char_u **varname);
854static funccall_T *get_funccal(void);
855static void vars_clear_ext(hashtab_T *ht, int free_val);
856static void delete_var(hashtab_T *ht, hashitem_T *hi);
857static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
858static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
859static void set_var(char_u *name, typval_T *varp, int copy);
860static int var_check_ro(int flags, char_u *name, int use_gettext);
861static int var_check_fixed(int flags, char_u *name, int use_gettext);
862static int var_check_func_name(char_u *name, int new_var);
863static int valid_varname(char_u *varname);
864static int tv_check_lock(int lock, char_u *name, int use_gettext);
865static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
866static char_u *find_option_end(char_u **arg, int *opt_flags);
867static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
868static int eval_fname_script(char_u *p);
869static int eval_fname_sid(char_u *p);
870static void list_func_head(ufunc_T *fp, int indent);
871static ufunc_T *find_func(char_u *name);
872static int function_exists(char_u *name);
873static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875static void func_do_profile(ufunc_T *fp);
876static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
877static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000878static int
879# ifdef __BORLANDC__
880 _RTLENTRYF
881# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889static int script_autoload(char_u *name, int reload);
890static char_u *autoload_name(char_u *name);
891static void cat_func_name(char_u *buf, ufunc_T *fp);
892static void func_free(ufunc_T *fp);
893static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
894static int can_free_funccal(funccall_T *fc, int copyID) ;
895static void free_funccal(funccall_T *fc, int free_val);
896static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
897static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
898static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
899static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
900static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
901static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
902static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int write_list(FILE *fd, list_T *list, int binary);
904static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000905
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906
907#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100908static int compare_func_name(const void *s1, const void *s2);
909static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910#endif
911
Bram Moolenaar33570922005-01-25 22:26:29 +0000912/*
913 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000914 */
915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100916eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000917{
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 int i;
919 struct vimvar *p;
920
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200921 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
922 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200923 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000924 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000925 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926
927 for (i = 0; i < VV_LEN; ++i)
928 {
929 p = &vimvars[i];
930 STRCPY(p->vv_di.di_key, p->vv_name);
931 if (p->vv_flags & VV_RO)
932 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
933 else if (p->vv_flags & VV_RO_SBX)
934 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
935 else
936 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000937
938 /* add to v: scope dict, unless the value is not always available */
939 if (p->vv_type != VAR_UNKNOWN)
940 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000942 /* add to compat scope dict */
943 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100945 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
946
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000947 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100948 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200949 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100950 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100951
952 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
953 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
954 set_vim_var_nr(VV_NONE, VVAL_NONE);
955 set_vim_var_nr(VV_NULL, VVAL_NULL);
956
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200957 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958
959#ifdef EBCDIC
960 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100961 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200962 */
963 sortFunctions();
964#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000965}
966
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967#if defined(EXITFREE) || defined(PROTO)
968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100969eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970{
971 int i;
972 struct vimvar *p;
973
974 for (i = 0; i < VV_LEN; ++i)
975 {
976 p = &vimvars[i];
977 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000979 vim_free(p->vv_str);
980 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000981 }
982 else if (p->vv_di.di_tv.v_type == VAR_LIST)
983 {
984 list_unref(p->vv_list);
985 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000987 }
988 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000989 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990 hash_clear(&compat_hashtab);
991
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100993# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200994 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000996
997 /* global variables */
998 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000999
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001000 /* autoloaded script names */
1001 ga_clear_strings(&ga_loaded);
1002
Bram Moolenaarcca74132013-09-25 21:00:28 +02001003 /* Script-local variables. First clear all the variables and in a second
1004 * loop free the scriptvar_T, because a variable in one script might hold
1005 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 ga_clear(&ga_scripts);
1011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001012 /* unreferenced lists and dicts */
1013 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001014
1015 /* functions */
1016 free_all_functions();
1017 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001018}
1019#endif
1020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * Return the name of the executed function.
1023 */
1024 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001025func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
1031 * Return the address holding the next breakpoint line for a funccall cookie.
1032 */
1033 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001034func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037}
1038
1039/*
1040 * Return the address holding the debug tick for a funccall cookie.
1041 */
1042 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001043func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar33570922005-01-25 22:26:29 +00001045 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046}
1047
1048/*
1049 * Return the nesting level for a funccall cookie.
1050 */
1051 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001052func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055}
1056
1057/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001058funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001060/* pointer to list of previously used funccal, still around because some
1061 * item in it is still being used. */
1062funccall_T *previous_funccal = NULL;
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064/*
1065 * Return TRUE when a function was ended by a ":return" command.
1066 */
1067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
1070 return current_funccal->returned;
1071}
1072
1073
1074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * Set an internal variable to a string value. Creates the variable if it does
1076 * not already exist.
1077 */
1078 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001081 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001082 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 val = vim_strsave(value);
1085 if (val != NULL)
1086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093 }
1094}
1095
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static char_u *redir_endp = NULL;
1099static char_u *redir_varname = NULL;
1100
1101/*
1102 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 * Returns OK if successfully completed the setup. FAIL otherwise.
1105 */
1106 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001107var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
1109 int save_emsg;
1110 int err;
1111 typval_T tv;
1112
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001114 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 {
1116 EMSG(_(e_invarg));
1117 return FAIL;
1118 }
1119
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 redir_varname = vim_strsave(name);
1122 if (redir_varname == NULL)
1123 return FAIL;
1124
1125 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1126 if (redir_lval == NULL)
1127 {
1128 var_redir_stop();
1129 return FAIL;
1130 }
1131
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 /* The output is stored in growarray "redir_ga" until redirection ends. */
1133 ga_init2(&redir_ga, (int)sizeof(char), 500);
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001136 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001137 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1139 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_endp != NULL && *redir_endp != NUL)
1142 /* Trailing characters are present after the variable name */
1143 EMSG(_(e_trailing));
1144 else
1145 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 var_redir_stop();
1148 return FAIL;
1149 }
1150
1151 /* check if we can write to the variable: set it to or append an empty
1152 * string */
1153 save_emsg = did_emsg;
1154 did_emsg = FALSE;
1155 tv.v_type = VAR_STRING;
1156 tv.vval.v_string = (char_u *)"";
1157 if (append)
1158 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1159 else
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001161 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001163 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 if (err)
1165 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 var_redir_stop();
1168 return FAIL;
1169 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170
1171 return OK;
1172}
1173
1174/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175 * Append "value[value_len]" to the variable set by var_redir_start().
1176 * The actual appending is postponed until redirection ends, because the value
1177 * appended may in fact be the string we write to, changing it may cause freed
1178 * memory to be used:
1179 * :redir => foo
1180 * :let foo
1181 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 */
1183 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001184var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
1188 if (redir_lval == NULL)
1189 return;
1190
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 {
1198 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 }
1201 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203}
1204
1205/*
1206 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 */
1209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001210var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 typval_T tv;
1213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 if (redir_lval != NULL)
1215 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 /* If there was no error: assign the text to the variable. */
1217 if (redir_endp != NULL)
1218 {
1219 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1220 tv.v_type = VAR_STRING;
1221 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 /* Call get_lval() again, if it's inside a Dict or List it may
1223 * have changed. */
1224 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001225 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001226 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1227 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1228 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 /* free the collected output */
1232 vim_free(redir_ga.ga_data);
1233 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001235 vim_free(redir_lval);
1236 redir_lval = NULL;
1237 }
1238 vim_free(redir_varname);
1239 redir_varname = NULL;
1240}
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242# if defined(FEAT_MBYTE) || defined(PROTO)
1243 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001244eval_charconvert(
1245 char_u *enc_from,
1246 char_u *enc_to,
1247 char_u *fname_from,
1248 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int err = FALSE;
1251
1252 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1253 set_vim_var_string(VV_CC_TO, enc_to, -1);
1254 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1255 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1256 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1257 err = TRUE;
1258 set_vim_var_string(VV_CC_FROM, NULL, -1);
1259 set_vim_var_string(VV_CC_TO, NULL, -1);
1260 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262
1263 if (err)
1264 return FAIL;
1265 return OK;
1266}
1267# endif
1268
1269# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_FNAME_IN, fname, -1);
1276 set_vim_var_string(VV_CMDARG, args, -1);
1277 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1278 err = TRUE;
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_CMDARG, NULL, -1);
1281
1282 if (err)
1283 {
1284 mch_remove(fname);
1285 return FAIL;
1286 }
1287 return OK;
1288}
1289# endif
1290
1291# if defined(FEAT_DIFF) || defined(PROTO)
1292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293eval_diff(
1294 char_u *origfile,
1295 char_u *newfile,
1296 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1301 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1302 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1303 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1306 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1307}
1308
1309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_patch(
1311 char_u *origfile,
1312 char_u *difffile,
1313 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 int err;
1316
1317 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1319 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1320 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1321 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1322 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1323 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1324}
1325# endif
1326
1327/*
1328 * Top level evaluation function, returning a boolean.
1329 * Sets "error" to TRUE if there was an error.
1330 * Return TRUE or FALSE.
1331 */
1332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001333eval_to_bool(
1334 char_u *arg,
1335 int *error,
1336 char_u **nextcmd,
1337 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 int retval = FALSE;
1341
1342 if (skip)
1343 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
1347 {
1348 *error = FALSE;
1349 if (!skip)
1350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354 }
1355 if (skip)
1356 --emsg_skip;
1357
1358 return retval;
1359}
1360
1361/*
1362 * Top level evaluation function, returning a string. If "skip" is TRUE,
1363 * only parsing to "nextcmd" is done, without reporting errors. Return
1364 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1365 */
1366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_string_skip(
1368 char_u *arg,
1369 char_u **nextcmd,
1370 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *retval;
1374
1375 if (skip)
1376 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 retval = NULL;
1379 else
1380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 retval = vim_strsave(get_tv_string(&tv));
1382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 if (skip)
1385 --emsg_skip;
1386
1387 return retval;
1388}
1389
1390/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391 * Skip over an expression at "*pp".
1392 * Return FAIL for an error, OK otherwise.
1393 */
1394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398
1399 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401}
1402
1403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001405 * When "convert" is TRUE convert a List into a sequence of lines and convert
1406 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Return pointer to allocated memory, or NULL for failure.
1408 */
1409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001410eval_to_string(
1411 char_u *arg,
1412 char_u **nextcmd,
1413 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001418#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 retval = NULL;
1424 else
1425 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001426 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 {
1428 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001429 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001430 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 if (tv.vval.v_list->lv_len > 0)
1433 ga_append(&ga, NL);
1434 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435 ga_append(&ga, NUL);
1436 retval = (char_u *)ga.ga_data;
1437 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001438#ifdef FEAT_FLOAT
1439 else if (convert && tv.v_type == VAR_FLOAT)
1440 {
1441 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1442 retval = vim_strsave(numbuf);
1443 }
1444#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001445 else
1446 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449
1450 return retval;
1451}
1452
1453/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001454 * Call eval_to_string() without using current local variables and using
1455 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001458eval_to_string_safe(
1459 char_u *arg,
1460 char_u **nextcmd,
1461 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 char_u *retval;
1464 void *save_funccalp;
1465
1466 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 ++sandbox;
1469 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001471 if (use_sandbox)
1472 --sandbox;
1473 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 restore_funccal(save_funccalp);
1475 return retval;
1476}
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Top level evaluation function, returning a number.
1480 * Evaluates "expr" silently.
1481 * Returns -1 for an error.
1482 */
1483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
Bram Moolenaar33570922005-01-25 22:26:29 +00001486 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001488 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490 ++emsg_off;
1491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 retval = -1;
1494 else
1495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001496 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 --emsg_off;
1500
1501 return retval;
1502}
1503
Bram Moolenaara40058a2005-07-11 22:42:07 +00001504/*
1505 * Prepare v: variable "idx" to be used.
1506 * Save the current typeval in "save_tv".
1507 * When not used yet add the variable to the v: hashtable.
1508 */
1509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511{
1512 *save_tv = vimvars[idx].vv_tv;
1513 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1514 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1515}
1516
1517/*
1518 * Restore v: variable "idx" to typeval "save_tv".
1519 * When no longer defined, remove the variable from the v: hashtable.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523{
1524 hashitem_T *hi;
1525
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526 vimvars[idx].vv_tv = *save_tv;
1527 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1528 {
1529 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1530 if (HASHITEM_EMPTY(hi))
1531 EMSG2(_(e_intern2), "restore_vimvar()");
1532 else
1533 hash_remove(&vimvarht, hi);
1534 }
1535}
1536
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001537#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538/*
1539 * Evaluate an expression to a list with suggestions.
1540 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001541 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542 */
1543 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545{
1546 typval_T save_val;
1547 typval_T rettv;
1548 list_T *list = NULL;
1549 char_u *p = skipwhite(expr);
1550
1551 /* Set "v:val" to the bad word. */
1552 prepare_vimvar(VV_VAL, &save_val);
1553 vimvars[VV_VAL].vv_type = VAR_STRING;
1554 vimvars[VV_VAL].vv_str = badword;
1555 if (p_verbose == 0)
1556 ++emsg_off;
1557
1558 if (eval1(&p, &rettv, TRUE) == OK)
1559 {
1560 if (rettv.v_type != VAR_LIST)
1561 clear_tv(&rettv);
1562 else
1563 list = rettv.vval.v_list;
1564 }
1565
1566 if (p_verbose == 0)
1567 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001568 restore_vimvar(VV_VAL, &save_val);
1569
1570 return list;
1571}
1572
1573/*
1574 * "list" is supposed to contain two items: a word and a number. Return the
1575 * word in "pp" and the number as the return value.
1576 * Return -1 if anything isn't right.
1577 * Used to get the good word and score from the eval_spell_expr() result.
1578 */
1579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001580get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581{
1582 listitem_T *li;
1583
1584 li = list->lv_first;
1585 if (li == NULL)
1586 return -1;
1587 *pp = get_tv_string(&li->li_tv);
1588
1589 li = li->li_next;
1590 if (li == NULL)
1591 return -1;
1592 return get_tv_number(&li->li_tv);
1593}
1594#endif
1595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001597 * Top level evaluation function.
1598 * Returns an allocated typval_T with the result.
1599 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 */
1601 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603{
1604 typval_T *tv;
1605
1606 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001607 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608 {
1609 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001610 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611 }
1612
1613 return tv;
1614}
1615
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 * Uses argv[argc] for the function arguments. Only Number and String
1620 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624call_vim_function(
1625 char_u *func,
1626 int argc,
1627 char_u **argv,
1628 int safe, /* use the sandbox */
1629 int str_arg_only, /* all arguments are strings */
1630 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 long n;
1634 int len;
1635 int i;
1636 int doesrange;
1637 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001640 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
1644 for (i = 0; i < argc; i++)
1645 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001646 /* Pass a NULL or empty argument as an empty string */
1647 if (argv[i] == NULL || *argv[i] == NUL)
1648 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001649 argvars[i].v_type = VAR_STRING;
1650 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 continue;
1652 }
1653
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001654 if (str_arg_only)
1655 len = 0;
1656 else
1657 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001658 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (len != 0 && len == (int)STRLEN(argv[i]))
1660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001661 argvars[i].v_type = VAR_NUMBER;
1662 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_STRING;
1667 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 }
1670
1671 if (safe)
1672 {
1673 save_funccalp = save_funccal();
1674 ++sandbox;
1675 }
1676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1678 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (safe)
1682 {
1683 --sandbox;
1684 restore_funccal(save_funccalp);
1685 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 vim_free(argvars);
1687
1688 if (ret == FAIL)
1689 clear_tv(rettv);
1690
1691 return ret;
1692}
1693
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001694/*
1695 * Call vimL function "func" and return the result as a number.
1696 * Returns -1 when calling the function fails.
1697 * Uses argv[argc] for the function arguments.
1698 */
1699 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retnr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001705{
1706 typval_T rettv;
1707 long retval;
1708
1709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1711 return -1;
1712
1713 retval = get_tv_number_chk(&rettv, NULL);
1714 clear_tv(&rettv);
1715 return retval;
1716}
1717
1718#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1719 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1720
Bram Moolenaar4f688582007-07-24 12:34:30 +00001721# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001723 * Call vimL function "func" and return the result as a string.
1724 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 * Uses argv[argc] for the function arguments.
1726 */
1727 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728call_func_retstr(
1729 char_u *func,
1730 int argc,
1731 char_u **argv,
1732 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733{
1734 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001735 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001737 /* All arguments are passed as strings, no conversion to number. */
1738 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 return NULL;
1740
1741 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 return retval;
1744}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001745# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001747/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001748 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retlist(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
1760
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001761 /* All arguments are passed as strings, no conversion to number. */
1762 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 return NULL;
1764
1765 if (rettv.v_type != VAR_LIST)
1766 {
1767 clear_tv(&rettv);
1768 return NULL;
1769 }
1770
1771 return rettv.vval.v_list;
1772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
1774
1775/*
1776 * Save the current function call pointer, and set it to NULL.
1777 * Used when executing autocommands and for ":source".
1778 */
1779 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 current_funccal = NULL;
1785 return (void *)fc;
1786}
1787
1788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001789restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791 funccall_T *fc = (funccall_T *)vfc;
1792
1793 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794}
1795
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796#if defined(FEAT_PROFILE) || defined(PROTO)
1797/*
1798 * Prepare profiling for entering a child or something else that is not
1799 * counted for the script/function itself.
1800 * Should always be called in pair with prof_child_exit().
1801 */
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803prof_child_enter(
1804 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805{
1806 funccall_T *fc = current_funccal;
1807
1808 if (fc != NULL && fc->func->uf_profiling)
1809 profile_start(&fc->prof_child);
1810 script_prof_save(tm);
1811}
1812
1813/*
1814 * Take care of time spent in a child.
1815 * Should always be called after prof_child_enter().
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818prof_child_exit(
1819 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001820{
1821 funccall_T *fc = current_funccal;
1822
1823 if (fc != NULL && fc->func->uf_profiling)
1824 {
1825 profile_end(&fc->prof_child);
1826 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1827 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1828 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1829 }
1830 script_prof_restore(tm);
1831}
1832#endif
1833
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#ifdef FEAT_FOLDING
1836/*
1837 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1838 * it in "*cp". Doesn't give error messages.
1839 */
1840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001841eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
Bram Moolenaar33570922005-01-25 22:26:29 +00001843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int retval;
1845 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001846 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1847 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 ++sandbox;
1852 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 retval = 0;
1856 else
1857 {
1858 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (tv.v_type == VAR_NUMBER)
1860 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001861 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 retval = 0;
1863 else
1864 {
1865 /* If the result is a string, check if there is a non-digit before
1866 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (!VIM_ISDIGIT(*s) && *s != '-')
1869 *cp = *s++;
1870 retval = atol((char *)s);
1871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 --sandbox;
1877 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 return retval;
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let" list all variable values
1885 * ":let var1 var2" list variable values
1886 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 * ":let var += expr" assignment command.
1888 * ":let var -= expr" assignment command.
1889 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 int var_count = 0;
1900 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001902 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaardb552d602006-03-23 22:59:57 +00001905 argend = skip_var_list(arg, &var_count, &semicolon);
1906 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001908 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1909 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 expr = skipwhite(argend);
1911 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1912 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001914 /*
1915 * ":let" without "=": list variables
1916 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 if (*arg == '[')
1918 EMSG(_(e_invarg));
1919 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_glob_vars(&first);
1926 list_buf_vars(&first);
1927 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001928#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_script_vars(&first);
1932 list_func_vars(&first);
1933 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 eap->nextcmd = check_nextcmd(arg);
1936 }
1937 else
1938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op[0] = '=';
1940 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1944 op[0] = *expr; /* +=, -= or .= */
1945 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 else
1948 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (eap->skip)
1951 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (eap->skip)
1954 {
1955 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 --emsg_skip;
1958 }
1959 else if (i != FAIL)
1960 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
1966}
1967
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968/*
1969 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1970 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 * When "nextchars" is not NULL it points to a string with characters that
1972 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1973 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 * Returns OK or FAIL;
1975 */
1976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977ex_let_vars(
1978 char_u *arg_start,
1979 typval_T *tv,
1980 int copy, /* copy values from "tv", don't move */
1981 int semicolon, /* from skip_var_list() */
1982 int var_count, /* from skip_var_list() */
1983 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984{
1985 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 listitem_T *item;
1989 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990
1991 if (*arg != '[')
1992 {
1993 /*
1994 * ":let var = expr" or ":for var in list"
1995 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 return FAIL;
1998 return OK;
1999 }
2000
2001 /*
2002 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2003 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002004 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 EMSG(_(e_listreq));
2007 return FAIL;
2008 }
2009
2010 i = list_len(l);
2011 if (semicolon == 0 && var_count < i)
2012 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002013 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 return FAIL;
2015 }
2016 if (var_count - semicolon > i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021
2022 item = l->lv_first;
2023 while (*arg != ']')
2024 {
2025 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 item = item->li_next;
2028 if (arg == NULL)
2029 return FAIL;
2030
2031 arg = skipwhite(arg);
2032 if (*arg == ';')
2033 {
2034 /* Put the rest of the list (may be empty) in the var after ';'.
2035 * Create a new list for this. */
2036 l = list_alloc();
2037 if (l == NULL)
2038 return FAIL;
2039 while (item != NULL)
2040 {
2041 list_append_tv(l, &item->li_tv);
2042 item = item->li_next;
2043 }
2044
2045 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002046 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 ltv.vval.v_list = l;
2048 l->lv_refcount = 1;
2049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2051 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 clear_tv(&ltv);
2053 if (arg == NULL)
2054 return FAIL;
2055 break;
2056 }
2057 else if (*arg != ',' && *arg != ']')
2058 {
2059 EMSG2(_(e_intern2), "ex_let_vars()");
2060 return FAIL;
2061 }
2062 }
2063
2064 return OK;
2065}
2066
2067/*
2068 * Skip over assignable variable "var" or list of variables "[var, var]".
2069 * Used for ":let varvar = expr" and ":for varvar in expr".
2070 * For "[var, var]" increment "*var_count" for each variable.
2071 * for "[var, var; var]" set "semicolon".
2072 * Return NULL for an error.
2073 */
2074 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002075skip_var_list(
2076 char_u *arg,
2077 int *var_count,
2078 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079{
2080 char_u *p, *s;
2081
2082 if (*arg == '[')
2083 {
2084 /* "[var, var]": find the matching ']'. */
2085 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002086 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 {
2088 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2089 s = skip_var_one(p);
2090 if (s == p)
2091 {
2092 EMSG2(_(e_invarg2), p);
2093 return NULL;
2094 }
2095 ++*var_count;
2096
2097 p = skipwhite(s);
2098 if (*p == ']')
2099 break;
2100 else if (*p == ';')
2101 {
2102 if (*semicolon == 1)
2103 {
2104 EMSG(_("Double ; in list of variables"));
2105 return NULL;
2106 }
2107 *semicolon = 1;
2108 }
2109 else if (*p != ',')
2110 {
2111 EMSG2(_(e_invarg2), p);
2112 return NULL;
2113 }
2114 }
2115 return p + 1;
2116 }
2117 else
2118 return skip_var_one(arg);
2119}
2120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002122 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002123 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 if (*arg == '@' && arg[1] != NUL)
2129 return arg + 2;
2130 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2131 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132}
2133
Bram Moolenaara7043832005-01-21 11:56:39 +00002134/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 * List variables for hashtab "ht" with prefix "prefix".
2136 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_hashtable_vars(
2140 hashtab_T *ht,
2141 char_u *prefix,
2142 int empty,
2143 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 hashitem_T *hi;
2146 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 int todo;
2148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002149 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2151 {
2152 if (!HASHITEM_EMPTY(hi))
2153 {
2154 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002155 di = HI2DI(hi);
2156 if (empty || di->di_tv.v_type != VAR_STRING
2157 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159 }
2160 }
2161}
2162
2163/*
2164 * List global variables.
2165 */
2166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002168{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002170}
2171
2172/*
2173 * List buffer variables.
2174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002177{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 char_u numbuf[NUMBUFLEN];
2179
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182
2183 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2185 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186}
2187
2188/*
2189 * List window variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002196}
2197
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198#ifdef FEAT_WINDOWS
2199/*
2200 * List tab page variables.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002207}
2208#endif
2209
Bram Moolenaara7043832005-01-21 11:56:39 +00002210/*
2211 * List Vim variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217}
2218
2219/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220 * List script-local variables, if there is a script.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224{
2225 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2227 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228}
2229
2230/*
2231 * List function variables, if there is a function.
2232 */
2233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235{
2236 if (current_funccal != NULL)
2237 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239}
2240
2241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 * List variables in "arg".
2243 */
2244 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246{
2247 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 char_u *name_start;
2251 char_u *arg_subsc;
2252 char_u *tofree;
2253 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254
2255 while (!ends_excmd(*arg) && !got_int)
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002259 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2261 {
2262 emsg_severe = TRUE;
2263 EMSG(_(e_trailing));
2264 break;
2265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 /* get_name_len() takes care of expanding curly braces */
2270 name_start = name = arg;
2271 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2272 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* This is mainly to keep test 49 working: when expanding
2275 * curly braces fails overrule the exception error message. */
2276 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 emsg_severe = TRUE;
2279 EMSG2(_(e_invarg2), arg);
2280 break;
2281 }
2282 error = TRUE;
2283 }
2284 else
2285 {
2286 if (tofree != NULL)
2287 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002288 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 /* handle d.key, l[idx], f(expr) */
2293 arg_subsc = arg;
2294 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'g': list_glob_vars(first); break;
2303 case 'b': list_buf_vars(first); break;
2304 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 'v': list_vim_vars(first); break;
2309 case 's': list_script_vars(first); break;
2310 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 default:
2312 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
2315 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 {
2317 char_u numbuf[NUMBUFLEN];
2318 char_u *tf;
2319 int c;
2320 char_u *s;
2321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002322 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 c = *arg;
2324 *arg = NUL;
2325 list_one_var_a((char_u *)"",
2326 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 tv.v_type,
2328 s == NULL ? (char_u *)"" : s,
2329 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 *arg = c;
2331 vim_free(tf);
2332 }
2333 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
2336 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337
2338 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002340
2341 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 return arg;
2345}
2346
2347/*
2348 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2349 * Returns a pointer to the char just after the var name.
2350 * Returns NULL if there is an error.
2351 */
2352 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353ex_let_one(
2354 char_u *arg, /* points to variable name */
2355 typval_T *tv, /* value to assign to variable */
2356 int copy, /* copy value from "tv" */
2357 char_u *endchars, /* valid chars after variable name or NULL */
2358 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359{
2360 int c1;
2361 char_u *name;
2362 char_u *p;
2363 char_u *arg_end = NULL;
2364 int len;
2365 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367
2368 /*
2369 * ":let $VAR = expr": Set environment variable.
2370 */
2371 if (*arg == '$')
2372 {
2373 /* Find the end of the name. */
2374 ++arg;
2375 name = arg;
2376 len = get_env_len(&arg);
2377 if (len == 0)
2378 EMSG2(_(e_invarg2), name - 1);
2379 else
2380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 if (op != NULL && (*op == '+' || *op == '-'))
2382 EMSG2(_(e_letwrong), op);
2383 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002386 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 {
2388 c1 = name[len];
2389 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 p = get_tv_string_chk(tv);
2391 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 {
2393 int mustfree = FALSE;
2394 char_u *s = vim_getenv(name, &mustfree);
2395
2396 if (s != NULL)
2397 {
2398 p = tofree = concat_str(s, p);
2399 if (mustfree)
2400 vim_free(s);
2401 }
2402 }
2403 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 if (STRICMP(name, "HOME") == 0)
2407 init_homedir();
2408 else if (didset_vim && STRICMP(name, "VIM") == 0)
2409 didset_vim = FALSE;
2410 else if (didset_vimruntime
2411 && STRICMP(name, "VIMRUNTIME") == 0)
2412 didset_vimruntime = FALSE;
2413 arg_end = arg;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419 }
2420
2421 /*
2422 * ":let &option = expr": Set option value.
2423 * ":let &l:option = expr": Set local option value.
2424 * ":let &g:option = expr": Set global option value.
2425 */
2426 else if (*arg == '&')
2427 {
2428 /* Find the end of the name. */
2429 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 if (p == NULL || (endchars != NULL
2431 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 long n;
2436 int opt_type;
2437 long numval;
2438 char_u *stringval = NULL;
2439 char_u *s;
2440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 c1 = *p;
2442 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443
2444 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 s = get_tv_string_chk(tv); /* != NULL if number or string */
2446 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
2448 opt_type = get_option_value(arg, &numval,
2449 &stringval, opt_flags);
2450 if ((opt_type == 1 && *op == '.')
2451 || (opt_type == 0 && *op != '.'))
2452 EMSG2(_(e_letwrong), op);
2453 else
2454 {
2455 if (opt_type == 1) /* number */
2456 {
2457 if (*op == '+')
2458 n = numval + n;
2459 else
2460 n = numval - n;
2461 }
2462 else if (opt_type == 0 && stringval != NULL) /* string */
2463 {
2464 s = concat_str(stringval, s);
2465 vim_free(stringval);
2466 stringval = s;
2467 }
2468 }
2469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 if (s != NULL)
2471 {
2472 set_option_value(arg, n, s, opt_flags);
2473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let @r = expr": Set register contents.
2482 */
2483 else if (*arg == '@')
2484 {
2485 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (op != NULL && (*op == '+' || *op == '-'))
2487 EMSG2(_(e_letwrong), op);
2488 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002489 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 EMSG(_(e_letunexp));
2491 else
2492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 char_u *s;
2495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 p = get_tv_string_chk(tv);
2497 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002499 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 if (s != NULL)
2501 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 vim_free(s);
2504 }
2505 }
2506 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 arg_end = arg + 1;
2510 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002511 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002523 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2527 EMSG(_(e_letunexp));
2528 else
2529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 arg_end = p;
2532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
2536
2537 else
2538 EMSG2(_(e_invarg2), arg);
2539
2540 return arg_end;
2541}
2542
2543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2545 */
2546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548{
2549 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2550 {
2551 EMSG2(_(e_readonlyvar), arg);
2552 return TRUE;
2553 }
2554 return FALSE;
2555}
2556
2557/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Get an lval: variable, Dict item or List item that can be assigned a value
2559 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2560 * "name.key", "name.key[expr]" etc.
2561 * Indexing only works if "name" is an existing List or Dictionary.
2562 * "name" points to the start of the name.
2563 * If "rettv" is not NULL it points to the value to be assigned.
2564 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2565 * wrong; must end in space or cmd separator.
2566 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 * flags:
2568 * GLV_QUIET: do not give error messages
2569 * GLV_NO_AUTOLOAD: do not use script autoloading
2570 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002572 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 */
2575 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002576get_lval(
2577 char_u *name,
2578 typval_T *rettv,
2579 lval_T *lp,
2580 int unlet,
2581 int skip,
2582 int flags, /* GLV_ values */
2583 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 char_u *expr_start, *expr_end;
2587 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 dictitem_T *v;
2589 typval_T var1;
2590 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002596 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600
2601 if (skip)
2602 {
2603 /* When skipping just find the end of the name. */
2604 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 }
2607
2608 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002609 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (expr_start != NULL)
2611 {
2612 /* Don't expand the name when we already know there is an error. */
2613 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2614 && *p != '[' && *p != '.')
2615 {
2616 EMSG(_(e_trailing));
2617 return NULL;
2618 }
2619
2620 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2621 if (lp->ll_exp_name == NULL)
2622 {
2623 /* Report an invalid expression in braces, unless the
2624 * expression evaluation has been cancelled due to an
2625 * aborting error, an interrupt, or an exception. */
2626 if (!aborting() && !quiet)
2627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002628 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 EMSG2(_(e_invarg2), name);
2630 return NULL;
2631 }
2632 }
2633 lp->ll_name = lp->ll_exp_name;
2634 }
2635 else
2636 lp->ll_name = name;
2637
2638 /* Without [idx] or .key we are done. */
2639 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2640 return p;
2641
2642 cc = *p;
2643 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002644 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (v == NULL && !quiet)
2646 EMSG2(_(e_undefvar), lp->ll_name);
2647 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 if (v == NULL)
2649 return NULL;
2650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 /*
2652 * Loop until no more [idx] or .key is following.
2653 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2658 && !(lp->ll_tv->v_type == VAR_DICT
2659 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E689: Can only index a List or Dictionary"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E708: [:] must come last"));
2669 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 len = -1;
2673 if (*p == '.')
2674 {
2675 key = p + 1;
2676 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2677 ;
2678 if (len == 0)
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!quiet)
2681 EMSG(_(e_emptykey));
2682 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
2684 p = key + len;
2685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p == ':')
2691 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 else
2693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 empty1 = FALSE;
2695 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 if (get_tv_string_chk(&var1) == NULL)
2698 {
2699 /* not a number or string */
2700 clear_tv(&var1);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
2705 /* Optionally get the second index [ :expr]. */
2706 if (*p == ':')
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (rettv != NULL && (rettv->v_type != VAR_LIST
2717 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 p = skipwhite(p + 1);
2726 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 else
2729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (!empty1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 if (get_tv_string_chk(&var2) == NULL)
2738 {
2739 /* not a number or string */
2740 if (!empty1)
2741 clear_tv(&var1);
2742 clear_tv(&var2);
2743 return NULL;
2744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761
2762 /* Skip to past ']'. */
2763 ++p;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
2768 if (len == -1)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (*key == NUL)
2773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (!quiet)
2775 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002780 lp->ll_list = NULL;
2781 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 /* When assigning to a scope dictionary check that a function and
2785 * variable name is valid (only variable name unless it is l: or
2786 * g: dictionary). Disallow overwriting a builtin function. */
2787 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 int prevval;
2790 int wrong;
2791
2792 if (len != -1)
2793 {
2794 prevval = key[len];
2795 key[len] = NUL;
2796 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002797 else
2798 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2800 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 || !valid_varname(key);
2803 if (len != -1)
2804 key[len] = prevval;
2805 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 return NULL;
2807 }
2808
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 /* Can't add "v:" variable. */
2812 if (lp->ll_dict == &vimvardict)
2813 {
2814 EMSG2(_(e_illvar), name);
2815 return NULL;
2816 }
2817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002818 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (len == -1)
2832 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 p = NULL;
2835 break;
2836 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002838 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 if (len == -1)
2842 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 }
2845 else
2846 {
2847 /*
2848 * Get the number and item for the only or first index of the List.
2849 */
2850 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
2853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002854 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 clear_tv(&var1);
2856 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_list = lp->ll_tv->vval.v_list;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002862 if (lp->ll_n1 < 0)
2863 {
2864 lp->ll_n1 = 0;
2865 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2866 }
2867 }
2868 if (lp->ll_li == NULL)
2869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 if (!quiet)
2873 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876
2877 /*
2878 * May need to find the item or absolute index for the second
2879 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 * When no index given: "lp->ll_empty2" is TRUE.
2881 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 {
2892 if (!quiet)
2893 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2900 if (lp->ll_n1 < 0)
2901 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2902 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 return p;
2915}
2916
2917/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 */
2920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922{
2923 vim_free(lp->ll_exp_name);
2924 vim_free(lp->ll_newkey);
2925}
2926
2927/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933set_var_lval(
2934 lval_T *lp,
2935 char_u *endp,
2936 typval_T *rettv,
2937 int copy,
2938 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939{
2940 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 listitem_T *ri;
2942 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943
2944 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 cc = *endp;
2949 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 if (op != NULL && *op != '=')
2951 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002954 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002956 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 if ((di == NULL
2960 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2961 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2962 FALSE)))
2963 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 set_var(lp->ll_name, &tv, FALSE);
2965 clear_tv(&tv);
2966 }
2967 }
2968 else
2969 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 else if (tv_check_lock(lp->ll_newkey == NULL
2974 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002975 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 else if (lp->ll_range)
2978 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 listitem_T *ll_li = lp->ll_li;
2980 int ll_n1 = lp->ll_n1;
2981
2982 /*
2983 * Check whether any of the list items is locked
2984 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002985 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 return;
2989 ri = ri->li_next;
2990 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2991 break;
2992 ll_li = ll_li->li_next;
2993 ++ll_n1;
2994 }
2995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 /*
2997 * Assign the List values to the list items.
2998 */
2999 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3003 else
3004 {
3005 clear_tv(&lp->ll_li->li_tv);
3006 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = ri->li_next;
3009 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3010 break;
3011 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003014 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 ri = NULL;
3017 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 lp->ll_li = lp->ll_li->li_next;
3021 ++lp->ll_n1;
3022 }
3023 if (ri != NULL)
3024 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 else if (lp->ll_empty2
3026 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 : lp->ll_n1 != lp->ll_n2)
3028 EMSG(_("E711: List value has not enough items"));
3029 }
3030 else
3031 {
3032 /*
3033 * Assign to a List or Dictionary item.
3034 */
3035 if (lp->ll_newkey != NULL)
3036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 if (op != NULL && *op != '=')
3038 {
3039 EMSG2(_(e_letwrong), op);
3040 return;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 if (di == NULL)
3046 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3048 {
3049 vim_free(di);
3050 return;
3051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003053 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 else if (op != NULL && *op != '=')
3055 {
3056 tv_op(lp->ll_tv, rettv, op);
3057 return;
3058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 /*
3063 * Assign the value to the variable or list item.
3064 */
3065 if (copy)
3066 copy_tv(rettv, lp->ll_tv);
3067 else
3068 {
3069 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003070 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 }
3073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074}
3075
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3078 * Returns OK or FAIL.
3079 */
3080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082{
3083 long n;
3084 char_u numbuf[NUMBUFLEN];
3085 char_u *s;
3086
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003087 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3088 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3089 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 {
3091 switch (tv1->v_type)
3092 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 case VAR_DICT:
3095 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003098 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 break;
3100
3101 case VAR_LIST:
3102 if (*op != '+' || tv2->v_type != VAR_LIST)
3103 break;
3104 /* List += List */
3105 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3106 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3107 return OK;
3108
3109 case VAR_NUMBER:
3110 case VAR_STRING:
3111 if (tv2->v_type == VAR_LIST)
3112 break;
3113 if (*op == '+' || *op == '-')
3114 {
3115 /* nr += nr or nr -= nr*/
3116 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (tv2->v_type == VAR_FLOAT)
3119 {
3120 float_T f = n;
3121
3122 if (*op == '+')
3123 f += tv2->vval.v_float;
3124 else
3125 f -= tv2->vval.v_float;
3126 clear_tv(tv1);
3127 tv1->v_type = VAR_FLOAT;
3128 tv1->vval.v_float = f;
3129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#endif
3132 {
3133 if (*op == '+')
3134 n += get_tv_number(tv2);
3135 else
3136 n -= get_tv_number(tv2);
3137 clear_tv(tv1);
3138 tv1->v_type = VAR_NUMBER;
3139 tv1->vval.v_number = n;
3140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 }
3142 else
3143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (tv2->v_type == VAR_FLOAT)
3145 break;
3146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 /* str .= str */
3148 s = get_tv_string(tv1);
3149 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_STRING;
3152 tv1->vval.v_string = s;
3153 }
3154 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003157#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 {
3159 float_T f;
3160
3161 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3162 && tv2->v_type != VAR_NUMBER
3163 && tv2->v_type != VAR_STRING))
3164 break;
3165 if (tv2->v_type == VAR_FLOAT)
3166 f = tv2->vval.v_float;
3167 else
3168 f = get_tv_number(tv2);
3169 if (*op == '+')
3170 tv1->vval.v_float += f;
3171 else
3172 tv1->vval.v_float -= f;
3173 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003175 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003176 }
3177 }
3178
3179 EMSG2(_(e_letwrong), op);
3180 return FAIL;
3181}
3182
3183/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 * Add a watcher to a list.
3185 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003187list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 lwp = &l->lv_watch;
3203 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3204 {
3205 if (lw == lwrem)
3206 {
3207 *lwp = lw->lw_next;
3208 break;
3209 }
3210 lwp = &lw->lw_next;
3211 }
3212}
3213
3214/*
3215 * Just before removing an item from a list: advance watchers to the next
3216 * item.
3217 */
3218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222
3223 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3224 if (lw->lw_item == item)
3225 lw->lw_item = item->li_next;
3226}
3227
3228/*
3229 * Evaluate the expression used in a ":for var in expr" command.
3230 * "arg" points to "var".
3231 * Set "*errp" to TRUE for an error, FALSE otherwise;
3232 * Return a pointer that holds the info. Null when there is an error.
3233 */
3234 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval_for_line(
3236 char_u *arg,
3237 int *errp,
3238 char_u **nextcmdp,
3239 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 typval_T tv;
3244 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 *errp = TRUE; /* default: there is an error */
3247
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 if (fi == NULL)
3250 return NULL;
3251
3252 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3253 if (expr == NULL)
3254 return fi;
3255
3256 expr = skipwhite(expr);
3257 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3258 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003259 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 return fi;
3261 }
3262
3263 if (skip)
3264 ++emsg_skip;
3265 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3266 {
3267 *errp = FALSE;
3268 if (!skip)
3269 {
3270 l = tv.vval.v_list;
3271 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 clear_tv(&tv);
3275 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 else
3277 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003278 /* No need to increment the refcount, it's already set for the
3279 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 fi->fi_list = l;
3281 list_add_watch(l, &fi->fi_lw);
3282 fi->fi_lw.lw_item = l->lv_first;
3283 }
3284 }
3285 }
3286 if (skip)
3287 --emsg_skip;
3288
3289 return fi;
3290}
3291
3292/*
3293 * Use the first item in a ":for" list. Advance to the next.
3294 * Assign the values to the variable (list). "arg" points to the first one.
3295 * Return TRUE when a valid item was found, FALSE when at end of list or
3296 * something wrong.
3297 */
3298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003301 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304
3305 item = fi->fi_lw.lw_item;
3306 if (item == NULL)
3307 result = FALSE;
3308 else
3309 {
3310 fi->fi_lw.lw_item = item->li_next;
3311 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3312 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3313 }
3314 return result;
3315}
3316
3317/*
3318 * Free the structure used to store info used by ":for".
3319 */
3320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003325 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 list_unref(fi->fi_list);
3329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 vim_free(fi);
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3334
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336set_context_for_expression(
3337 expand_T *xp,
3338 char_u *arg,
3339 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 int got_eq = FALSE;
3342 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (cmdidx == CMD_let)
3346 {
3347 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 if (vim_iswhite(*p))
3356 break;
3357 }
3358 return;
3359 }
3360 }
3361 else
3362 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3363 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((xp->xp_pattern = vim_strpbrk(arg,
3365 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3366 {
3367 c = *xp->xp_pattern;
3368 if (c == '&')
3369 {
3370 c = xp->xp_pattern[1];
3371 if (c == '&')
3372 {
3373 ++xp->xp_pattern;
3374 xp->xp_context = cmdidx != CMD_let || got_eq
3375 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3376 }
3377 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003380 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3381 xp->xp_pattern += 2;
3382
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else if (c == '$')
3386 {
3387 /* environment variable */
3388 xp->xp_context = EXPAND_ENV_VARS;
3389 }
3390 else if (c == '=')
3391 {
3392 got_eq = TRUE;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && xp->xp_context == EXPAND_FUNCTIONS
3397 && vim_strchr(xp->xp_pattern, '(') == NULL)
3398 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
3402 else if (cmdidx != CMD_let || got_eq)
3403 {
3404 if (c == '"') /* string */
3405 {
3406 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3407 if (c == '\\' && xp->xp_pattern[1] != NUL)
3408 ++xp->xp_pattern;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '\'') /* literal string */
3412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3415 /* skip */ ;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '|')
3419 {
3420 if (xp->xp_pattern[1] == '|')
3421 {
3422 ++xp->xp_pattern;
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
3426 xp->xp_context = EXPAND_COMMANDS;
3427 }
3428 else
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003432 /* Doesn't look like something valid, expand as an expression
3433 * anyway. */
3434 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 arg = xp->xp_pattern;
3436 if (*arg != NUL)
3437 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3438 /* skip */ ;
3439 }
3440 xp->xp_pattern = arg;
3441}
3442
3443#endif /* FEAT_CMDL_COMPL */
3444
3445/*
3446 * ":1,25call func(arg1, arg2)" function call.
3447 */
3448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *arg = eap->arg;
3452 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 linenr_T lnum;
3458 int doesrange;
3459 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003462 if (eap->skip)
3463 {
3464 /* trans_function_name() doesn't work well when skipping, use eval0()
3465 * instead to skip to any following command, e.g. for:
3466 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003467 ++emsg_skip;
3468 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3469 clear_tv(&rettv);
3470 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003471 return;
3472 }
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003475 if (fudi.fd_newkey != NULL)
3476 {
3477 /* Still need to give an error message for missing key. */
3478 EMSG2(_(e_dictkey), fudi.fd_newkey);
3479 vim_free(fudi.fd_newkey);
3480 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 if (tofree == NULL)
3482 return;
3483
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003484 /* Increase refcount on dictionary, it could get deleted when evaluating
3485 * the arguments. */
3486 if (fudi.fd_dict != NULL)
3487 ++fudi.fd_dict->dv_refcount;
3488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003491 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar532c7802005-01-27 14:44:31 +00003493 /* Skip white space to allow ":call func ()". Not good, but required for
3494 * backward compatibility. */
3495 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 if (*startarg != '(')
3499 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003500 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 goto end;
3502 }
3503
3504 /*
3505 * When skipping, evaluate the function once, to find the end of the
3506 * arguments.
3507 * When the function takes a range, this is discovered after the first
3508 * call, and the loop is broken.
3509 */
3510 if (eap->skip)
3511 {
3512 ++emsg_skip;
3513 lnum = eap->line2; /* do it once, also with an invalid range */
3514 }
3515 else
3516 lnum = eap->line1;
3517 for ( ; lnum <= eap->line2; ++lnum)
3518 {
3519 if (!eap->skip && eap->addr_count > 0)
3520 {
3521 curwin->w_cursor.lnum = lnum;
3522 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003523#ifdef FEAT_VIRTUALEDIT
3524 curwin->w_cursor.coladd = 0;
3525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 eap->line1, eap->line2, &doesrange,
3530 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 failed = TRUE;
3533 break;
3534 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003535
3536 /* Handle a function returning a Funcref, Dictionary or List. */
3537 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3538 {
3539 failed = TRUE;
3540 break;
3541 }
3542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (doesrange || eap->skip)
3545 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (aborting())
3552 break;
3553 }
3554 if (eap->skip)
3555 --emsg_skip;
3556
3557 if (!failed)
3558 {
3559 /* Check for trailing illegal characters and a following command. */
3560 if (!ends_excmd(*arg))
3561 {
3562 emsg_severe = TRUE;
3563 EMSG(_(e_trailing));
3564 }
3565 else
3566 eap->nextcmd = check_nextcmd(arg);
3567 }
3568
3569end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
3575 * ":unlet[!] var1 ... " command.
3576 */
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 ex_unletlock(eap, eap->arg, 0);
3581}
3582
3583/*
3584 * ":lockvar" and ":unlockvar" commands
3585 */
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607ex_unletlock(
3608 exarg_T *eap,
3609 char_u *argstart,
3610 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662do_unlet_var(
3663 lval_T *lp,
3664 char_u *name_end,
3665 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003740 if (ht == &globvarht)
3741 d = &globvardict;
3742 else if (current_funccal != NULL
3743 && ht == &current_funccal->l_vars.dv_hashtab)
3744 d = &current_funccal->l_vars;
3745 else if (ht == &compat_hashtab)
3746 d = &vimvardict;
3747 else
3748 {
3749 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3750 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3751 }
3752 if (d == NULL)
3753 {
3754 EMSG2(_(e_intern2), "do_unlet()");
3755 return FAIL;
3756 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 hi = hash_find(ht, varname);
3758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003760 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003761 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003762 || var_check_ro(di->di_flags, name, FALSE)
3763 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003764 return FAIL;
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 delete_var(ht, hi);
3767 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770 if (forceit)
3771 return OK;
3772 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774}
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776/*
3777 * Lock or unlock variable indicated by "lp".
3778 * "deep" is the levels to go (-1 for unlimited);
3779 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3780 */
3781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782do_lock_var(
3783 lval_T *lp,
3784 char_u *name_end,
3785 int deep,
3786 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787{
3788 int ret = OK;
3789 int cc;
3790 dictitem_T *di;
3791
3792 if (deep == 0) /* nothing to do */
3793 return OK;
3794
3795 if (lp->ll_tv == NULL)
3796 {
3797 cc = *name_end;
3798 *name_end = NUL;
3799
3800 /* Normal name or expanded name. */
3801 if (check_changedtick(lp->ll_name))
3802 ret = FAIL;
3803 else
3804 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003805 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 if (di == NULL)
3807 ret = FAIL;
3808 else
3809 {
3810 if (lock)
3811 di->di_flags |= DI_FLAGS_LOCK;
3812 else
3813 di->di_flags &= ~DI_FLAGS_LOCK;
3814 item_lock(&di->di_tv, deep, lock);
3815 }
3816 }
3817 *name_end = cc;
3818 }
3819 else if (lp->ll_range)
3820 {
3821 listitem_T *li = lp->ll_li;
3822
3823 /* (un)lock a range of List items. */
3824 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3825 {
3826 item_lock(&li->li_tv, deep, lock);
3827 li = li->li_next;
3828 ++lp->ll_n1;
3829 }
3830 }
3831 else if (lp->ll_list != NULL)
3832 /* (un)lock a List item. */
3833 item_lock(&lp->ll_li->li_tv, deep, lock);
3834 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003835 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 item_lock(&lp->ll_di->di_tv, deep, lock);
3837
3838 return ret;
3839}
3840
3841/*
3842 * Lock or unlock an item. "deep" is nr of levels to go.
3843 */
3844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846{
3847 static int recurse = 0;
3848 list_T *l;
3849 listitem_T *li;
3850 dict_T *d;
3851 hashitem_T *hi;
3852 int todo;
3853
3854 if (recurse >= DICT_MAXNEST)
3855 {
3856 EMSG(_("E743: variable nested too deep for (un)lock"));
3857 return;
3858 }
3859 if (deep == 0)
3860 return;
3861 ++recurse;
3862
3863 /* lock/unlock the item itself */
3864 if (lock)
3865 tv->v_lock |= VAR_LOCKED;
3866 else
3867 tv->v_lock &= ~VAR_LOCKED;
3868
3869 switch (tv->v_type)
3870 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 case VAR_UNKNOWN:
3872 case VAR_NUMBER:
3873 case VAR_STRING:
3874 case VAR_FUNC:
3875 case VAR_FLOAT:
3876 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003877 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003878 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879 break;
3880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003881 case VAR_LIST:
3882 if ((l = tv->vval.v_list) != NULL)
3883 {
3884 if (lock)
3885 l->lv_lock |= VAR_LOCKED;
3886 else
3887 l->lv_lock &= ~VAR_LOCKED;
3888 if (deep < 0 || deep > 1)
3889 /* recursive: lock/unlock the items the List contains */
3890 for (li = l->lv_first; li != NULL; li = li->li_next)
3891 item_lock(&li->li_tv, deep - 1, lock);
3892 }
3893 break;
3894 case VAR_DICT:
3895 if ((d = tv->vval.v_dict) != NULL)
3896 {
3897 if (lock)
3898 d->dv_lock |= VAR_LOCKED;
3899 else
3900 d->dv_lock &= ~VAR_LOCKED;
3901 if (deep < 0 || deep > 1)
3902 {
3903 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003904 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3906 {
3907 if (!HASHITEM_EMPTY(hi))
3908 {
3909 --todo;
3910 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3911 }
3912 }
3913 }
3914 }
3915 }
3916 --recurse;
3917}
3918
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003920 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3921 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003922 */
3923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003925{
3926 return (tv->v_lock & VAR_LOCKED)
3927 || (tv->v_type == VAR_LIST
3928 && tv->vval.v_list != NULL
3929 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3930 || (tv->v_type == VAR_DICT
3931 && tv->vval.v_dict != NULL
3932 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3933}
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3936/*
3937 * Delete all "menutrans_" variables.
3938 */
3939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 {
3949 if (!HASHITEM_EMPTY(hi))
3950 {
3951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3953 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 }
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957}
3958#endif
3959
3960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3961
3962/*
3963 * Local string buffer for the next two functions to store a variable name
3964 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3965 * get_user_var_name().
3966 */
3967
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003968static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970static char_u *varnamebuf = NULL;
3971static int varnamebuflen = 0;
3972
3973/*
3974 * Function to concatenate a prefix and a variable name.
3975 */
3976 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003977cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int len;
3980
3981 len = (int)STRLEN(name) + 3;
3982 if (len > varnamebuflen)
3983 {
3984 vim_free(varnamebuf);
3985 len += 10; /* some additional space */
3986 varnamebuf = alloc(len);
3987 if (varnamebuf == NULL)
3988 {
3989 varnamebuflen = 0;
3990 return NULL;
3991 }
3992 varnamebuflen = len;
3993 }
3994 *varnamebuf = prefix;
3995 varnamebuf[1] = ':';
3996 STRCPY(varnamebuf + 2, name);
3997 return varnamebuf;
3998}
3999
4000/*
4001 * Function given to ExpandGeneric() to obtain the list of user defined
4002 * (global/buffer/window/built-in) variable names.
4003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127eval0(
4128 char_u *arg,
4129 typval_T *rettv,
4130 char_u **nextcmd,
4131 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
4175 * Get the first variable.
4176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 if ((*arg)[0] == '?')
4181 {
4182 result = FALSE;
4183 if (evaluate)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 int error = FALSE;
4186
4187 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193
4194 /*
4195 * Get the second variable.
4196 */
4197 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200
4201 /*
4202 * Check for the ":".
4203 */
4204 if ((*arg)[0] != ':')
4205 {
4206 EMSG(_("E109: Missing ':' after '?'"));
4207 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210 }
4211
4212 /*
4213 * Get the third variable.
4214 */
4215 *arg = skipwhite(*arg + 1);
4216 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4217 {
4218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221 }
4222 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225
4226 return OK;
4227}
4228
4229/*
4230 * Handle first level expression:
4231 * expr2 || expr2 || expr2 logical OR
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004239eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 long result;
4243 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * Get the first variable.
4248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
4251
4252 /*
4253 * Repeat until there is no following "||".
4254 */
4255 first = TRUE;
4256 result = FALSE;
4257 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4258 {
4259 if (evaluate && first)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 first = FALSE;
4267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 2);
4273 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4274 return FAIL;
4275
4276 /*
4277 * Compute the result.
4278 */
4279 if (evaluate && !result)
4280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (error)
4285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_NUMBER;
4290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * Handle second level expression:
4299 * expr3 && expr3 && expr3 logical AND
4300 *
4301 * "arg" must point to the first non-white of the expression.
4302 * "arg" is advanced to the next non-white after the recognized expression.
4303 *
4304 * Return OK or FAIL.
4305 */
4306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004307eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 long result;
4311 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /*
4315 * Get the first variable.
4316 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319
4320 /*
4321 * Repeat until there is no following "&&".
4322 */
4323 first = TRUE;
4324 result = TRUE;
4325 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4326 {
4327 if (evaluate && first)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 first = FALSE;
4335 }
4336
4337 /*
4338 * Get the second variable.
4339 */
4340 *arg = skipwhite(*arg + 2);
4341 if (eval4(arg, &var2, evaluate && result) == FAIL)
4342 return FAIL;
4343
4344 /*
4345 * Compute the result.
4346 */
4347 if (evaluate && result)
4348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 if (error)
4353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 if (evaluate)
4356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_NUMBER;
4358 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 }
4361
4362 return OK;
4363}
4364
4365/*
4366 * Handle third level expression:
4367 * var1 == var2
4368 * var1 =~ var2
4369 * var1 != var2
4370 * var1 !~ var2
4371 * var1 > var2
4372 * var1 >= var2
4373 * var1 < var2
4374 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004375 * var1 is var2
4376 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *
4378 * "arg" must point to the first non-white of the expression.
4379 * "arg" is advanced to the next non-white after the recognized expression.
4380 *
4381 * Return OK or FAIL.
4382 */
4383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004384eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u *p;
4388 int i;
4389 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int len = 2;
4392 long n1, n2;
4393 char_u *s1, *s2;
4394 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4395 regmatch_T regmatch;
4396 int ic;
4397 char_u *save_cpo;
4398
4399 /*
4400 * Get the first variable.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return FAIL;
4404
4405 p = *arg;
4406 switch (p[0])
4407 {
4408 case '=': if (p[1] == '=')
4409 type = TYPE_EQUAL;
4410 else if (p[1] == '~')
4411 type = TYPE_MATCH;
4412 break;
4413 case '!': if (p[1] == '=')
4414 type = TYPE_NEQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_NOMATCH;
4417 break;
4418 case '>': if (p[1] != '=')
4419 {
4420 type = TYPE_GREATER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_GEQUAL;
4425 break;
4426 case '<': if (p[1] != '=')
4427 {
4428 type = TYPE_SMALLER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_SEQUAL;
4433 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 case 'i': if (p[1] == 's')
4435 {
4436 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4437 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004438 i = p[len];
4439 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 {
4441 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4442 type_is = TRUE;
4443 }
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447
4448 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
4451 if (type != TYPE_UNKNOWN)
4452 {
4453 /* extra question mark appended: ignore case */
4454 if (p[len] == '?')
4455 {
4456 ic = TRUE;
4457 ++len;
4458 }
4459 /* extra '#' appended: match case */
4460 else if (p[len] == '#')
4461 {
4462 ic = FALSE;
4463 ++len;
4464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004465 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
4467 ic = p_ic;
4468
4469 /*
4470 * Get the second variable.
4471 */
4472 *arg = skipwhite(p + len);
4473 if (eval5(arg, &var2, evaluate) == FAIL)
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477 }
4478
4479 if (evaluate)
4480 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 if (type_is && rettv->v_type != var2.v_type)
4482 {
4483 /* For "is" a different type always means FALSE, for "notis"
4484 * it means TRUE. */
4485 n1 = (type == TYPE_NEQUAL);
4486 }
4487 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4488 {
4489 if (type_is)
4490 {
4491 n1 = (rettv->v_type == var2.v_type
4492 && rettv->vval.v_list == var2.vval.v_list);
4493 if (type == TYPE_NEQUAL)
4494 n1 = !n1;
4495 }
4496 else if (rettv->v_type != var2.v_type
4497 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4498 {
4499 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004500 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004502 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 clear_tv(rettv);
4504 clear_tv(&var2);
4505 return FAIL;
4506 }
4507 else
4508 {
4509 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004510 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4511 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 if (type == TYPE_NEQUAL)
4513 n1 = !n1;
4514 }
4515 }
4516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004517 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4518 {
4519 if (type_is)
4520 {
4521 n1 = (rettv->v_type == var2.v_type
4522 && rettv->vval.v_dict == var2.vval.v_dict);
4523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 else if (rettv->v_type != var2.v_type
4527 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4528 {
4529 if (rettv->v_type != var2.v_type)
4530 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4531 else
4532 EMSG(_("E736: Invalid operation for Dictionary"));
4533 clear_tv(rettv);
4534 clear_tv(&var2);
4535 return FAIL;
4536 }
4537 else
4538 {
4539 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004540 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4541 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004542 if (type == TYPE_NEQUAL)
4543 n1 = !n1;
4544 }
4545 }
4546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4548 {
4549 if (rettv->v_type != var2.v_type
4550 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4551 {
4552 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004553 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 clear_tv(rettv);
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 else
4561 {
4562 /* Compare two Funcrefs for being equal or unequal. */
4563 if (rettv->vval.v_string == NULL
4564 || var2.vval.v_string == NULL)
4565 n1 = FALSE;
4566 else
4567 n1 = STRCMP(rettv->vval.v_string,
4568 var2.vval.v_string) == 0;
4569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
4571 }
4572 }
4573
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574#ifdef FEAT_FLOAT
4575 /*
4576 * If one of the two variables is a float, compare as a float.
4577 * When using "=~" or "!~", always compare as string.
4578 */
4579 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
4582 float_T f1, f2;
4583
4584 if (rettv->v_type == VAR_FLOAT)
4585 f1 = rettv->vval.v_float;
4586 else
4587 f1 = get_tv_number(rettv);
4588 if (var2.v_type == VAR_FLOAT)
4589 f2 = var2.vval.v_float;
4590 else
4591 f2 = get_tv_number(&var2);
4592 n1 = FALSE;
4593 switch (type)
4594 {
4595 case TYPE_EQUAL: n1 = (f1 == f2); break;
4596 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4597 case TYPE_GREATER: n1 = (f1 > f2); break;
4598 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4599 case TYPE_SMALLER: n1 = (f1 < f2); break;
4600 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4601 case TYPE_UNKNOWN:
4602 case TYPE_MATCH:
4603 case TYPE_NOMATCH: break; /* avoid gcc warning */
4604 }
4605 }
4606#endif
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * If one of the two variables is a number, compare as a number.
4610 * When using "=~" or "!~", always compare as string.
4611 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 n1 = get_tv_number(rettv);
4616 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 switch (type)
4618 {
4619 case TYPE_EQUAL: n1 = (n1 == n2); break;
4620 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4621 case TYPE_GREATER: n1 = (n1 > n2); break;
4622 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4623 case TYPE_SMALLER: n1 = (n1 < n2); break;
4624 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4625 case TYPE_UNKNOWN:
4626 case TYPE_MATCH:
4627 case TYPE_NOMATCH: break; /* avoid gcc warning */
4628 }
4629 }
4630 else
4631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 s1 = get_tv_string_buf(rettv, buf1);
4633 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4636 else
4637 i = 0;
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (i == 0); break;
4642 case TYPE_NEQUAL: n1 = (i != 0); break;
4643 case TYPE_GREATER: n1 = (i > 0); break;
4644 case TYPE_GEQUAL: n1 = (i >= 0); break;
4645 case TYPE_SMALLER: n1 = (i < 0); break;
4646 case TYPE_SEQUAL: n1 = (i <= 0); break;
4647
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH:
4650 /* avoid 'l' flag in 'cpoptions' */
4651 save_cpo = p_cpo;
4652 p_cpo = (char_u *)"";
4653 regmatch.regprog = vim_regcomp(s2,
4654 RE_MAGIC + RE_STRING);
4655 regmatch.rm_ic = ic;
4656 if (regmatch.regprog != NULL)
4657 {
4658 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (type == TYPE_NOMATCH)
4661 n1 = !n1;
4662 }
4663 p_cpo = save_cpo;
4664 break;
4665
4666 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4667 }
4668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 rettv->v_type = VAR_NUMBER;
4672 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
4675
4676 return OK;
4677}
4678
4679/*
4680 * Handle fourth level expression:
4681 * + number addition
4682 * - number subtraction
4683 * . string concatenation
4684 *
4685 * "arg" must point to the first non-white of the expression.
4686 * "arg" is advanced to the next non-white after the recognized expression.
4687 *
4688 * Return OK or FAIL.
4689 */
4690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 typval_T var2;
4694 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int op;
4696 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 float_T f1 = 0, f2 = 0;
4699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 char_u *s1, *s2;
4701 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4702 char_u *p;
4703
4704 /*
4705 * Get the first variable.
4706 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709
4710 /*
4711 * Repeat computing, until no '+', '-' or '.' is following.
4712 */
4713 for (;;)
4714 {
4715 op = **arg;
4716 if (op != '+' && op != '-' && op != '.')
4717 break;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 if ((op != '+' || rettv->v_type != VAR_LIST)
4720#ifdef FEAT_FLOAT
4721 && (op == '.' || rettv->v_type != VAR_FLOAT)
4722#endif
4723 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
4725 /* For "list + ...", an illegal use of the first operand as
4726 * a number cannot be determined before evaluating the 2nd
4727 * operand: if this is also a list, all is ok.
4728 * For "something . ...", "something - ..." or "non-list + ...",
4729 * we know that the first operand needs to be a string or number
4730 * without evaluating the 2nd operand. So check before to avoid
4731 * side effects after an error. */
4732 if (evaluate && get_tv_string_chk(rettv) == NULL)
4733 {
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737 }
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /*
4740 * Get the second variable.
4741 */
4742 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return FAIL;
4747 }
4748
4749 if (evaluate)
4750 {
4751 /*
4752 * Compute the result.
4753 */
4754 if (op == '.')
4755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4757 s2 = get_tv_string_buf_chk(&var2, buf2);
4758 if (s2 == NULL) /* type error ? */
4759 {
4760 clear_tv(rettv);
4761 clear_tv(&var2);
4762 return FAIL;
4763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004764 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 clear_tv(rettv);
4766 rettv->v_type = VAR_STRING;
4767 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004769 else if (op == '+' && rettv->v_type == VAR_LIST
4770 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004771 {
4772 /* concatenate Lists */
4773 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4774 &var3) == FAIL)
4775 {
4776 clear_tv(rettv);
4777 clear_tv(&var2);
4778 return FAIL;
4779 }
4780 clear_tv(rettv);
4781 *rettv = var3;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
4784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 int error = FALSE;
4786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787#ifdef FEAT_FLOAT
4788 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 f1 = rettv->vval.v_float;
4791 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 else
4794#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 n1 = get_tv_number_chk(rettv, &error);
4797 if (error)
4798 {
4799 /* This can only happen for "list + non-list". For
4800 * "non-list + ..." or "something - ...", we returned
4801 * before evaluating the 2nd operand. */
4802 clear_tv(rettv);
4803 return FAIL;
4804 }
4805#ifdef FEAT_FLOAT
4806 if (var2.v_type == VAR_FLOAT)
4807 f1 = n1;
4808#endif
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 {
4813 f2 = var2.vval.v_float;
4814 n2 = 0;
4815 }
4816 else
4817#endif
4818 {
4819 n2 = get_tv_number_chk(&var2, &error);
4820 if (error)
4821 {
4822 clear_tv(rettv);
4823 clear_tv(&var2);
4824 return FAIL;
4825 }
4826#ifdef FEAT_FLOAT
4827 if (rettv->v_type == VAR_FLOAT)
4828 f2 = n2;
4829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832
4833#ifdef FEAT_FLOAT
4834 /* If there is a float on either side the result is a float. */
4835 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4836 {
4837 if (op == '+')
4838 f1 = f1 + f2;
4839 else
4840 f1 = f1 - f2;
4841 rettv->v_type = VAR_FLOAT;
4842 rettv->vval.v_float = f1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#endif
4846 {
4847 if (op == '+')
4848 n1 = n1 + n2;
4849 else
4850 n1 = n1 - n2;
4851 rettv->v_type = VAR_NUMBER;
4852 rettv->vval.v_number = n1;
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 return OK;
4859}
4860
4861/*
4862 * Handle fifth level expression:
4863 * * number multiplication
4864 * / number division
4865 * % number modulo
4866 *
4867 * "arg" must point to the first non-white of the expression.
4868 * "arg" is advanced to the next non-white after the recognized expression.
4869 *
4870 * Return OK or FAIL.
4871 */
4872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873eval6(
4874 char_u **arg,
4875 typval_T *rettv,
4876 int evaluate,
4877 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int op;
4881 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882#ifdef FEAT_FLOAT
4883 int use_float = FALSE;
4884 float_T f1 = 0, f2;
4885#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Get the first variable.
4890 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004891 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return FAIL;
4893
4894 /*
4895 * Repeat computing, until no '*', '/' or '%' is following.
4896 */
4897 for (;;)
4898 {
4899 op = **arg;
4900 if (op != '*' && op != '/' && op != '%')
4901 break;
4902
4903 if (evaluate)
4904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#ifdef FEAT_FLOAT
4906 if (rettv->v_type == VAR_FLOAT)
4907 {
4908 f1 = rettv->vval.v_float;
4909 use_float = TRUE;
4910 n1 = 0;
4911 }
4912 else
4913#endif
4914 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 if (error)
4917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
4920 n1 = 0;
4921
4922 /*
4923 * Get the second variable.
4924 */
4925 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928
4929 if (evaluate)
4930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 if (var2.v_type == VAR_FLOAT)
4933 {
4934 if (!use_float)
4935 {
4936 f1 = n1;
4937 use_float = TRUE;
4938 }
4939 f2 = var2.vval.v_float;
4940 n2 = 0;
4941 }
4942 else
4943#endif
4944 {
4945 n2 = get_tv_number_chk(&var2, &error);
4946 clear_tv(&var2);
4947 if (error)
4948 return FAIL;
4949#ifdef FEAT_FLOAT
4950 if (use_float)
4951 f2 = n2;
4952#endif
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 /*
4956 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959#ifdef FEAT_FLOAT
4960 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 if (op == '*')
4963 f1 = f1 * f2;
4964 else if (op == '/')
4965 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966# ifdef VMS
4967 /* VMS crashes on divide by zero, work around it */
4968 if (f2 == 0.0)
4969 {
4970 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004975 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976 }
4977 else
4978 f1 = f1 / f2;
4979# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 /* We rely on the floating point library to handle divide
4981 * by zero to result in "inf" and not a crash. */
4982 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004987 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 return FAIL;
4989 }
4990 rettv->v_type = VAR_FLOAT;
4991 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 if (op == '*')
4997 n1 = n1 * n2;
4998 else if (op == '/')
4999 {
5000 if (n2 == 0) /* give an error message? */
5001 {
5002 if (n1 == 0)
5003 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5004 else if (n1 < 0)
5005 n1 = -0x7fffffffL;
5006 else
5007 n1 = 0x7fffffffL;
5008 }
5009 else
5010 n1 = n1 / n2;
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 {
5014 if (n2 == 0) /* give an error message? */
5015 n1 = 0;
5016 else
5017 n1 = n1 % n2;
5018 }
5019 rettv->v_type = VAR_NUMBER;
5020 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 }
5024
5025 return OK;
5026}
5027
5028/*
5029 * Handle sixth level expression:
5030 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005031 * "string" string constant
5032 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * &option-name option value
5034 * @r register contents
5035 * identifier variable value
5036 * function() function call
5037 * $VAR environment variable
5038 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005039 * [expr, expr] List
5040 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *
5042 * Also handle:
5043 * ! in front logical NOT
5044 * - in front unary minus
5045 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 * trailing [] subscript in String or List
5047 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *
5049 * "arg" must point to the first non-white of the expression.
5050 * "arg" is advanced to the next non-white after the recognized expression.
5051 *
5052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055eval7(
5056 char_u **arg,
5057 typval_T *rettv,
5058 int evaluate,
5059 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 long n;
5062 int len;
5063 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *start_leader, *end_leader;
5065 int ret = OK;
5066 char_u *alias;
5067
5068 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * Skip '!' and '-' characters. They are handled later.
5076 */
5077 start_leader = *arg;
5078 while (**arg == '!' || **arg == '-' || **arg == '+')
5079 *arg = skipwhite(*arg + 1);
5080 end_leader = *arg;
5081
5082 switch (**arg)
5083 {
5084 /*
5085 * Number constant.
5086 */
5087 case '0':
5088 case '1':
5089 case '2':
5090 case '3':
5091 case '4':
5092 case '5':
5093 case '6':
5094 case '7':
5095 case '8':
5096 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 {
5098#ifdef FEAT_FLOAT
5099 char_u *p = skipdigits(*arg + 1);
5100 int get_float = FALSE;
5101
5102 /* We accept a float when the format matches
5103 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005104 * strict to avoid backwards compatibility problems.
5105 * Don't look for a float after the "." operator, so that
5106 * ":let vers = 1.2.3" doesn't fail. */
5107 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 get_float = TRUE;
5110 p = skipdigits(p + 2);
5111 if (*p == 'e' || *p == 'E')
5112 {
5113 ++p;
5114 if (*p == '-' || *p == '+')
5115 ++p;
5116 if (!vim_isdigit(*p))
5117 get_float = FALSE;
5118 else
5119 p = skipdigits(p + 1);
5120 }
5121 if (ASCII_ISALPHA(*p) || *p == '.')
5122 get_float = FALSE;
5123 }
5124 if (get_float)
5125 {
5126 float_T f;
5127
5128 *arg += string2float(*arg, &f);
5129 if (evaluate)
5130 {
5131 rettv->v_type = VAR_FLOAT;
5132 rettv->vval.v_float = f;
5133 }
5134 }
5135 else
5136#endif
5137 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005138 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 *arg += len;
5140 if (evaluate)
5141 {
5142 rettv->v_type = VAR_NUMBER;
5143 rettv->vval.v_number = n;
5144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /*
5150 * String constant: "string".
5151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 break;
5160
5161 /*
5162 * List: [expr, expr]
5163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166
5167 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 * Dictionary: {key: val, key: val}
5169 */
5170 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5171 break;
5172
5173 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005176 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Environment variable: $VAR.
5181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 /*
5186 * Register contents: @r.
5187 */
5188 case '@': ++*arg;
5189 if (evaluate)
5190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005192 rettv->vval.v_string = get_reg_contents(**arg,
5193 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 if (**arg != NUL)
5196 ++*arg;
5197 break;
5198
5199 /*
5200 * nested expression: (expression).
5201 */
5202 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (**arg == ')')
5205 ++*arg;
5206 else if (ret == OK)
5207 {
5208 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 ret = FAIL;
5211 }
5212 break;
5213
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217
5218 if (ret == NOTDONE)
5219 {
5220 /*
5221 * Must be a variable or function name.
5222 * Can also be a curly-braces kind of name: {expr}.
5223 */
5224 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 if (alias != NULL)
5227 s = alias;
5228
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 ret = FAIL;
5231 else
5232 {
5233 if (**arg == '(') /* recursive! */
5234 {
5235 /* If "s" is the name of a variable of type VAR_FUNC
5236 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005237 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 /* Invoke the function. */
5240 ret = get_func_tv(s, len, rettv, arg,
5241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005242 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243
5244 /* If evaluate is FALSE rettv->v_type was not set in
5245 * get_func_tv, but it's needed in handle_subscript() to parse
5246 * what follows. So set it here. */
5247 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5248 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005249 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250 rettv->v_type = VAR_FUNC;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /* Stop the expression evaluation when immediately
5254 * aborting on error, or when an interrupt occurred or
5255 * an exception was thrown but not caught. */
5256 if (aborting())
5257 {
5258 if (ret == OK)
5259 clear_tv(rettv);
5260 ret = FAIL;
5261 }
5262 }
5263 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005264 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 else
5266 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *arg = skipwhite(*arg);
5272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5274 * expr(expr). */
5275 if (ret == OK)
5276 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5280 */
5281 if (ret == OK && evaluate && end_leader > start_leader)
5282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 int val = 0;
5285#ifdef FEAT_FLOAT
5286 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 if (rettv->v_type == VAR_FLOAT)
5289 f = rettv->vval.v_float;
5290 else
5291#endif
5292 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 clear_tv(rettv);
5296 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 else
5299 {
5300 while (end_leader > start_leader)
5301 {
5302 --end_leader;
5303 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 {
5305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 f = !f;
5308 else
5309#endif
5310 val = !val;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = -f;
5317 else
5318#endif
5319 val = -val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 {
5325 clear_tv(rettv);
5326 rettv->vval.v_float = f;
5327 }
5328 else
5329#endif
5330 {
5331 clear_tv(rettv);
5332 rettv->v_type = VAR_NUMBER;
5333 rettv->vval.v_number = val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
5338 return ret;
5339}
5340
5341/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005342 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5343 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5345 */
5346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347eval_index(
5348 char_u **arg,
5349 typval_T *rettv,
5350 int evaluate,
5351 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352{
5353 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 long len = -1;
5357 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_FUNC:
5364 if (verbose)
5365 EMSG(_("E695: Cannot index a Funcref"));
5366 return FAIL;
5367 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 if (verbose)
5370 EMSG(_(e_float_as_string));
5371 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005372#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005374 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005375 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 if (verbose)
5377 EMSG(_("E909: Cannot index a special variable"));
5378 return FAIL;
5379 case VAR_UNKNOWN:
5380 if (evaluate)
5381 return FAIL;
5382 /* FALLTHROUGH */
5383
5384 case VAR_STRING:
5385 case VAR_NUMBER:
5386 case VAR_LIST:
5387 case VAR_DICT:
5388 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005484 case VAR_FUNC:
5485 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 case VAR_SPECIAL:
5487 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005488 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489 break; /* not evaluating, skipping over subscript */
5490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 case VAR_NUMBER:
5492 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 len = (long)STRLEN(s);
5495 if (range)
5496 {
5497 /* The resulting variable is a substring. If the indexes
5498 * are out of range the result is empty. */
5499 if (n1 < 0)
5500 {
5501 n1 = len + n1;
5502 if (n1 < 0)
5503 n1 = 0;
5504 }
5505 if (n2 < 0)
5506 n2 = len + n2;
5507 else if (n2 >= len)
5508 n2 = len;
5509 if (n1 >= len || n2 < 0 || n1 > n2)
5510 s = NULL;
5511 else
5512 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5513 }
5514 else
5515 {
5516 /* The resulting variable is a string of a single
5517 * character. If the index is too big or negative the
5518 * result is empty. */
5519 if (n1 >= len || n1 < 0)
5520 s = NULL;
5521 else
5522 s = vim_strnsave(s + n1, 1);
5523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 clear_tv(rettv);
5525 rettv->v_type = VAR_STRING;
5526 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 break;
5528
5529 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 if (n1 < 0)
5532 n1 = len + n1;
5533 if (!empty1 && (n1 < 0 || n1 >= len))
5534 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005535 /* For a range we allow invalid values and return an empty
5536 * list. A list index out of range is an error. */
5537 if (!range)
5538 {
5539 if (verbose)
5540 EMSGN(_(e_listidx), n1);
5541 return FAIL;
5542 }
5543 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 if (range)
5546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 list_T *l;
5548 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549
5550 if (n2 < 0)
5551 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005552 else if (n2 >= len)
5553 n2 = len - 1;
5554 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005555 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 l = list_alloc();
5557 if (l == NULL)
5558 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 n1 <= n2; ++n1)
5561 {
5562 if (list_append_tv(l, &item->li_tv) == FAIL)
5563 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005564 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 return FAIL;
5566 }
5567 item = item->li_next;
5568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 rettv->v_type = VAR_LIST;
5571 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005572 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 else
5575 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005576 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581
5582 case VAR_DICT:
5583 if (range)
5584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005585 if (verbose)
5586 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 if (len == -1)
5588 clear_tv(&var1);
5589 return FAIL;
5590 }
5591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593
5594 if (len == -1)
5595 {
5596 key = get_tv_string(&var1);
5597 if (*key == NUL)
5598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005599 if (verbose)
5600 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 clear_tv(&var1);
5602 return FAIL;
5603 }
5604 }
5605
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005606 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005608 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005609 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 if (len == -1)
5611 clear_tv(&var1);
5612 if (item == NULL)
5613 return FAIL;
5614
5615 copy_tv(&item->di_tv, &var1);
5616 clear_tv(rettv);
5617 *rettv = var1;
5618 }
5619 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 }
5622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 return OK;
5624}
5625
5626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Get an option value.
5628 * "arg" points to the '&' or '+' before the option name.
5629 * "arg" is advanced to character after the option name.
5630 * Return OK or FAIL.
5631 */
5632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005633get_option_tv(
5634 char_u **arg,
5635 typval_T *rettv, /* when NULL, only check if option exists */
5636 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 char_u *option_end;
5639 long numval;
5640 char_u *stringval;
5641 int opt_type;
5642 int c;
5643 int working = (**arg == '+'); /* has("+option") */
5644 int ret = OK;
5645 int opt_flags;
5646
5647 /*
5648 * Isolate the option name and find its value.
5649 */
5650 option_end = find_option_end(arg, &opt_flags);
5651 if (option_end == NULL)
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E112: Option name missing: %s"), *arg);
5655 return FAIL;
5656 }
5657
5658 if (!evaluate)
5659 {
5660 *arg = option_end;
5661 return OK;
5662 }
5663
5664 c = *option_end;
5665 *option_end = NUL;
5666 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (opt_type == -3) /* invalid name */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 EMSG2(_("E113: Unknown option: %s"), *arg);
5673 ret = FAIL;
5674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 if (opt_type == -2) /* hidden string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (opt_type == -1) /* hidden number option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_NUMBER;
5685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == 1) /* number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else /* string option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 }
5698 else if (working && (opt_type == -2 || opt_type == -1))
5699 ret = FAIL;
5700
5701 *option_end = c; /* put back for error messages */
5702 *arg = option_end;
5703
5704 return ret;
5705}
5706
5707/*
5708 * Allocate a variable for a string constant.
5709 * Return OK or FAIL.
5710 */
5711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *p;
5715 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 int extra = 0;
5717
5718 /*
5719 * Find the end of the string, skipping backslashed characters.
5720 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 if (*p == '\\' && p[1] != NUL)
5724 {
5725 ++p;
5726 /* A "\<x>" form occupies at least 4 characters, and produces up
5727 * to 6 characters: reserve space for 2 extra */
5728 if (*p == '<')
5729 extra += 2;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732
5733 if (*p != '"')
5734 {
5735 EMSG2(_("E114: Missing quote: %s"), *arg);
5736 return FAIL;
5737 }
5738
5739 /* If only parsing, set *arg and return here */
5740 if (!evaluate)
5741 {
5742 *arg = p + 1;
5743 return OK;
5744 }
5745
5746 /*
5747 * Copy the string into allocated memory, handling backslashed
5748 * characters.
5749 */
5750 name = alloc((unsigned)(p - *arg + extra));
5751 if (name == NULL)
5752 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 rettv->v_type = VAR_STRING;
5754 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (*p == '\\')
5759 {
5760 switch (*++p)
5761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 case 'b': *name++ = BS; ++p; break;
5763 case 'e': *name++ = ESC; ++p; break;
5764 case 'f': *name++ = FF; ++p; break;
5765 case 'n': *name++ = NL; ++p; break;
5766 case 'r': *name++ = CAR; ++p; break;
5767 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 case 'X': /* hex: "\x1", "\x12" */
5770 case 'x':
5771 case 'u': /* Unicode: "\u0023" */
5772 case 'U':
5773 if (vim_isxdigit(p[1]))
5774 {
5775 int n, nr;
5776 int c = toupper(*p);
5777
5778 if (c == 'X')
5779 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005782 else
5783 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 nr = 0;
5785 while (--n >= 0 && vim_isxdigit(p[1]))
5786 {
5787 ++p;
5788 nr = (nr << 4) + hex2nr(*p);
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#ifdef FEAT_MBYTE
5792 /* For "\u" store the number according to
5793 * 'encoding'. */
5794 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 else
5797#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801
5802 /* octal: "\1", "\12", "\123" */
5803 case '0':
5804 case '1':
5805 case '2':
5806 case '3':
5807 case '4':
5808 case '5':
5809 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case '7': *name = *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = (*name << 3) + *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
5815 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819
5820 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (extra != 0)
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 /* FALLTHROUGH */
5828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 }
5833 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 *arg = p + 1;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 * Return OK or FAIL.
5846 */
5847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 int reduce = 0;
5853
5854 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
5857 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 break;
5863 ++reduce;
5864 ++p;
5865 }
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 return FAIL;
5872 }
5873
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 if (!evaluate)
5876 {
5877 *arg = p + 1;
5878 return OK;
5879 }
5880
5881 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 */
5884 str = alloc((unsigned)((p - *arg) - reduce));
5885 if (str == NULL)
5886 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 rettv->v_type = VAR_STRING;
5888 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 break;
5896 ++p;
5897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 *arg = p + 1;
5902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 return OK;
5904}
5905
5906/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 * Allocate a variable for a List and fill it from "*arg".
5908 * Return OK or FAIL.
5909 */
5910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005911get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912{
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 list_T *l = NULL;
5914 typval_T tv;
5915 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916
5917 if (evaluate)
5918 {
5919 l = list_alloc();
5920 if (l == NULL)
5921 return FAIL;
5922 }
5923
5924 *arg = skipwhite(*arg + 1);
5925 while (**arg != ']' && **arg != NUL)
5926 {
5927 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5928 goto failret;
5929 if (evaluate)
5930 {
5931 item = listitem_alloc();
5932 if (item != NULL)
5933 {
5934 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 list_append(l, item);
5937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 else
5939 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941
5942 if (**arg == ']')
5943 break;
5944 if (**arg != ',')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 goto failret;
5948 }
5949 *arg = skipwhite(*arg + 1);
5950 }
5951
5952 if (**arg != ']')
5953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955failret:
5956 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005957 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 return FAIL;
5959 }
5960
5961 *arg = skipwhite(*arg + 1);
5962 if (evaluate)
5963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 rettv->v_type = VAR_LIST;
5965 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 ++l->lv_refcount;
5967 }
5968
5969 return OK;
5970}
5971
5972/*
5973 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005974 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005976 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 list_T *l;
5980
5981 l = (list_T *)alloc_clear(sizeof(list_T));
5982 if (l != NULL)
5983 {
5984 /* Prepend the list to the list of lists for garbage collection. */
5985 if (first_list != NULL)
5986 first_list->lv_used_prev = l;
5987 l->lv_used_prev = NULL;
5988 l->lv_used_next = first_list;
5989 first_list = l;
5990 }
5991 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992}
5993
5994/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005995 * Allocate an empty list for a return value.
5996 * Returns OK or FAIL.
5997 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006000{
6001 list_T *l = list_alloc();
6002
6003 if (l == NULL)
6004 return FAIL;
6005
6006 rettv->vval.v_list = l;
6007 rettv->v_type = VAR_LIST;
6008 ++l->lv_refcount;
6009 return OK;
6010}
6011
6012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 * Unreference a list: decrement the reference count and free it when it
6014 * becomes zero.
6015 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006017list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006019 if (l != NULL && --l->lv_refcount <= 0)
6020 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021}
6022
6023/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006024 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Ignores the reference count.
6026 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_free(
6029 list_T *l,
6030 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006034 /* Remove the list from the list of lists for garbage collection. */
6035 if (l->lv_used_prev == NULL)
6036 first_list = l->lv_used_next;
6037 else
6038 l->lv_used_prev->lv_used_next = l->lv_used_next;
6039 if (l->lv_used_next != NULL)
6040 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6041
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 /* Remove the item before deleting it. */
6045 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006046 if (recurse || (item->li_tv.v_type != VAR_LIST
6047 && item->li_tv.v_type != VAR_DICT))
6048 clear_tv(&item->li_tv);
6049 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 }
6051 vim_free(l);
6052}
6053
6054/*
6055 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006056 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006059listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060{
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062}
6063
6064/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006065 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 vim_free(item);
6072}
6073
6074/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 * Remove a list item from a List and free it. Also clears the value.
6076 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006080 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081 listitem_free(item);
6082}
6083
6084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 * Get the number of items in a list.
6086 */
6087 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006088list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 if (l == NULL)
6091 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 * Return TRUE when two lists have exactly the same values.
6097 */
6098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099list_equal(
6100 list_T *l1,
6101 list_T *l2,
6102 int ic, /* ignore case for strings */
6103 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (l1 == NULL || l2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (l1 == l2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (list_len(l1) != list_len(l2))
6112 return FALSE;
6113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 for (item1 = l1->lv_first, item2 = l2->lv_first;
6115 item1 != NULL && item2 != NULL;
6116 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return FALSE;
6119 return item1 == NULL && item2 == NULL;
6120}
6121
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122/*
6123 * Return the dictitem that an entry in a hashtable points to.
6124 */
6125 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127{
6128 return HI2DI(hi);
6129}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 * Return TRUE when two dictionaries have exactly the same key/values.
6133 */
6134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135dict_equal(
6136 dict_T *d1,
6137 dict_T *d2,
6138 int ic, /* ignore case for strings */
6139 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
6142 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006145 if (d1 == NULL || d2 == NULL)
6146 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006147 if (d1 == d2)
6148 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 if (dict_len(d1) != dict_len(d2))
6150 return FALSE;
6151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 if (!HASHITEM_EMPTY(hi))
6156 {
6157 item2 = dict_find(d2, hi->hi_key, -1);
6158 if (item2 == NULL)
6159 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 return FALSE;
6162 --todo;
6163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 }
6165 return TRUE;
6166}
6167
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168static int tv_equal_recurse_limit;
6169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 */
6175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176tv_equal(
6177 typval_T *tv1,
6178 typval_T *tv2,
6179 int ic, /* ignore case */
6180 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181{
6182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006185 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006190 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 * recursiveness to a limit. We guess they are equal then.
6192 * A fixed limit has the problem of still taking an awful long time.
6193 * Reduce the limit every time running into it. That should work fine for
6194 * deeply linked structures that are not recursively linked and catch
6195 * recursiveness quickly. */
6196 if (!recursive)
6197 tv_equal_recurse_limit = 1000;
6198 if (recursive_cnt >= tv_equal_recurse_limit)
6199 {
6200 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 ++recursive_cnt;
6214 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 case VAR_FUNC:
6219 return (tv1->vval.v_string != NULL
6220 && tv2->vval.v_string != NULL
6221 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6222
6223 case VAR_NUMBER:
6224 return tv1->vval.v_number == tv2->vval.v_number;
6225
6226 case VAR_STRING:
6227 s1 = get_tv_string_buf(tv1, buf1);
6228 s2 = get_tv_string_buf(tv2, buf2);
6229 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006230
6231 case VAR_SPECIAL:
6232 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006233
6234 case VAR_FLOAT:
6235#ifdef FEAT_FLOAT
6236 return tv1->vval.v_float == tv2->vval.v_float;
6237#endif
6238 case VAR_JOB:
6239#ifdef FEAT_JOB
6240 return tv1->vval.v_job == tv2->vval.v_job;
6241#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006242 case VAR_CHANNEL:
6243#ifdef FEAT_CHANNEL
6244 return tv1->vval.v_channel == tv2->vval.v_channel;
6245#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006246 case VAR_UNKNOWN:
6247 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006249
Bram Moolenaara03f2332016-02-06 18:09:59 +01006250 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6251 * does not equal anything, not even itself. */
6252 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253}
6254
6255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Locate item with index "n" in list "l" and return it.
6257 * A negative index is counted from the end; -1 is the last item.
6258 * Returns NULL when "n" is out of range.
6259 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262{
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 long idx;
6265
6266 if (l == NULL)
6267 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268
6269 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 n = l->lv_len + n;
6272
6273 /* Check for index out of range. */
6274 if (n < 0 || n >= l->lv_len)
6275 return NULL;
6276
6277 /* When there is a cached index may start search from there. */
6278 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006280 if (n < l->lv_idx / 2)
6281 {
6282 /* closest to the start of the list */
6283 item = l->lv_first;
6284 idx = 0;
6285 }
6286 else if (n > (l->lv_idx + l->lv_len) / 2)
6287 {
6288 /* closest to the end of the list */
6289 item = l->lv_last;
6290 idx = l->lv_len - 1;
6291 }
6292 else
6293 {
6294 /* closest to the cached index */
6295 item = l->lv_idx_item;
6296 idx = l->lv_idx;
6297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 }
6299 else
6300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 if (n < l->lv_len / 2)
6302 {
6303 /* closest to the start of the list */
6304 item = l->lv_first;
6305 idx = 0;
6306 }
6307 else
6308 {
6309 /* closest to the end of the list */
6310 item = l->lv_last;
6311 idx = l->lv_len - 1;
6312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314
6315 while (n > idx)
6316 {
6317 /* search forward */
6318 item = item->li_next;
6319 ++idx;
6320 }
6321 while (n < idx)
6322 {
6323 /* search backward */
6324 item = item->li_prev;
6325 --idx;
6326 }
6327
6328 /* cache the used index */
6329 l->lv_idx = idx;
6330 l->lv_idx_item = item;
6331
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 return item;
6333}
6334
6335/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006336 * Get list item "l[idx]" as a number.
6337 */
6338 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339list_find_nr(
6340 list_T *l,
6341 long idx,
6342 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx);
6347 if (li == NULL)
6348 {
6349 if (errorp != NULL)
6350 *errorp = TRUE;
6351 return -1L;
6352 }
6353 return get_tv_number_chk(&li->li_tv, errorp);
6354}
6355
6356/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006357 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6358 */
6359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006361{
6362 listitem_T *li;
6363
6364 li = list_find(l, idx - 1);
6365 if (li == NULL)
6366 {
6367 EMSGN(_(e_listidx), idx);
6368 return NULL;
6369 }
6370 return get_tv_string(&li->li_tv);
6371}
6372
6373/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374 * Locate "item" list "l" and return its index.
6375 * Returns -1 when "item" is not in the list.
6376 */
6377 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379{
6380 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382
6383 if (l == NULL)
6384 return -1;
6385 idx = 0;
6386 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6387 ++idx;
6388 if (li == NULL)
6389 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006390 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006391}
6392
6393/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 * Append item "item" to the end of list "l".
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398{
6399 if (l->lv_last == NULL)
6400 {
6401 /* empty list */
6402 l->lv_first = item;
6403 l->lv_last = item;
6404 item->li_prev = NULL;
6405 }
6406 else
6407 {
6408 l->lv_last->li_next = item;
6409 item->li_prev = l->lv_last;
6410 l->lv_last = item;
6411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 item->li_next = NULL;
6414}
6415
6416/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006421list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 copy_tv(tv, &li->li_tv);
6428 list_append(l, li);
6429 return OK;
6430}
6431
6432/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006433 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 * Return FAIL when out of memory.
6435 */
6436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438{
6439 listitem_T *li = listitem_alloc();
6440
6441 if (li == NULL)
6442 return FAIL;
6443 li->li_tv.v_type = VAR_DICT;
6444 li->li_tv.v_lock = 0;
6445 li->li_tv.vval.v_dict = dict;
6446 list_append(list, li);
6447 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 return OK;
6449}
6450
6451/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454 * Returns FAIL when out of memory.
6455 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006458{
6459 listitem_T *li = listitem_alloc();
6460
6461 if (li == NULL)
6462 return FAIL;
6463 list_append(l, li);
6464 li->li_tv.v_type = VAR_STRING;
6465 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466 if (str == NULL)
6467 li->li_tv.vval.v_string = NULL;
6468 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006469 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 return FAIL;
6471 return OK;
6472}
6473
6474/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006475 * Append "n" to list "l".
6476 * Returns FAIL when out of memory.
6477 */
6478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480{
6481 listitem_T *li;
6482
6483 li = listitem_alloc();
6484 if (li == NULL)
6485 return FAIL;
6486 li->li_tv.v_type = VAR_NUMBER;
6487 li->li_tv.v_lock = 0;
6488 li->li_tv.vval.v_number = n;
6489 list_append(l, li);
6490 return OK;
6491}
6492
6493/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 * If "item" is NULL append at the end.
6496 * Return FAIL when out of memory.
6497 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006499list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502
6503 if (ni == NULL)
6504 return FAIL;
6505 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506 list_insert(l, ni, item);
6507 return OK;
6508}
6509
6510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006512{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513 if (item == NULL)
6514 /* Append new item at end of list. */
6515 list_append(l, ni);
6516 else
6517 {
6518 /* Insert new item before existing item. */
6519 ni->li_prev = item->li_prev;
6520 ni->li_next = item;
6521 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_idx;
6525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 l->lv_idx_item = NULL;
6530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534}
6535
6536/*
6537 * Extend "l1" with "l2".
6538 * If "bef" is NULL append at the end, otherwise insert before this item.
6539 * Returns FAIL when out of memory.
6540 */
6541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543{
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006545 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006547 /* We also quit the loop when we have inserted the original item count of
6548 * the list, avoid a hang when we extend a list with itself. */
6549 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6551 return FAIL;
6552 return OK;
6553}
6554
6555/*
6556 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6557 * Return FAIL when out of memory.
6558 */
6559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561{
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006564 if (l1 == NULL || l2 == NULL)
6565 return FAIL;
6566
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 if (l == NULL)
6570 return FAIL;
6571 tv->v_type = VAR_LIST;
6572 tv->vval.v_list = l;
6573
6574 /* append all items from the second list */
6575 return list_extend(l, l2, NULL);
6576}
6577
6578/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 * Returns NULL when out of memory.
6583 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *copy;
6588 listitem_T *item;
6589 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 if (orig == NULL)
6592 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593
6594 copy = list_alloc();
6595 if (copy != NULL)
6596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (copyID != 0)
6598 {
6599 /* Do this before adding the items, because one of the items may
6600 * refer back to this list. */
6601 orig->lv_copyID = copyID;
6602 orig->lv_copylist = copy;
6603 }
6604 for (item = orig->lv_first; item != NULL && !got_int;
6605 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 {
6607 ni = listitem_alloc();
6608 if (ni == NULL)
6609 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 {
6612 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6613 {
6614 vim_free(ni);
6615 break;
6616 }
6617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 list_append(copy, ni);
6621 }
6622 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 if (item != NULL)
6624 {
6625 list_unref(copy);
6626 copy = NULL;
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
6629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 return copy;
6631}
6632
6633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006635 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006636 * This used to be called list_remove, but that conflicts with a Sun header
6637 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 /* notify watchers */
6645 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006647 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 list_fix_watch(l, ip);
6649 if (ip == item2)
6650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
6653 if (item2->li_next == NULL)
6654 l->lv_last = item->li_prev;
6655 else
6656 item2->li_next->li_prev = item->li_prev;
6657 if (item->li_prev == NULL)
6658 l->lv_first = item2->li_next;
6659 else
6660 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006661 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662}
6663
6664/*
6665 * Return an allocated string with the string representation of a list.
6666 * May return NULL.
6667 */
6668 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
6671 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (tv->vval.v_list == NULL)
6674 return NULL;
6675 ga_init2(&ga, (int)sizeof(char), 80);
6676 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 {
6679 vim_free(ga.ga_data);
6680 return NULL;
6681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 ga_append(&ga, ']');
6683 ga_append(&ga, NUL);
6684 return (char_u *)ga.ga_data;
6685}
6686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687typedef struct join_S {
6688 char_u *s;
6689 char_u *tofree;
6690} join_T;
6691
6692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693list_join_inner(
6694 garray_T *gap, /* to store the result in */
6695 list_T *l,
6696 char_u *sep,
6697 int echo_style,
6698 int copyID,
6699 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700{
6701 int i;
6702 join_T *p;
6703 int len;
6704 int sumlen = 0;
6705 int first = TRUE;
6706 char_u *tofree;
6707 char_u numbuf[NUMBUFLEN];
6708 listitem_T *item;
6709 char_u *s;
6710
6711 /* Stringify each item in the list. */
6712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6713 {
6714 if (echo_style)
6715 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6716 else
6717 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6718 if (s == NULL)
6719 return FAIL;
6720
6721 len = (int)STRLEN(s);
6722 sumlen += len;
6723
Bram Moolenaarcde88542015-08-11 19:14:00 +02006724 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6726 if (tofree != NULL || s != numbuf)
6727 {
6728 p->s = s;
6729 p->tofree = tofree;
6730 }
6731 else
6732 {
6733 p->s = vim_strnsave(s, len);
6734 p->tofree = p->s;
6735 }
6736
6737 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006738 if (did_echo_string_emsg) /* recursion error, bail out */
6739 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 }
6741
6742 /* Allocate result buffer with its total size, avoid re-allocation and
6743 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6744 if (join_gap->ga_len >= 2)
6745 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6746 if (ga_grow(gap, sumlen + 2) == FAIL)
6747 return FAIL;
6748
6749 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6750 {
6751 if (first)
6752 first = FALSE;
6753 else
6754 ga_concat(gap, sep);
6755 p = ((join_T *)join_gap->ga_data) + i;
6756
6757 if (p->s != NULL)
6758 ga_concat(gap, p->s);
6759 line_breakcheck();
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006767 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771list_join(
6772 garray_T *gap,
6773 list_T *l,
6774 char_u *sep,
6775 int echo_style,
6776 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 garray_T join_ga;
6779 int retval;
6780 join_T *p;
6781 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782
Bram Moolenaard39a7512015-04-16 22:51:22 +02006783 if (l->lv_len < 1)
6784 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6786 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6787
6788 /* Dispose each item in join_ga. */
6789 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 p = (join_T *)join_ga.ga_data;
6792 for (i = 0; i < join_ga.ga_len; ++i)
6793 {
6794 vim_free(p->tofree);
6795 ++p;
6796 }
6797 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799
6800 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801}
6802
6803/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006804 * Return the next (unique) copy ID.
6805 * Used for serializing nested structures.
6806 */
6807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006809{
6810 current_copyID += COPYID_INC;
6811 return current_copyID;
6812}
6813
6814/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 * Garbage collection for lists and dictionaries.
6816 *
6817 * We use reference counts to be able to free most items right away when they
6818 * are no longer used. But for composite items it's possible that it becomes
6819 * unused while the reference count is > 0: When there is a recursive
6820 * reference. Example:
6821 * :let l = [1, 2, 3]
6822 * :let d = {9: l}
6823 * :let l[1] = d
6824 *
6825 * Since this is quite unusual we handle this with garbage collection: every
6826 * once in a while find out which lists and dicts are not referenced from any
6827 * variable.
6828 *
6829 * Here is a good reference text about garbage collection (refers to Python
6830 * but it applies to all reference-counting mechanisms):
6831 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833
6834/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 * Do garbage collection for lists and dicts.
6836 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006841 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006842 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 buf_T *buf;
6844 win_T *wp;
6845 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006846 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006847 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006849#ifdef FEAT_WINDOWS
6850 tabpage_T *tp;
6851#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853 /* Only do this once. */
6854 want_garbage_collect = FALSE;
6855 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006856 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006857
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 /* We advance by two because we add one for items referenced through
6859 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006860 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /*
6863 * 1. Go through all accessible variables and mark all lists and dicts
6864 * with copyID.
6865 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
6867 /* Don't free variables in the previous_funccal list unless they are only
6868 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006869 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6871 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6873 NULL);
6874 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6875 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 }
6877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /* script-local variables */
6879 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
6882 /* buffer-local variables */
6883 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6885 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#ifdef FEAT_AUTOCMD
6892 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6894 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006895#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#ifdef FEAT_WINDOWS
6898 /* tabpage-local variables */
6899 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#endif
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* function-local variables */
6908 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6909 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6911 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 }
6913
Bram Moolenaard812df62008-11-09 12:46:09 +00006914 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006916
Bram Moolenaar1dced572012-04-05 16:54:08 +02006917#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006919#endif
6920
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
6925#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006927#endif
6928
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006929#ifdef FEAT_CHANNEL
6930 abort = abort || set_ref_in_channel(copyID);
6931#endif
6932
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 /*
6936 * 2. Free lists and dictionaries that are not referenced.
6937 */
6938 did_free = free_unref_items(copyID);
6939
6940 /*
6941 * 3. Check if any funccal can be freed now.
6942 */
6943 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 if (can_free_funccal(*pfc, copyID))
6946 {
6947 fc = *pfc;
6948 *pfc = fc->caller;
6949 free_funccal(fc, TRUE);
6950 did_free = TRUE;
6951 did_free_funccal = TRUE;
6952 }
6953 else
6954 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (did_free_funccal)
6957 /* When a funccal was freed some more items might be garbage
6958 * collected, so run again. */
6959 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 else if (p_verbose > 0)
6962 {
6963 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6964 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965
6966 return did_free;
6967}
6968
6969/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006970 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 */
6972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 dict_T *dd, *dd_next;
6976 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 int did_free = FALSE;
6978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 /* Free the Dictionary and ordinary items it contains, but don't
6988 * recurse into Lists and Dictionaries, they will be in the list
6989 * of dicts or list of lists. */
6990 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 dd = dd_next;
6994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of lists and free items without the copyID.
6998 * But don't free a list that has a watcher (used in a for loop), these
6999 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
7001 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 {
7003 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7005 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the List and ordinary items it contains, but don't recurse
7008 * into Lists and Dictionaries, they will be in the list of dicts
7009 * or list of lists. */
7010 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 ll = ll_next;
7014 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 return did_free;
7017}
7018
7019/*
7020 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 * "list_stack" is used to add lists to be marked. Can be NULL.
7022 *
7023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027{
7028 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 hashtab_T *cur_ht;
7032 ht_stack_T *ht_stack = NULL;
7033 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 cur_ht = ht;
7036 for (;;)
7037 {
7038 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 /* Mark each item in the hashtab. If the item contains a hashtab
7041 * it is added to ht_stack, if it contains a list it is added to
7042 * list_stack. */
7043 todo = (int)cur_ht->ht_used;
7044 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7045 if (!HASHITEM_EMPTY(hi))
7046 {
7047 --todo;
7048 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7049 &ht_stack, list_stack);
7050 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052
7053 if (ht_stack == NULL)
7054 break;
7055
7056 /* take an item from the stack */
7057 cur_ht = ht_stack->ht;
7058 tempitem = ht_stack;
7059 ht_stack = ht_stack->prev;
7060 free(tempitem);
7061 }
7062
7063 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064}
7065
7066/*
7067 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7069 *
7070 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007073set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 listitem_T *li;
7076 int abort = FALSE;
7077 list_T *cur_l;
7078 list_stack_T *list_stack = NULL;
7079 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 cur_l = l;
7082 for (;;)
7083 {
7084 if (!abort)
7085 /* Mark each item in the list. If the item contains a hashtab
7086 * it is added to ht_stack, if it contains a list it is added to
7087 * list_stack. */
7088 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7089 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7090 ht_stack, &list_stack);
7091 if (list_stack == NULL)
7092 break;
7093
7094 /* take an item from the stack */
7095 cur_l = list_stack->list;
7096 tempitem = list_stack;
7097 list_stack = list_stack->prev;
7098 free(tempitem);
7099 }
7100
7101 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102}
7103
7104/*
7105 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 * "list_stack" is used to add lists to be marked. Can be NULL.
7107 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7108 *
7109 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112set_ref_in_item(
7113 typval_T *tv,
7114 int copyID,
7115 ht_stack_T **ht_stack,
7116 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117{
7118 dict_T *dd;
7119 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007124 dd = tv->vval.v_dict;
7125 if (dd != NULL && dd->dv_copyID != copyID)
7126 {
7127 /* Didn't see this dict yet. */
7128 dd->dv_copyID = copyID;
7129 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007131 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7132 }
7133 else
7134 {
7135 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7136 if (newitem == NULL)
7137 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 else
7139 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 newitem->ht = &dd->dv_hashtab;
7141 newitem->prev = *ht_stack;
7142 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 }
7146 }
7147 else if (tv->v_type == VAR_LIST)
7148 {
7149 ll = tv->vval.v_list;
7150 if (ll != NULL && ll->lv_copyID != copyID)
7151 {
7152 /* Didn't see this list yet. */
7153 ll->lv_copyID = copyID;
7154 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 abort = set_ref_in_list(ll, copyID, ht_stack);
7157 }
7158 else
7159 {
7160 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 if (newitem == NULL)
7163 abort = TRUE;
7164 else
7165 {
7166 newitem->list = ll;
7167 newitem->prev = *list_stack;
7168 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007171 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174}
7175
7176/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 * Allocate an empty header for a dictionary.
7178 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007179 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181{
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007186 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007187 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 if (first_dict != NULL)
7189 first_dict->dv_used_prev = d;
7190 d->dv_used_next = first_dict;
7191 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007196 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201}
7202
7203/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007204 * Allocate an empty dict for a return value.
7205 * Returns OK or FAIL.
7206 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007209{
7210 dict_T *d = dict_alloc();
7211
7212 if (d == NULL)
7213 return FAIL;
7214
7215 rettv->vval.v_dict = d;
7216 rettv->v_type = VAR_DICT;
7217 ++d->dv_refcount;
7218 return OK;
7219}
7220
7221
7222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 * Unreference a Dictionary: decrement the reference count and free it when it
7224 * becomes zero.
7225 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007227dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007229 if (d != NULL && --d->dv_refcount <= 0)
7230 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
7233/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007234 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 * Ignores the reference count.
7236 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238dict_free(
7239 dict_T *d,
7240 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007242 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007246 /* Remove the dict from the list of dicts for garbage collection. */
7247 if (d->dv_used_prev == NULL)
7248 first_dict = d->dv_used_next;
7249 else
7250 d->dv_used_prev->dv_used_next = d->dv_used_next;
7251 if (d->dv_used_next != NULL)
7252 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7253
7254 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007256 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 if (!HASHITEM_EMPTY(hi))
7260 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007261 /* Remove the item before deleting it, just in case there is
7262 * something recursive causing trouble. */
7263 di = HI2DI(hi);
7264 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 if (recurse || (di->di_tv.v_type != VAR_LIST
7266 && di->di_tv.v_type != VAR_DICT))
7267 clear_tv(&di->di_tv);
7268 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 --todo;
7270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 vim_free(d);
7274}
7275
7276/*
7277 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 * The "key" is copied to the new item.
7279 * Note that the value of the item "di_tv" still needs to be initialized!
7280 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007282 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284{
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007291 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007292 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294}
7295
7296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 * Make a copy of a Dictionary item.
7298 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7305 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (di != NULL)
7307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007309 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 copy_tv(&org->di_tv, &di->di_tv);
7311 }
7312 return di;
7313}
7314
7315/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 * Remove item "item" from Dictionary "dict" and free it.
7317 */
7318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320{
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (HASHITEM_EMPTY(hi))
7325 EMSG2(_(e_intern2), "dictitem_remove()");
7326 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 dictitem_free(item);
7329}
7330
7331/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 * Free a dict item. Also clears the value.
7333 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 if (item->di_flags & DI_FLAGS_ALLOC)
7339 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7344 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 * Returns NULL when out of memory.
7347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350{
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 dict_T *copy;
7352 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355
7356 if (orig == NULL)
7357 return NULL;
7358
7359 copy = dict_alloc();
7360 if (copy != NULL)
7361 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 if (copyID != 0)
7363 {
7364 orig->dv_copyID = copyID;
7365 orig->dv_copydict = copy;
7366 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 --todo;
7373
7374 di = dictitem_alloc(hi->hi_key);
7375 if (di == NULL)
7376 break;
7377 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 {
7379 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7380 copyID) == FAIL)
7381 {
7382 vim_free(di);
7383 break;
7384 }
7385 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 else
7387 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7388 if (dict_add(copy, di) == FAIL)
7389 {
7390 dictitem_free(di);
7391 break;
7392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 if (todo > 0)
7398 {
7399 dict_unref(copy);
7400 copy = NULL;
7401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 }
7403
7404 return copy;
7405}
7406
7407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007409 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413{
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415}
7416
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007418 * Add a number or string entry to dictionary "d".
7419 * When "str" is NULL use number "nr", otherwise use "str".
7420 * Returns FAIL when out of memory and when key already exists.
7421 */
7422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423dict_add_nr_str(
7424 dict_T *d,
7425 char *key,
7426 long nr,
7427 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007428{
7429 dictitem_T *item;
7430
7431 item = dictitem_alloc((char_u *)key);
7432 if (item == NULL)
7433 return FAIL;
7434 item->di_tv.v_lock = 0;
7435 if (str == NULL)
7436 {
7437 item->di_tv.v_type = VAR_NUMBER;
7438 item->di_tv.vval.v_number = nr;
7439 }
7440 else
7441 {
7442 item->di_tv.v_type = VAR_STRING;
7443 item->di_tv.vval.v_string = vim_strsave(str);
7444 }
7445 if (dict_add(d, item) == FAIL)
7446 {
7447 dictitem_free(item);
7448 return FAIL;
7449 }
7450 return OK;
7451}
7452
7453/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007454 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007455 * Returns FAIL when out of memory and when key already exists.
7456 */
7457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007459{
7460 dictitem_T *item;
7461
7462 item = dictitem_alloc((char_u *)key);
7463 if (item == NULL)
7464 return FAIL;
7465 item->di_tv.v_lock = 0;
7466 item->di_tv.v_type = VAR_LIST;
7467 item->di_tv.vval.v_list = list;
7468 if (dict_add(d, item) == FAIL)
7469 {
7470 dictitem_free(item);
7471 return FAIL;
7472 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007473 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007474 return OK;
7475}
7476
7477/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 * Get the number of items in a Dictionary.
7479 */
7480 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 if (d == NULL)
7484 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007485 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486}
7487
7488/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Find item "key[len]" in Dictionary "d".
7490 * If "len" is negative use strlen(key).
7491 * Returns NULL when not found.
7492 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007493 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496#define AKEYLEN 200
7497 char_u buf[AKEYLEN];
7498 char_u *akey;
7499 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 if (len < 0)
7503 akey = key;
7504 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 tofree = akey = vim_strnsave(key, len);
7507 if (akey == NULL)
7508 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007509 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 else
7511 {
7512 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007513 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 akey = buf;
7515 }
7516
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 vim_free(tofree);
7519 if (HASHITEM_EMPTY(hi))
7520 return NULL;
7521 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522}
7523
7524/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 * Get a string item from a dictionary.
7526 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527 * Returns NULL if the entry doesn't exist or out of memory.
7528 */
7529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531{
7532 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007533 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534
7535 di = dict_find(d, key, -1);
7536 if (di == NULL)
7537 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007538 s = get_tv_string(&di->di_tv);
7539 if (save && s != NULL)
7540 s = vim_strsave(s);
7541 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542}
7543
7544/*
7545 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007546 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547 */
7548 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550{
7551 dictitem_T *di;
7552
7553 di = dict_find(d, key, -1);
7554 if (di == NULL)
7555 return 0;
7556 return get_tv_number(&di->di_tv);
7557}
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Return an allocated string with the string representation of a Dictionary.
7561 * May return NULL.
7562 */
7563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565{
7566 garray_T ga;
7567 int first = TRUE;
7568 char_u *tofree;
7569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 return NULL;
7577 ga_init2(&ga, (int)sizeof(char), 80);
7578 ga_append(&ga, '{');
7579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 --todo;
7586
7587 if (first)
7588 first = FALSE;
7589 else
7590 ga_concat(&ga, (char_u *)", ");
7591
7592 tofree = string_quote(hi->hi_key, FALSE);
7593 if (tofree != NULL)
7594 {
7595 ga_concat(&ga, tofree);
7596 vim_free(tofree);
7597 }
7598 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 if (s != NULL)
7601 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007603 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007604 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007605 line_breakcheck();
7606
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 if (todo > 0)
7610 {
7611 vim_free(ga.ga_data);
7612 return NULL;
7613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614
7615 ga_append(&ga, '}');
7616 ga_append(&ga, NUL);
7617 return (char_u *)ga.ga_data;
7618}
7619
7620/*
7621 * Allocate a variable for a Dictionary and fill it from "*arg".
7622 * Return OK or FAIL. Returns NOTDONE for {expr}.
7623 */
7624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626{
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d = NULL;
7628 typval_T tvkey;
7629 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007630 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634
7635 /*
7636 * First check if it's not a curly-braces thing: {expr}.
7637 * Must do this without evaluating, otherwise a function may be called
7638 * twice. Unfortunately this means we need to call eval1() twice for the
7639 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 if (*start != '}')
7643 {
7644 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7645 return FAIL;
7646 if (*start == '}')
7647 return NOTDONE;
7648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649
7650 if (evaluate)
7651 {
7652 d = dict_alloc();
7653 if (d == NULL)
7654 return FAIL;
7655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 tvkey.v_type = VAR_UNKNOWN;
7657 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 *arg = skipwhite(*arg + 1);
7660 while (**arg != '}' && **arg != NUL)
7661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 goto failret;
7664 if (**arg != ':')
7665 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007666 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007670 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007672 key = get_tv_string_buf_chk(&tvkey, buf);
7673 if (key == NULL || *key == NUL)
7674 {
7675 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7676 if (key != NULL)
7677 EMSG(_(e_emptykey));
7678 clear_tv(&tvkey);
7679 goto failret;
7680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 *arg = skipwhite(*arg + 1);
7684 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7685 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007686 if (evaluate)
7687 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 goto failret;
7689 }
7690 if (evaluate)
7691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 if (item != NULL)
7694 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007695 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 clear_tv(&tv);
7698 goto failret;
7699 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 item = dictitem_alloc(key);
7701 clear_tv(&tvkey);
7702 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007705 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 if (dict_add(d, item) == FAIL)
7707 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 }
7709 }
7710
7711 if (**arg == '}')
7712 break;
7713 if (**arg != ',')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 goto failret;
7717 }
7718 *arg = skipwhite(*arg + 1);
7719 }
7720
7721 if (**arg != '}')
7722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724failret:
7725 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007726 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 return FAIL;
7728 }
7729
7730 *arg = skipwhite(*arg + 1);
7731 if (evaluate)
7732 {
7733 rettv->v_type = VAR_DICT;
7734 rettv->vval.v_dict = d;
7735 ++d->dv_refcount;
7736 }
7737
7738 return OK;
7739}
7740
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007741#if defined(FEAT_CHANNEL) || defined(PROTO)
7742/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007743 * Decrement the reference count on "channel" and maybe free it when it goes
7744 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007745 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746 */
7747 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007748channel_unref(channel_T *channel)
7749{
7750 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007751 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007753}
7754#endif
7755
Bram Moolenaar65edff82016-02-21 16:40:11 +01007756#if defined(FEAT_JOB) || defined(PROTO)
7757static job_T *first_job = NULL;
7758
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 static void
7760job_free(job_T *job)
7761{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007762# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007763 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 if (job->jv_channel != NULL)
7765 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007766 /* The link from the channel to the job doesn't count as a reference,
7767 * thus don't decrement the refcount of the job. The reference from
7768 * the job to the channel does count the refrence, decrement it and
7769 * NULL the reference. We don't set ch_job_killed, unreferencing the
7770 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 job->jv_channel->ch_job = NULL;
7772 channel_unref(job->jv_channel);
7773 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007774# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007775 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007776
7777 if (job->jv_next != NULL)
7778 job->jv_next->jv_prev = job->jv_prev;
7779 if (job->jv_prev == NULL)
7780 first_job = job->jv_next;
7781 else
7782 job->jv_prev->jv_next = job->jv_next;
7783
7784 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007785 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 vim_free(job);
7787}
7788
7789 static void
7790job_unref(job_T *job)
7791{
7792 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007793 {
7794 /* Do not free the job when it has not ended yet and there is a
7795 * "stoponexit" flag or an exit callback. */
7796 if (job->jv_status != JOB_STARTED
7797 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007798 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007799 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007800 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007801# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007802 else if (job->jv_channel != NULL)
7803 {
7804 /* Do remove the link to the channel, otherwise it hangs
7805 * around until Vim exits. See job_free() for refcount. */
7806 job->jv_channel->ch_job = NULL;
7807 channel_unref(job->jv_channel);
7808 job->jv_channel = NULL;
7809 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007810# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007811 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812}
7813
7814/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007815 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816 */
7817 static job_T *
7818job_alloc(void)
7819{
7820 job_T *job;
7821
7822 job = (job_T *)alloc_clear(sizeof(job_T));
7823 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 job->jv_stoponexit = vim_strsave((char_u *)"term");
7827
7828 if (first_job != NULL)
7829 {
7830 first_job->jv_prev = job;
7831 job->jv_next = first_job;
7832 }
7833 first_job = job;
7834 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007835 return job;
7836}
7837
Bram Moolenaar65edff82016-02-21 16:40:11 +01007838 static void
7839job_set_options(job_T *job, jobopt_T *opt)
7840{
7841 if (opt->jo_set & JO_STOPONEXIT)
7842 {
7843 vim_free(job->jv_stoponexit);
7844 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7845 job->jv_stoponexit = NULL;
7846 else
7847 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7848 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007849 if (opt->jo_set & JO_EXIT_CB)
7850 {
7851 vim_free(job->jv_exit_cb);
7852 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7853 job->jv_exit_cb = NULL;
7854 else
7855 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7856 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007857}
7858
7859/*
7860 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7861 */
7862 void
7863job_stop_on_exit()
7864{
7865 job_T *job;
7866
7867 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007868 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007869 mch_stop_job(job, job->jv_stoponexit);
7870}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007871#endif
7872
Bram Moolenaar17a13432016-01-24 14:22:10 +01007873 static char *
7874get_var_special_name(int nr)
7875{
7876 switch (nr)
7877 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007878 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007879 case VVAL_TRUE: return "v:true";
7880 case VVAL_NONE: return "v:none";
7881 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007882 }
7883 EMSG2(_(e_intern2), "get_var_special_name()");
7884 return "42";
7885}
7886
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 * Return a string with the string representation of a variable.
7889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007893 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 */
7895 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896echo_string(
7897 typval_T *tv,
7898 char_u **tofree,
7899 char_u *numbuf,
7900 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 static int recurse = 0;
7903 char_u *r = NULL;
7904
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007907 if (!did_echo_string_emsg)
7908 {
7909 /* Only give this message once for a recursive call to avoid
7910 * flooding the user with errors. And stop iterating over lists
7911 * and dicts. */
7912 did_echo_string_emsg = TRUE;
7913 EMSG(_("E724: variable nested too deep for displaying"));
7914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007916 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 }
7918 ++recurse;
7919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 switch (tv->v_type)
7921 {
7922 case VAR_FUNC:
7923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 r = tv->vval.v_string;
7925 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928 if (tv->vval.v_list == NULL)
7929 {
7930 *tofree = NULL;
7931 r = NULL;
7932 }
7933 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7934 {
7935 *tofree = NULL;
7936 r = (char_u *)"[...]";
7937 }
7938 else
7939 {
7940 tv->vval.v_list->lv_copyID = copyID;
7941 *tofree = list2string(tv, copyID);
7942 r = *tofree;
7943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007944 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007945
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947 if (tv->vval.v_dict == NULL)
7948 {
7949 *tofree = NULL;
7950 r = NULL;
7951 }
7952 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7953 {
7954 *tofree = NULL;
7955 r = (char_u *)"{...}";
7956 }
7957 else
7958 {
7959 tv->vval.v_dict->dv_copyID = copyID;
7960 *tofree = dict2string(tv, copyID);
7961 r = *tofree;
7962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 case VAR_STRING:
7966 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007967 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007969 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 *tofree = NULL;
7971 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976 *tofree = NULL;
7977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7978 r = numbuf;
7979 break;
7980#endif
7981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 case VAR_SPECIAL:
7983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987
Bram Moolenaar8502c702014-06-17 12:51:16 +02007988 if (--recurse == 0)
7989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991}
7992
7993/*
7994 * Return a string with the string representation of a variable.
7995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7996 * "numbuf" is used for a number.
7997 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007998 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 */
8000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001tv2string(
8002 typval_T *tv,
8003 char_u **tofree,
8004 char_u *numbuf,
8005 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006{
8007 switch (tv->v_type)
8008 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 case VAR_FUNC:
8010 *tofree = string_quote(tv->vval.v_string, TRUE);
8011 return *tofree;
8012 case VAR_STRING:
8013 *tofree = string_quote(tv->vval.v_string, FALSE);
8014 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008016#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017 *tofree = NULL;
8018 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8019 return numbuf;
8020#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008022 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031}
8032
8033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 * Return string "str" in ' quotes, doubling ' characters.
8035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 */
8038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040{
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 char_u *p, *r, *s;
8043
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 len = (function ? 13 : 3);
8045 if (str != NULL)
8046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 for (p = str; *p != NUL; mb_ptr_adv(p))
8049 if (*p == '\'')
8050 ++len;
8051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 s = r = alloc(len);
8053 if (r != NULL)
8054 {
8055 if (function)
8056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 r += 10;
8059 }
8060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 if (str != NULL)
8063 for (p = str; *p != NUL; )
8064 {
8065 if (*p == '\'')
8066 *r++ = '\'';
8067 MB_COPY_CHAR(p, r);
8068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070 if (function)
8071 *r++ = ')';
8072 *r++ = NUL;
8073 }
8074 return s;
8075}
8076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078/*
8079 * Convert the string "text" to a floating point number.
8080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8081 * this always uses a decimal point.
8082 * Returns the length of the text that was consumed.
8083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085string2float(
8086 char_u *text,
8087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088{
8089 char *s = (char *)text;
8090 float_T f;
8091
8092 f = strtod(s, &s);
8093 *value = f;
8094 return (int)((char_u *)s - text);
8095}
8096#endif
8097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Get the value of an environment variable.
8100 * "arg" is pointing to the '$'. It is advanced to after the name.
8101 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 */
8104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008105get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 char_u *string = NULL;
8108 int len;
8109 int cc;
8110 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008111 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 ++*arg;
8114 name = *arg;
8115 len = get_env_len(arg);
8116 if (evaluate)
8117 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008119 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008120
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008121 cc = name[len];
8122 name[len] = NUL;
8123 /* first try vim_getenv(), fast for normal environment vars */
8124 string = vim_getenv(name, &mustfree);
8125 if (string != NULL && *string != NUL)
8126 {
8127 if (!mustfree)
8128 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008130 else
8131 {
8132 if (mustfree)
8133 vim_free(string);
8134
8135 /* next try expanding things like $VIM and ${HOME} */
8136 string = expand_env_save(name - 1);
8137 if (string != NULL && *string == '$')
8138 {
8139 vim_free(string);
8140 string = NULL;
8141 }
8142 }
8143 name[len] = cc;
8144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->v_type = VAR_STRING;
8146 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148
8149 return OK;
8150}
8151
8152/*
8153 * Array with names and number of arguments of all internal functions
8154 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8155 */
8156static struct fst
8157{
8158 char *f_name; /* function name */
8159 char f_min_argc; /* minimal number of arguments */
8160 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008161 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008162 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163} functions[] =
8164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008170 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"append", 2, 2, f_append},
8173 {"argc", 0, 0, f_argc},
8174 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008175 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008176 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008177#ifdef FEAT_FLOAT
8178 {"asin", 1, 1, f_asin}, /* WJMc */
8179#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008181 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008182 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_false", 1, 2, f_assert_false},
8184 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008187 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008190 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"bufexists", 1, 1, f_bufexists},
8192 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8193 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8194 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8195 {"buflisted", 1, 1, f_buflisted},
8196 {"bufloaded", 1, 1, f_bufloaded},
8197 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008198 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"bufwinnr", 1, 1, f_bufwinnr},
8200 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008201 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008202 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204#ifdef FEAT_FLOAT
8205 {"ceil", 1, 1, f_ceil},
8206#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207#ifdef FEAT_CHANNEL
8208 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008209 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8210 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008211 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008212# ifdef FEAT_JOB
8213 {"ch_getjob", 1, 1, f_ch_getjob},
8214# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008215 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008216 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008217 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008218 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008219 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8221 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008222 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008226 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008228 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232 {"complete_add", 1, 1, f_complete_add},
8233 {"complete_check", 0, 0, f_complete_check},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008243 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008245 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 {"diff_filler", 1, 1, f_diff_filler},
8248 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008249 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008250 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"eventhandler", 0, 0, f_eventhandler},
8254 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008255 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257#ifdef FEAT_FLOAT
8258 {"exp", 1, 1, f_exp},
8259#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008260 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008261 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008262 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8264 {"filereadable", 1, 1, f_filereadable},
8265 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008267 {"finddir", 1, 3, f_finddir},
8268 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"float2nr", 1, 1, f_float2nr},
8271 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008274 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"fnamemodify", 2, 2, f_fnamemodify},
8276 {"foldclosed", 1, 1, f_foldclosed},
8277 {"foldclosedend", 1, 1, f_foldclosedend},
8278 {"foldlevel", 1, 1, f_foldlevel},
8279 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008280 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008283 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008285 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008286 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getchar", 0, 1, f_getchar},
8288 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008289 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getcmdline", 0, 0, f_getcmdline},
8291 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008292 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008293 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008294 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008295 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008297 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"getfsize", 1, 1, f_getfsize},
8299 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008300 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008302 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008303 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008304 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008305 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008307 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"gettabvar", 2, 3, f_gettabvar},
8310 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getwinposx", 0, 0, f_getwinposx},
8312 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008315 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008316 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008318 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008320 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8322 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8323 {"histadd", 2, 2, f_histadd},
8324 {"histdel", 1, 2, f_histdel},
8325 {"histget", 1, 2, f_histget},
8326 {"histnr", 1, 1, f_histnr},
8327 {"hlID", 1, 1, f_hlID},
8328 {"hlexists", 1, 1, f_hlexists},
8329 {"hostname", 0, 0, f_hostname},
8330 {"iconv", 3, 3, f_iconv},
8331 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008333 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008335 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"inputrestore", 0, 0, f_inputrestore},
8337 {"inputsave", 0, 0, f_inputsave},
8338 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008340 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008343#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8344 {"isnan", 1, 1, f_isnan},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008347#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008348# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008349 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008350# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008351 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008352 {"job_start", 1, 2, f_job_start},
8353 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008354 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008355#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008357 {"js_decode", 1, 1, f_js_decode},
8358 {"js_encode", 1, 1, f_js_encode},
8359 {"json_decode", 1, 1, f_json_decode},
8360 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"libcall", 3, 3, f_libcall},
8365 {"libcallnr", 3, 3, f_libcallnr},
8366 {"line", 1, 1, f_line},
8367 {"line2byte", 1, 1, f_line2byte},
8368 {"lispindent", 1, 1, f_lispindent},
8369 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372 {"log10", 1, 1, f_log10},
8373#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008374#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008375 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008376#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008377 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008378 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008379 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008380 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008381 {"matchadd", 2, 5, f_matchadd},
8382 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008384 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008385 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008387 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008388 {"max", 1, 1, f_max},
8389 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008390#ifdef vim_mkdir
8391 {"mkdir", 1, 3, f_mkdir},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008394#ifdef FEAT_MZSCHEME
8395 {"mzeval", 1, 1, f_mzeval},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008398 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008401#ifdef FEAT_PERL
8402 {"perleval", 1, 1, f_perleval},
8403#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"pow", 2, 2, f_pow},
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008410#ifdef FEAT_PYTHON3
8411 {"py3eval", 1, 1, f_py3eval},
8412#endif
8413#ifdef FEAT_PYTHON
8414 {"pyeval", 1, 1, f_pyeval},
8415#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008417 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008418 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008419#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008420 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008421#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008422 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"remote_expr", 2, 3, f_remote_expr},
8424 {"remote_foreground", 1, 1, f_remote_foreground},
8425 {"remote_peek", 1, 2, f_remote_peek},
8426 {"remote_read", 1, 1, f_remote_read},
8427 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008430 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008432 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"round", 1, 1, f_round},
8435#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008436 {"screenattr", 2, 2, f_screenattr},
8437 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008438 {"screencol", 0, 0, f_screencol},
8439 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008441 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008442 {"searchpair", 3, 7, f_searchpair},
8443 {"searchpairpos", 3, 7, f_searchpairpos},
8444 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"server2client", 2, 2, f_server2client},
8446 {"serverlist", 0, 0, f_serverlist},
8447 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008448 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008450 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008452 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008453 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008454 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008455 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008457 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008458 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008460#ifdef FEAT_CRYPT
8461 {"sha256", 1, 1, f_sha256},
8462#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008463 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008464 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#ifdef FEAT_FLOAT
8467 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008468 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008469#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008470 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008471 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008472 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008473 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008474 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008475#ifdef FEAT_FLOAT
8476 {"sqrt", 1, 1, f_sqrt},
8477 {"str2float", 1, 1, f_str2float},
8478#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008479 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008480 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008481 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#ifdef HAVE_STRFTIME
8483 {"strftime", 1, 2, f_strftime},
8484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"strlen", 1, 1, f_strlen},
8488 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008489 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008491 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008492 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"substitute", 4, 4, f_substitute},
8494 {"synID", 3, 3, f_synID},
8495 {"synIDattr", 2, 3, f_synIDattr},
8496 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008497 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008498 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008499 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008500 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008502 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008503 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008504 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008505 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506#ifdef FEAT_FLOAT
8507 {"tan", 1, 1, f_tan},
8508 {"tanh", 1, 1, f_tanh},
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008511 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"tolower", 1, 1, f_tolower},
8513 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008514 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008515#ifdef FEAT_FLOAT
8516 {"trunc", 1, 1, f_trunc},
8517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008519 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008520 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008521 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008522 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"virtcol", 1, 1, f_virtcol},
8524 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008525 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"winbufnr", 1, 1, f_winbufnr},
8527 {"wincol", 0, 0, f_wincol},
8528 {"winheight", 1, 1, f_winheight},
8529 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008530 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008532 {"winrestview", 1, 1, f_winrestview},
8533 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008535 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008536 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008537 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538};
8539
8540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8541
8542/*
8543 * Function given to ExpandGeneric() to obtain the list of internal
8544 * or user defined function names.
8545 */
8546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 static int intidx = -1;
8550 char_u *name;
8551
8552 if (idx == 0)
8553 intidx = -1;
8554 if (intidx < 0)
8555 {
8556 name = get_user_func_name(xp, idx);
8557 if (name != NULL)
8558 return name;
8559 }
8560 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8561 {
8562 STRCPY(IObuff, functions[intidx].f_name);
8563 STRCAT(IObuff, "(");
8564 if (functions[intidx].f_max_argc == 0)
8565 STRCAT(IObuff, ")");
8566 return IObuff;
8567 }
8568
8569 return NULL;
8570}
8571
8572/*
8573 * Function given to ExpandGeneric() to obtain the list of internal or
8574 * user defined variable or function names.
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008577get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 static int intidx = -1;
8580 char_u *name;
8581
8582 if (idx == 0)
8583 intidx = -1;
8584 if (intidx < 0)
8585 {
8586 name = get_function_name(xp, idx);
8587 if (name != NULL)
8588 return name;
8589 }
8590 return get_user_var_name(xp, ++intidx);
8591}
8592
8593#endif /* FEAT_CMDL_COMPL */
8594
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595#if defined(EBCDIC) || defined(PROTO)
8596/*
8597 * Compare struct fst by function name.
8598 */
8599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 struct fst *p1 = (struct fst *)s1;
8603 struct fst *p2 = (struct fst *)s2;
8604
8605 return STRCMP(p1->f_name, p2->f_name);
8606}
8607
8608/*
8609 * Sort the function table by function name.
8610 * The sorting of the table above is ASCII dependant.
8611 * On machines using EBCDIC we have to sort it.
8612 */
8613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008615{
8616 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8617
8618 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8619}
8620#endif
8621
8622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623/*
8624 * Find internal function in table above.
8625 * Return index, or -1 if not found
8626 */
8627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008628find_internal_func(
8629 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int first = 0;
8632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8633 int cmp;
8634 int x;
8635
8636 /*
8637 * Find the function name in the table. Binary search.
8638 */
8639 while (first <= last)
8640 {
8641 x = first + ((unsigned)(last - first) >> 1);
8642 cmp = STRCMP(name, functions[x].f_name);
8643 if (cmp < 0)
8644 last = x - 1;
8645 else if (cmp > 0)
8646 first = x + 1;
8647 else
8648 return x;
8649 }
8650 return -1;
8651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8655 * name it contains, otherwise return "name".
8656 */
8657 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008658deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659{
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 int cc;
8662
8663 cc = name[*lenp];
8664 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008665 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 {
8671 *lenp = 0;
8672 return (char_u *)""; /* just in case */
8673 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008674 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 }
8677
8678 return name;
8679}
8680
8681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 * Allocate a variable for the result of a function.
8683 * Return OK or FAIL.
8684 */
8685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686get_func_tv(
8687 char_u *name, /* name of the function */
8688 int len, /* length of "name" */
8689 typval_T *rettv,
8690 char_u **arg, /* argument, pointing to the '(' */
8691 linenr_T firstline, /* first line of range */
8692 linenr_T lastline, /* last line of range */
8693 int *doesrange, /* return: function handled range */
8694 int evaluate,
8695 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 char_u *argp;
8698 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008699 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int argcount = 0; /* number of arguments found */
8701
8702 /*
8703 * Get the arguments.
8704 */
8705 argp = *arg;
8706 while (argcount < MAX_FUNC_ARGS)
8707 {
8708 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8709 if (*argp == ')' || *argp == ',' || *argp == NUL)
8710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8712 {
8713 ret = FAIL;
8714 break;
8715 }
8716 ++argcount;
8717 if (*argp != ',')
8718 break;
8719 }
8720 if (*argp == ')')
8721 ++argp;
8722 else
8723 ret = FAIL;
8724
8725 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 {
8730 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008731 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008733 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 *arg = skipwhite(argp);
8740 return ret;
8741}
8742
8743
8744/*
8745 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008746 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008747 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750call_func(
8751 char_u *funcname, /* name of the function */
8752 int len, /* length of "name" */
8753 typval_T *rettv, /* return value goes here */
8754 int argcount, /* number of "argvars" */
8755 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008756 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008757 linenr_T firstline, /* first line of range */
8758 linenr_T lastline, /* last line of range */
8759 int *doesrange, /* return: function handled range */
8760 int evaluate,
8761 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764#define ERROR_UNKNOWN 0
8765#define ERROR_TOOMANY 1
8766#define ERROR_TOOFEW 2
8767#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768#define ERROR_DICT 4
8769#define ERROR_NONE 5
8770#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 int error = ERROR_NONE;
8772 int i;
8773 int llen;
8774 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775#define FLEN_FIXED 40
8776 char_u fname_buf[FLEN_FIXED + 1];
8777 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008778 char_u *name;
8779
8780 /* Make a copy of the name, if it comes from a funcref variable it could
8781 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008782 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008783 if (name == NULL)
8784 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 /*
8787 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8788 * Change <SNR>123_name() to K_SNR 123_name().
8789 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 llen = eval_fname_script(name);
8792 if (llen > 0)
8793 {
8794 fname_buf[0] = K_SPECIAL;
8795 fname_buf[1] = KS_EXTRA;
8796 fname_buf[2] = (int)KE_SNR;
8797 i = 3;
8798 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8799 {
8800 if (current_SID <= 0)
8801 error = ERROR_SCRIPT;
8802 else
8803 {
8804 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8805 i = (int)STRLEN(fname_buf);
8806 }
8807 }
8808 if (i + STRLEN(name + llen) < FLEN_FIXED)
8809 {
8810 STRCPY(fname_buf + i, name + llen);
8811 fname = fname_buf;
8812 }
8813 else
8814 {
8815 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8816 if (fname == NULL)
8817 error = ERROR_OTHER;
8818 else
8819 {
8820 mch_memmove(fname, fname_buf, (size_t)i);
8821 STRCPY(fname + i, name + llen);
8822 }
8823 }
8824 }
8825 else
8826 fname = name;
8827
8828 *doesrange = FALSE;
8829
8830
8831 /* execute the function if no errors detected and executing */
8832 if (evaluate && error == ERROR_NONE)
8833 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008834 char_u *rfname = fname;
8835
8836 /* Ignore "g:" before a function name. */
8837 if (fname[0] == 'g' && fname[1] == ':')
8838 rfname = fname + 2;
8839
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008840 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8841 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 error = ERROR_UNKNOWN;
8843
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008844 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 /*
8847 * User defined function.
8848 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 /* Trigger FuncUndefined event, may load the function. */
8853 if (fp == NULL
8854 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008855 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008858 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008859 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
8861#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008862 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008863 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008864 {
8865 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008866 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008867 }
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (fp != NULL)
8870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008875 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008877 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 else
8880 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008881 int did_save_redo = FALSE;
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 /*
8884 * Call the user function.
8885 * Save and restore search patterns, script variables and
8886 * redo buffer.
8887 */
8888 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008889#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008891#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008892 {
8893 saveRedobuff();
8894 did_save_redo = TRUE;
8895 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008896 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008899 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8900 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8901 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008902 /* Function was unreferenced while being used, free it
8903 * now. */
8904 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008905 if (did_save_redo)
8906 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 restore_search_patterns();
8908 error = ERROR_NONE;
8909 }
8910 }
8911 }
8912 else
8913 {
8914 /*
8915 * Find the function name in the table, call its implementation.
8916 */
8917 i = find_internal_func(fname);
8918 if (i >= 0)
8919 {
8920 if (argcount < functions[i].f_min_argc)
8921 error = ERROR_TOOFEW;
8922 else if (argcount > functions[i].f_max_argc)
8923 error = ERROR_TOOMANY;
8924 else
8925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008926 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 error = ERROR_NONE;
8929 }
8930 }
8931 }
8932 /*
8933 * The function call (or "FuncUndefined" autocommand sequence) might
8934 * have been aborted by an error, an interrupt, or an explicitly thrown
8935 * exception that has not been caught so far. This situation can be
8936 * tested for by calling aborting(). For an error in an internal
8937 * function or for the "E132" error in call_user_func(), however, the
8938 * throw point at which the "force_abort" flag (temporarily reset by
8939 * emsg()) is normally updated has not been reached yet. We need to
8940 * update that flag first to make aborting() reliable.
8941 */
8942 update_force_abort();
8943 }
8944 if (error == ERROR_NONE)
8945 ret = OK;
8946
8947 /*
8948 * Report an error unless the argument evaluation or function call has been
8949 * cancelled due to an aborting error, an interrupt, or an exception.
8950 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008951 if (!aborting())
8952 {
8953 switch (error)
8954 {
8955 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008957 break;
8958 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008959 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008960 break;
8961 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008962 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008963 name);
8964 break;
8965 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008966 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008967 name);
8968 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008970 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 name);
8972 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008973 }
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (fname != name && fname != fname_buf)
8977 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008978 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
8980 return ret;
8981}
8982
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983/*
8984 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008985 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008989{
8990 char_u *p;
8991
8992 if (*name == K_SPECIAL)
8993 p = concat_str((char_u *)"<SNR>", name + 3);
8994 else
8995 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008996 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008997 if (p != name)
8998 vim_free(p);
8999}
9000
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009001/*
9002 * Return TRUE for a non-zero Number and a non-empty String.
9003 */
9004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009005non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009006{
9007 return ((argvars[0].v_type == VAR_NUMBER
9008 && argvars[0].vval.v_number != 0)
9009 || (argvars[0].v_type == VAR_STRING
9010 && argvars[0].vval.v_string != NULL
9011 && *argvars[0].vval.v_string != NUL));
9012}
9013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014/*********************************************
9015 * Implementation of the built-in functions
9016 */
9017
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009018#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009019static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009020
9021/*
9022 * Get the float value of "argvars[0]" into "f".
9023 * Returns FAIL when the argument is not a Number or Float.
9024 */
9025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009026get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009027{
9028 if (argvars[0].v_type == VAR_FLOAT)
9029 {
9030 *f = argvars[0].vval.v_float;
9031 return OK;
9032 }
9033 if (argvars[0].v_type == VAR_NUMBER)
9034 {
9035 *f = (float_T)argvars[0].vval.v_number;
9036 return OK;
9037 }
9038 EMSG(_("E808: Number or Float required"));
9039 return FAIL;
9040}
9041
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009042/*
9043 * "abs(expr)" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009047{
9048 if (argvars[0].v_type == VAR_FLOAT)
9049 {
9050 rettv->v_type = VAR_FLOAT;
9051 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9052 }
9053 else
9054 {
9055 varnumber_T n;
9056 int error = FALSE;
9057
9058 n = get_tv_number_chk(&argvars[0], &error);
9059 if (error)
9060 rettv->vval.v_number = -1;
9061 else if (n > 0)
9062 rettv->vval.v_number = n;
9063 else
9064 rettv->vval.v_number = -n;
9065 }
9066}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009067
9068/*
9069 * "acos()" function
9070 */
9071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009072f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009074 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009075
9076 rettv->v_type = VAR_FLOAT;
9077 if (get_float_arg(argvars, &f) == OK)
9078 rettv->vval.v_float = acos(f);
9079 else
9080 rettv->vval.v_float = 0.0;
9081}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082#endif
9083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009085 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 */
9087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009088f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009095 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009096 && !tv_check_lock(l->lv_lock,
9097 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009098 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009100 }
9101 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102 EMSG(_(e_listreq));
9103}
9104
9105/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009106 * "alloc_fail(id, countdown, repeat)" function
9107 */
9108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009110{
9111 if (argvars[0].v_type != VAR_NUMBER
9112 || argvars[0].vval.v_number <= 0
9113 || argvars[1].v_type != VAR_NUMBER
9114 || argvars[1].vval.v_number < 0
9115 || argvars[2].v_type != VAR_NUMBER)
9116 EMSG(_(e_invarg));
9117 else
9118 {
9119 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009120 if (alloc_fail_id >= aid_last)
9121 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009122 alloc_fail_countdown = argvars[1].vval.v_number;
9123 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009124 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009125 }
9126}
9127
9128/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009129 * "and(expr, expr)" function
9130 */
9131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009132f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009133{
9134 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9135 & get_tv_number_chk(&argvars[1], NULL);
9136}
9137
9138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139 * "append(lnum, string/list)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143{
9144 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 list_T *l = NULL;
9147 listitem_T *li = NULL;
9148 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 long added = 0;
9150
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009151 /* When coming here from Insert mode, sync undo, so that this can be
9152 * undone separately from what was previously inserted. */
9153 if (u_sync_once == 2)
9154 {
9155 u_sync_once = 1; /* notify that u_sync() was called */
9156 u_sync(TRUE);
9157 }
9158
Bram Moolenaar0d660222005-01-07 21:51:51 +00009159 lnum = get_tv_lnum(argvars);
9160 if (lnum >= 0
9161 && lnum <= curbuf->b_ml.ml_line_count
9162 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166 l = argvars[1].vval.v_list;
9167 if (l == NULL)
9168 return;
9169 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 for (;;)
9172 {
9173 if (l == NULL)
9174 tv = &argvars[1]; /* append a string */
9175 else if (li == NULL)
9176 break; /* end of list */
9177 else
9178 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 line = get_tv_string_chk(tv);
9180 if (line == NULL) /* type error */
9181 {
9182 rettv->vval.v_number = 1; /* Failed */
9183 break;
9184 }
9185 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186 ++added;
9187 if (l == NULL)
9188 break;
9189 li = li->li_next;
9190 }
9191
9192 appended_lines_mark(lnum, added);
9193 if (curwin->w_cursor.lnum > lnum)
9194 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 else
9197 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198}
9199
9200/*
9201 * "argc()" function
9202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * "argidx()" function
9211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009219 * "arglistid()" function
9220 */
9221 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009222f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009223{
9224 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009225
9226 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009227 wp = find_tabwin(&argvars[0], &argvars[1]);
9228 if (wp != NULL)
9229 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009230}
9231
9232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 * "argv(nr)" function
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 int idx;
9239
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009240 if (argvars[0].v_type != VAR_UNKNOWN)
9241 {
9242 idx = get_tv_number_chk(&argvars[0], NULL);
9243 if (idx >= 0 && idx < ARGCOUNT)
9244 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9245 else
9246 rettv->vval.v_string = NULL;
9247 rettv->v_type = VAR_STRING;
9248 }
9249 else if (rettv_list_alloc(rettv) == OK)
9250 for (idx = 0; idx < ARGCOUNT; ++idx)
9251 list_append_string(rettv->vval.v_list,
9252 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253}
9254
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009255static void prepare_assert_error(garray_T*gap);
9256static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9257static void assert_error(garray_T *gap);
9258static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259
9260/*
9261 * Prepare "gap" for an assert error and add the sourcing position.
9262 */
9263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009264prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265{
9266 char buf[NUMBUFLEN];
9267
9268 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009269 if (sourcing_name != NULL)
9270 {
9271 ga_concat(gap, sourcing_name);
9272 if (sourcing_lnum > 0)
9273 ga_concat(gap, (char_u *)" ");
9274 }
9275 if (sourcing_lnum > 0)
9276 {
9277 sprintf(buf, "line %ld", (long)sourcing_lnum);
9278 ga_concat(gap, (char_u *)buf);
9279 }
9280 if (sourcing_name != NULL || sourcing_lnum > 0)
9281 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009282}
9283
9284/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009285 * Append "str" to "gap", escaping unprintable characters.
9286 * Changes NL to \n, CR to \r, etc.
9287 */
9288 static void
9289ga_concat_esc(garray_T *gap, char_u *str)
9290{
9291 char_u *p;
9292 char_u buf[NUMBUFLEN];
9293
9294 for (p = str; *p != NUL; ++p)
9295 switch (*p)
9296 {
9297 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9298 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9299 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9300 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9301 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9302 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9303 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9304 default:
9305 if (*p < ' ')
9306 {
9307 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9308 ga_concat(gap, buf);
9309 }
9310 else
9311 ga_append(gap, *p);
9312 break;
9313 }
9314}
9315
9316/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009317 * Fill "gap" with information about an assert error.
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320fill_assert_error(
9321 garray_T *gap,
9322 typval_T *opt_msg_tv,
9323 char_u *exp_str,
9324 typval_T *exp_tv,
9325 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009326{
9327 char_u numbuf[NUMBUFLEN];
9328 char_u *tofree;
9329
9330 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9331 {
9332 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9333 vim_free(tofree);
9334 }
9335 else
9336 {
9337 ga_concat(gap, (char_u *)"Expected ");
9338 if (exp_str == NULL)
9339 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009340 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 vim_free(tofree);
9342 }
9343 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009344 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009345 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009346 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009347 vim_free(tofree);
9348 }
9349}
Bram Moolenaar43345542015-11-29 17:35:35 +01009350
9351/*
9352 * Add an assert error to v:errors.
9353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 struct vimvar *vp = &vimvars[VV_ERRORS];
9358
9359 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9360 /* Make sure v:errors is a list. */
9361 set_vim_var_list(VV_ERRORS, list_alloc());
9362 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9363}
9364
Bram Moolenaar43345542015-11-29 17:35:35 +01009365/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 */
9368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009369f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009370{
9371 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009372
9373 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9377 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009378 ga_clear(&ga);
9379 }
9380}
9381
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009382/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009383 * "assert_exception(string[, msg])" function
9384 */
9385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009386f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387{
9388 garray_T ga;
9389 char *error;
9390
9391 error = (char *)get_tv_string_chk(&argvars[0]);
9392 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9393 {
9394 prepare_assert_error(&ga);
9395 ga_concat(&ga, (char_u *)"v:exception is not set");
9396 assert_error(&ga);
9397 ga_clear(&ga);
9398 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009399 else if (error != NULL
9400 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009401 {
9402 prepare_assert_error(&ga);
9403 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9404 &vimvars[VV_EXCEPTION].vv_tv);
9405 assert_error(&ga);
9406 ga_clear(&ga);
9407 }
9408}
9409
9410/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009411 * "assert_fails(cmd [, error])" function
9412 */
9413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009414f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009415{
9416 char_u *cmd = get_tv_string_chk(&argvars[0]);
9417 garray_T ga;
9418
9419 called_emsg = FALSE;
9420 suppress_errthrow = TRUE;
9421 emsg_silent = TRUE;
9422 do_cmdline_cmd(cmd);
9423 if (!called_emsg)
9424 {
9425 prepare_assert_error(&ga);
9426 ga_concat(&ga, (char_u *)"command did not fail: ");
9427 ga_concat(&ga, cmd);
9428 assert_error(&ga);
9429 ga_clear(&ga);
9430 }
9431 else if (argvars[1].v_type != VAR_UNKNOWN)
9432 {
9433 char_u buf[NUMBUFLEN];
9434 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9435
9436 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9437 {
9438 prepare_assert_error(&ga);
9439 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9440 &vimvars[VV_ERRMSG].vv_tv);
9441 assert_error(&ga);
9442 ga_clear(&ga);
9443 }
9444 }
9445
9446 called_emsg = FALSE;
9447 suppress_errthrow = FALSE;
9448 emsg_silent = FALSE;
9449 emsg_on_display = FALSE;
9450 set_vim_var_string(VV_ERRMSG, NULL, 0);
9451}
9452
9453/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 * Common for assert_true() and assert_false().
9455 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009458{
9459 int error = FALSE;
9460 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009461
Bram Moolenaar37127922016-02-06 20:29:28 +01009462 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009463 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009464 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 if (argvars[0].v_type != VAR_NUMBER
9466 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9467 || error)
9468 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 prepare_assert_error(&ga);
9470 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009471 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009472 NULL, &argvars[0]);
9473 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 ga_clear(&ga);
9475 }
9476}
9477
9478/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009480 */
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009483{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009484 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009485}
9486
9487/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009488 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009492{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009493 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009494}
9495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009497/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009498 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009503 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = asin(f);
9508 else
9509 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009510}
9511
9512/*
9513 * "atan()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009517{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009518 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &f) == OK)
9522 rettv->vval.v_float = atan(f);
9523 else
9524 rettv->vval.v_float = 0.0;
9525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009526
9527/*
9528 * "atan2()" function
9529 */
9530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009531f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009532{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009533 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009534
9535 rettv->v_type = VAR_FLOAT;
9536 if (get_float_arg(argvars, &fx) == OK
9537 && get_float_arg(&argvars[1], &fy) == OK)
9538 rettv->vval.v_float = atan2(fx, fy);
9539 else
9540 rettv->vval.v_float = 0.0;
9541}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009542#endif
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544/*
9545 * "browse(save, title, initdir, default)" function
9546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009548f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550#ifdef FEAT_BROWSE
9551 int save;
9552 char_u *title;
9553 char_u *initdir;
9554 char_u *defname;
9555 char_u buf[NUMBUFLEN];
9556 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 save = get_tv_number_chk(&argvars[0], &error);
9560 title = get_tv_string_chk(&argvars[1]);
9561 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9562 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (error || title == NULL || initdir == NULL || defname == NULL)
9565 rettv->vval.v_string = NULL;
9566 else
9567 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 do_browse(save ? BROWSE_SAVE : 0,
9569 title, defname, NULL, initdir, NULL, curbuf);
9570#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574}
9575
9576/*
9577 * "browsedir(title, initdir)" function
9578 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009581{
9582#ifdef FEAT_BROWSE
9583 char_u *title;
9584 char_u *initdir;
9585 char_u buf[NUMBUFLEN];
9586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 title = get_tv_string_chk(&argvars[0]);
9588 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 if (title == NULL || initdir == NULL)
9591 rettv->vval.v_string = NULL;
9592 else
9593 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009594 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599}
9600
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009601static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009602
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603/*
9604 * Find a buffer by number or exact name.
9605 */
9606 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009611 if (avar->v_type == VAR_NUMBER)
9612 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009613 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009616 if (buf == NULL)
9617 {
9618 /* No full path name match, try a match with a URL or a "nofile"
9619 * buffer, these don't use the full path. */
9620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9621 if (buf->b_fname != NULL
9622 && (path_with_url(buf->b_fname)
9623#ifdef FEAT_QUICKFIX
9624 || bt_nofile(buf)
9625#endif
9626 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009627 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009628 break;
9629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 }
9631 return buf;
9632}
9633
9634/*
9635 * "bufexists(expr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
9644 * "buflisted(expr)" function
9645 */
9646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 buf_T *buf;
9650
9651 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653}
9654
9655/*
9656 * "bufloaded(expr)" function
9657 */
9658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
9661 buf_T *buf;
9662
9663 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 static buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009668buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 int save_magic;
9671 char_u *save_cpo;
9672 buf_T *buf;
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9675 save_magic = p_magic;
9676 p_magic = TRUE;
9677 save_cpo = p_cpo;
9678 p_cpo = (char_u *)"";
9679
9680 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009681 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 p_magic = save_magic;
9684 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009685 return buf;
9686}
9687
9688/*
9689 * Get buffer by number or pattern.
9690 */
9691 static buf_T *
9692get_buf_tv(typval_T *tv, int curtab_only)
9693{
9694 char_u *name = tv->vval.v_string;
9695 buf_T *buf;
9696
9697 if (tv->v_type == VAR_NUMBER)
9698 return buflist_findnr((int)tv->vval.v_number);
9699 if (tv->v_type != VAR_STRING)
9700 return NULL;
9701 if (name == NULL || *name == NUL)
9702 return curbuf;
9703 if (name[0] == '$' && name[1] == NUL)
9704 return lastbuf;
9705
9706 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 /* If not found, try expanding the name, like done for bufexists(). */
9709 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712 return buf;
9713}
9714
9715/*
9716 * "bufname(expr)" function
9717 */
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 buf_T *buf;
9722
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 Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 --emsg_off;
9732}
9733
9734/*
9735 * "bufnr(expr)" function
9736 */
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009741 int error = FALSE;
9742 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009746 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009747 --emsg_off;
9748
9749 /* If the buffer isn't found and the second argument is not zero create a
9750 * new buffer. */
9751 if (buf == NULL
9752 && argvars[1].v_type != VAR_UNKNOWN
9753 && get_tv_number_chk(&argvars[1], &error) != 0
9754 && !error
9755 && (name = get_tv_string_chk(&argvars[0])) != NULL
9756 && !error)
9757 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9758
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
9766 * "bufwinnr(nr)" function
9767 */
9768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771#ifdef FEAT_WINDOWS
9772 win_T *wp;
9773 int winnr = 0;
9774#endif
9775 buf_T *buf;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009779 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef FEAT_WINDOWS
9781 for (wp = firstwin; wp; wp = wp->w_next)
9782 {
9783 ++winnr;
9784 if (wp->w_buffer == buf)
9785 break;
9786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790#endif
9791 --emsg_off;
9792}
9793
9794/*
9795 * "byte2line(byte)" function
9796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#else
9803 long boff = 0;
9804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 (linenr_T)0, &boff);
9811#endif
9812}
9813
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009816{
9817#ifdef FEAT_MBYTE
9818 char_u *t;
9819#endif
9820 char_u *str;
9821 long idx;
9822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 str = get_tv_string_chk(&argvars[0]);
9824 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009827 return;
9828
9829#ifdef FEAT_MBYTE
9830 t = str;
9831 for ( ; idx > 0; idx--)
9832 {
9833 if (*t == NUL) /* EOL reached */
9834 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009835 if (enc_utf8 && comp)
9836 t += utf_ptr2len(t);
9837 else
9838 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009839 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009840 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009841#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009844#endif
9845}
9846
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009847/*
9848 * "byteidx()" function
9849 */
9850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009852{
9853 byteidx(argvars, rettv, FALSE);
9854}
9855
9856/*
9857 * "byteidxcomp()" function
9858 */
9859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009861{
9862 byteidx(argvars, rettv, TRUE);
9863}
9864
Bram Moolenaardb913952012-06-29 12:54:53 +02009865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866func_call(
9867 char_u *name,
9868 typval_T *args,
9869 dict_T *selfdict,
9870 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009871{
9872 listitem_T *item;
9873 typval_T argv[MAX_FUNC_ARGS + 1];
9874 int argc = 0;
9875 int dummy;
9876 int r = 0;
9877
9878 for (item = args->vval.v_list->lv_first; item != NULL;
9879 item = item->li_next)
9880 {
9881 if (argc == MAX_FUNC_ARGS)
9882 {
9883 EMSG(_("E699: Too many arguments"));
9884 break;
9885 }
9886 /* Make a copy of each argument. This is needed to be able to set
9887 * v_lock to VAR_FIXED in the copy without changing the original list.
9888 */
9889 copy_tv(&item->li_tv, &argv[argc++]);
9890 }
9891
9892 if (item == NULL)
9893 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9894 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9895 &dummy, TRUE, selfdict);
9896
9897 /* Free the arguments. */
9898 while (argc > 0)
9899 clear_tv(&argv[--argc]);
9900
9901 return r;
9902}
9903
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905 * "call(func, arglist)" function
9906 */
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909{
9910 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009912
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 if (argvars[1].v_type != VAR_LIST)
9914 {
9915 EMSG(_(e_listreq));
9916 return;
9917 }
9918 if (argvars[1].vval.v_list == NULL)
9919 return;
9920
9921 if (argvars[0].v_type == VAR_FUNC)
9922 func = argvars[0].vval.v_string;
9923 else
9924 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (*func == NUL)
9926 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009927
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (argvars[2].v_type != VAR_UNKNOWN)
9929 {
9930 if (argvars[2].v_type != VAR_DICT)
9931 {
9932 EMSG(_(e_dictreq));
9933 return;
9934 }
9935 selfdict = argvars[2].vval.v_dict;
9936 }
9937
Bram Moolenaardb913952012-06-29 12:54:53 +02009938 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009939}
9940
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941#ifdef FEAT_FLOAT
9942/*
9943 * "ceil({float})" function
9944 */
9945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009946f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009948 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009949
9950 rettv->v_type = VAR_FLOAT;
9951 if (get_float_arg(argvars, &f) == OK)
9952 rettv->vval.v_float = ceil(f);
9953 else
9954 rettv->vval.v_float = 0.0;
9955}
9956#endif
9957
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959/*
9960 * Get a callback from "arg". It can be a Funcref or a function name.
9961 * When "arg" is zero return an empty string.
9962 * Return NULL for an invalid argument.
9963 */
9964 static char_u *
9965get_callback(typval_T *arg)
9966{
9967 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9968 return arg->vval.v_string;
9969 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9970 return (char_u *)"";
9971 EMSG(_("E999: Invalid callback argument"));
9972 return NULL;
9973}
9974
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009975 static int
9976handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9977{
9978 char_u *val = get_tv_string(item);
9979
9980 opt->jo_set |= jo;
9981 if (STRCMP(val, "nl") == 0)
9982 *modep = MODE_NL;
9983 else if (STRCMP(val, "raw") == 0)
9984 *modep = MODE_RAW;
9985 else if (STRCMP(val, "js") == 0)
9986 *modep = MODE_JS;
9987 else if (STRCMP(val, "json") == 0)
9988 *modep = MODE_JSON;
9989 else
9990 {
9991 EMSG2(_(e_invarg2), val);
9992 return FAIL;
9993 }
9994 return OK;
9995}
9996
Bram Moolenaar187db502016-02-27 14:44:26 +01009997 static int
9998handle_io(typval_T *item, int part, jobopt_T *opt)
9999{
10000 char_u *val = get_tv_string(item);
10001
10002 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
10003 if (STRCMP(val, "null") == 0)
10004 opt->jo_io[part] = JIO_NULL;
10005 else if (STRCMP(val, "pipe") == 0)
10006 opt->jo_io[part] = JIO_PIPE;
10007 else if (STRCMP(val, "file") == 0)
10008 opt->jo_io[part] = JIO_FILE;
10009 else if (STRCMP(val, "buffer") == 0)
10010 opt->jo_io[part] = JIO_BUFFER;
10011 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10012 opt->jo_io[part] = JIO_OUT;
10013 else
10014 {
10015 EMSG2(_(e_invarg2), val);
10016 return FAIL;
10017 }
10018 return OK;
10019}
10020
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010021 static void
10022clear_job_options(jobopt_T *opt)
10023{
10024 vim_memset(opt, 0, sizeof(jobopt_T));
10025}
10026
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010027/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010028 * Get the PART_ number from the first character of an option name.
10029 */
10030 static int
10031part_from_char(int c)
10032{
10033 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10034}
10035
10036/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010037 * Get the option entries from the dict in "tv", parse them and put the result
10038 * in "opt".
10039 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010040 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010041 */
10042 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010045 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010046 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010047 dict_T *dict;
10048 int todo;
10049 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010050 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010051
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010052 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010053 if (tv->v_type == VAR_UNKNOWN)
10054 return OK;
10055 if (tv->v_type != VAR_DICT)
10056 {
10057 EMSG(_(e_invarg));
10058 return FAIL;
10059 }
10060 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010061 if (dict == NULL)
10062 return OK;
10063
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010064 todo = (int)dict->dv_hashtab.ht_used;
10065 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10066 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010067 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010068 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010069
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010070 if (STRCMP(hi->hi_key, "mode") == 0)
10071 {
10072 if (!(supported & JO_MODE))
10073 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010074 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010075 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010076 }
10077 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10078 {
10079 if (!(supported & JO_IN_MODE))
10080 break;
10081 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10082 == FAIL)
10083 return FAIL;
10084 }
10085 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10086 {
10087 if (!(supported & JO_OUT_MODE))
10088 break;
10089 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10090 == FAIL)
10091 return FAIL;
10092 }
10093 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10094 {
10095 if (!(supported & JO_ERR_MODE))
10096 break;
10097 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10098 == FAIL)
10099 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010101 else if (STRCMP(hi->hi_key, "in-io") == 0
10102 || STRCMP(hi->hi_key, "out-io") == 0
10103 || STRCMP(hi->hi_key, "err-io") == 0)
10104 {
10105 if (!(supported & JO_OUT_IO))
10106 break;
10107 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10108 return FAIL;
10109 }
10110 else if (STRCMP(hi->hi_key, "in-name") == 0
10111 || STRCMP(hi->hi_key, "out-name") == 0
10112 || STRCMP(hi->hi_key, "err-name") == 0)
10113 {
10114 part = part_from_char(*hi->hi_key);
10115
10116 if (!(supported & JO_OUT_IO))
10117 break;
10118 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10119 opt->jo_io_name[part] =
10120 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10121 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010010122 else if (STRCMP(hi->hi_key, "in-buf") == 0
10123 || STRCMP(hi->hi_key, "out-buf") == 0
10124 || STRCMP(hi->hi_key, "err-buf") == 0)
10125 {
10126 part = part_from_char(*hi->hi_key);
10127
10128 if (!(supported & JO_OUT_IO))
10129 break;
10130 opt->jo_set |= JO_OUT_BUF << (part - PART_OUT);
10131 opt->jo_io_buf[part] = get_tv_number(item);
10132 if (opt->jo_io_buf[part] <= 0)
10133 {
10134 EMSG2(_(e_invarg2), get_tv_string(item));
10135 return FAIL;
10136 }
10137 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
10138 {
10139 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
10140 return FAIL;
10141 }
10142 }
Bram Moolenaar014069a2016-03-03 22:51:40 +010010143 else if (STRCMP(hi->hi_key, "in-top") == 0
10144 || STRCMP(hi->hi_key, "in-bot") == 0)
10145 {
10146 linenr_T *lp;
10147
10148 if (!(supported & JO_OUT_IO))
10149 break;
10150 if (hi->hi_key[3] == 't')
10151 {
10152 lp = &opt->jo_in_top;
10153 opt->jo_set |= JO_IN_TOP;
10154 }
10155 else
10156 {
10157 lp = &opt->jo_in_bot;
10158 opt->jo_set |= JO_IN_BOT;
10159 }
10160 *lp = get_tv_number(item);
10161 if (*lp < 0)
10162 {
10163 EMSG2(_(e_invarg2), get_tv_string(item));
10164 return FAIL;
10165 }
10166 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010167 else if (STRCMP(hi->hi_key, "callback") == 0)
10168 {
10169 if (!(supported & JO_CALLBACK))
10170 break;
10171 opt->jo_set |= JO_CALLBACK;
10172 opt->jo_callback = get_callback(item);
10173 if (opt->jo_callback == NULL)
10174 {
10175 EMSG2(_(e_invarg2), "callback");
10176 return FAIL;
10177 }
10178 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010179 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10180 {
10181 if (!(supported & JO_OUT_CALLBACK))
10182 break;
10183 opt->jo_set |= JO_OUT_CALLBACK;
10184 opt->jo_out_cb = get_callback(item);
10185 if (opt->jo_out_cb == NULL)
10186 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010187 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010188 return FAIL;
10189 }
10190 }
10191 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10192 {
10193 if (!(supported & JO_ERR_CALLBACK))
10194 break;
10195 opt->jo_set |= JO_ERR_CALLBACK;
10196 opt->jo_err_cb = get_callback(item);
10197 if (opt->jo_err_cb == NULL)
10198 {
10199 EMSG2(_(e_invarg2), "err-cb");
10200 return FAIL;
10201 }
10202 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010203 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10204 {
10205 if (!(supported & JO_CLOSE_CALLBACK))
10206 break;
10207 opt->jo_set |= JO_CLOSE_CALLBACK;
10208 opt->jo_close_cb = get_callback(item);
10209 if (opt->jo_close_cb == NULL)
10210 {
10211 EMSG2(_(e_invarg2), "close-cb");
10212 return FAIL;
10213 }
10214 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010215 else if (STRCMP(hi->hi_key, "waittime") == 0)
10216 {
10217 if (!(supported & JO_WAITTIME))
10218 break;
10219 opt->jo_set |= JO_WAITTIME;
10220 opt->jo_waittime = get_tv_number(item);
10221 }
10222 else if (STRCMP(hi->hi_key, "timeout") == 0)
10223 {
10224 if (!(supported & JO_TIMEOUT))
10225 break;
10226 opt->jo_set |= JO_TIMEOUT;
10227 opt->jo_timeout = get_tv_number(item);
10228 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010229 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10230 {
10231 if (!(supported & JO_OUT_TIMEOUT))
10232 break;
10233 opt->jo_set |= JO_OUT_TIMEOUT;
10234 opt->jo_out_timeout = get_tv_number(item);
10235 }
10236 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10237 {
10238 if (!(supported & JO_ERR_TIMEOUT))
10239 break;
10240 opt->jo_set |= JO_ERR_TIMEOUT;
10241 opt->jo_err_timeout = get_tv_number(item);
10242 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010243 else if (STRCMP(hi->hi_key, "part") == 0)
10244 {
10245 if (!(supported & JO_PART))
10246 break;
10247 opt->jo_set |= JO_PART;
10248 val = get_tv_string(item);
10249 if (STRCMP(val, "err") == 0)
10250 opt->jo_part = PART_ERR;
10251 else
10252 {
10253 EMSG2(_(e_invarg2), val);
10254 return FAIL;
10255 }
10256 }
10257 else if (STRCMP(hi->hi_key, "id") == 0)
10258 {
10259 if (!(supported & JO_ID))
10260 break;
10261 opt->jo_set |= JO_ID;
10262 opt->jo_id = get_tv_number(item);
10263 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010264 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10265 {
10266 if (!(supported & JO_STOPONEXIT))
10267 break;
10268 opt->jo_set |= JO_STOPONEXIT;
10269 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10270 opt->jo_soe_buf);
10271 if (opt->jo_stoponexit == NULL)
10272 {
10273 EMSG2(_(e_invarg2), "stoponexit");
10274 return FAIL;
10275 }
10276 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010277 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10278 {
10279 if (!(supported & JO_EXIT_CB))
10280 break;
10281 opt->jo_set |= JO_EXIT_CB;
10282 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010283 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010284 {
10285 EMSG2(_(e_invarg2), "exit-cb");
10286 return FAIL;
10287 }
10288 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010289 else
10290 break;
10291 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010292 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010293 if (todo > 0)
10294 {
10295 EMSG2(_(e_invarg2), hi->hi_key);
10296 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010297 }
10298
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010299 return OK;
10300}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010301#endif
10302
10303#ifdef FEAT_CHANNEL
10304/*
10305 * Get the channel from the argument.
10306 * Returns NULL if the handle is invalid.
10307 */
10308 static channel_T *
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010309get_channel_arg(typval_T *tv, int check_open)
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010310{
Bram Moolenaar151f6562016-03-07 21:19:38 +010010311 channel_T *channel = NULL;
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312
Bram Moolenaar151f6562016-03-07 21:19:38 +010010313 if (tv->v_type == VAR_JOB)
10314 {
10315 if (tv->vval.v_job != NULL)
10316 channel = tv->vval.v_job->jv_channel;
10317 }
10318 else if (tv->v_type == VAR_CHANNEL)
10319 {
10320 channel = tv->vval.v_channel;
10321 }
10322 else
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010323 {
10324 EMSG2(_(e_invarg2), get_tv_string(tv));
10325 return NULL;
10326 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010327
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010328 if (check_open && (channel == NULL || !channel_is_open(channel)))
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010329 {
10330 EMSG(_("E906: not an open channel"));
10331 return NULL;
10332 }
10333 return channel;
10334}
10335
10336/*
10337 * "ch_close()" function
10338 */
10339 static void
10340f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10341{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010342 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010343
10344 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010345 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010346 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010347 channel_clear(channel);
10348 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010349}
10350
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010351/*
10352 * "ch_getbufnr()" function
10353 */
10354 static void
10355f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10356{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010357 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010358
10359 rettv->vval.v_number = -1;
10360 if (channel != NULL)
10361 {
10362 char_u *what = get_tv_string(&argvars[1]);
10363 int part;
10364
10365 if (STRCMP(what, "err") == 0)
10366 part = PART_ERR;
10367 else if (STRCMP(what, "out") == 0)
10368 part = PART_OUT;
10369 else if (STRCMP(what, "in") == 0)
10370 part = PART_IN;
10371 else
10372 part = PART_SOCK;
10373 if (channel->ch_part[part].ch_buffer != NULL)
10374 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10375 }
10376}
10377
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010378# ifdef FEAT_JOB
10379/*
10380 * "ch_getjob()" function
10381 */
10382 static void
10383f_ch_getjob(typval_T *argvars, typval_T *rettv)
10384{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010385 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010386
10387 if (channel != NULL)
10388 {
10389 rettv->v_type = VAR_JOB;
10390 rettv->vval.v_job = channel->ch_job;
10391 if (channel->ch_job != NULL)
10392 ++channel->ch_job->jv_refcount;
10393 }
10394}
10395# endif
10396
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010397/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010398 * "ch_log()" function
10399 */
10400 static void
10401f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10402{
10403 char_u *msg = get_tv_string(&argvars[0]);
10404 channel_T *channel = NULL;
10405
10406 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010407 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010408
10409 ch_log(channel, (char *)msg);
10410}
10411
10412/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010413 * "ch_logfile()" function
10414 */
10415 static void
10416f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10417{
10418 char_u *fname;
10419 char_u *opt = (char_u *)"";
10420 char_u buf[NUMBUFLEN];
10421 FILE *file = NULL;
10422
10423 fname = get_tv_string(&argvars[0]);
10424 if (argvars[1].v_type == VAR_STRING)
10425 opt = get_tv_string_buf(&argvars[1], buf);
10426 if (*fname != NUL)
10427 {
10428 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10429 if (file == NULL)
10430 {
10431 EMSG2(_(e_notopen), fname);
10432 return;
10433 }
10434 }
10435 ch_logfile(file);
10436}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010437
10438/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010439 * "ch_open()" function
10440 */
10441 static void
10442f_ch_open(typval_T *argvars, typval_T *rettv)
10443{
10444 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010445 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010446 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010447 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010448 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010449 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010450
10451 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010452 rettv->v_type = VAR_CHANNEL;
10453 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010454
10455 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010456 if (argvars[1].v_type != VAR_UNKNOWN
10457 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010458 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010459 EMSG(_(e_invarg));
10460 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010461 }
10462
10463 /* parse address */
10464 p = vim_strchr(address, ':');
10465 if (p == NULL)
10466 {
10467 EMSG2(_(e_invarg2), address);
10468 return;
10469 }
10470 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010471 port = strtol((char *)p, &rest, 10);
10472 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010473 {
10474 p[-1] = ':';
10475 EMSG2(_(e_invarg2), address);
10476 return;
10477 }
10478
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010479 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010480 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010481 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010482 opt.jo_timeout = 2000;
10483 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010484 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010485 return;
10486 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010487 {
10488 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010489 return;
10490 }
10491
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010493 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010494 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010495 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496 opt.jo_set = JO_ALL;
10497 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010498 }
10499}
10500
10501/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010502 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010503 */
10504 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010505common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010506{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010507 channel_T *channel;
10508 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010509 jobopt_T opt;
10510 int mode;
10511 int timeout;
10512 int id = -1;
10513 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010514
10515 /* return an empty string by default */
10516 rettv->v_type = VAR_STRING;
10517 rettv->vval.v_string = NULL;
10518
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010519 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010520 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10521 == FAIL)
10522 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010523
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010524 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010525 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010526 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010527 if (opt.jo_set & JO_PART)
10528 part = opt.jo_part;
10529 else
10530 part = channel_part_read(channel);
10531 mode = channel_get_mode(channel, part);
10532 timeout = channel_get_timeout(channel, part);
10533 if (opt.jo_set & JO_TIMEOUT)
10534 timeout = opt.jo_timeout;
10535
10536 if (raw || mode == MODE_RAW || mode == MODE_NL)
10537 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10538 else
10539 {
10540 if (opt.jo_set & JO_ID)
10541 id = opt.jo_id;
10542 channel_read_json_block(channel, part, timeout, id, &listtv);
10543 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010544 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010545 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010546 vim_free(listtv);
10547 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010548 else
10549 {
10550 rettv->v_type = VAR_SPECIAL;
10551 rettv->vval.v_number = VVAL_NONE;
10552 }
10553 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010554 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010555}
10556
10557/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010558 * "ch_read()" function
10559 */
10560 static void
10561f_ch_read(typval_T *argvars, typval_T *rettv)
10562{
10563 common_channel_read(argvars, rettv, FALSE);
10564}
10565
10566/*
10567 * "ch_readraw()" function
10568 */
10569 static void
10570f_ch_readraw(typval_T *argvars, typval_T *rettv)
10571{
10572 common_channel_read(argvars, rettv, TRUE);
10573}
10574
10575/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010576 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010577 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010578 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010579 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010580 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010581 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010582send_common(
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010583 typval_T *argvars,
10584 char_u *text,
10585 int id,
10586 int eval,
10587 jobopt_T *opt,
10588 char *fun,
10589 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010590{
Bram Moolenaar77073442016-02-13 23:23:53 +010010591 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010592 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010593
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010594 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010595 if (channel == NULL)
10596 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010597 part_send = channel_part_send(channel);
10598 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010599
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010600 clear_job_options(opt);
10601 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010602 return NULL;
10603
Bram Moolenaara07fec92016-02-05 21:04:08 +010010604 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010605 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10606 * allowed. */
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010607 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010608 {
10609 if (eval)
10610 {
10611 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10612 return NULL;
10613 }
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010614 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010615 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010616
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010617 if (channel_send(channel, part_send, text, fun) == OK
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010618 && opt->jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010619 return channel;
10620 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010621}
10622
10623/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010624 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010625 */
10626 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010627ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010628{
10629 char_u *text;
10630 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010631 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010632 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010633 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010634 int part_send;
10635 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010636 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010637 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010638
10639 /* return an empty string by default */
10640 rettv->v_type = VAR_STRING;
10641 rettv->vval.v_string = NULL;
10642
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010643 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010644 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010645 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010646 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010647
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010648 ch_mode = channel_get_mode(channel, part_send);
10649 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010650 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010651 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010652 return;
10653 }
10654
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010655 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010656 text = json_encode_nr_expr(id, &argvars[1],
10657 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010658 if (text == NULL)
10659 return;
10660
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010661 channel = send_common(argvars, text, id, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010662 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010663 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010664 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010665 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010666 if (opt.jo_set & JO_TIMEOUT)
10667 timeout = opt.jo_timeout;
10668 else
10669 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010670 timeout = channel_get_timeout(channel, part_read);
10671 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10672 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010673 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010674 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010675
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010676 /* Move the item from the list and then change the type to
10677 * avoid the value being freed. */
10678 *rettv = list->lv_last->li_tv;
10679 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010680 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010681 }
10682 }
10683}
10684
10685/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010686 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010687 */
10688 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010689f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10690{
10691 ch_expr_common(argvars, rettv, TRUE);
10692}
10693
10694/*
10695 * "ch_sendexpr()" function
10696 */
10697 static void
10698f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10699{
10700 ch_expr_common(argvars, rettv, FALSE);
10701}
10702
10703/*
10704 * common for "ch_evalraw()" and "ch_sendraw()"
10705 */
10706 static void
10707ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010708{
10709 char_u buf[NUMBUFLEN];
10710 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010711 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010712 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010713 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010714 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010715
10716 /* return an empty string by default */
10717 rettv->v_type = VAR_STRING;
10718 rettv->vval.v_string = NULL;
10719
10720 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010721 channel = send_common(argvars, text, 0, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010722 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10723 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010724 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010725 if (opt.jo_set & JO_TIMEOUT)
10726 timeout = opt.jo_timeout;
10727 else
10728 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010729 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10730 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010731}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010732
10733/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010734 * "ch_evalraw()" function
10735 */
10736 static void
10737f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10738{
10739 ch_raw_common(argvars, rettv, TRUE);
10740}
10741
10742/*
10743 * "ch_sendraw()" function
10744 */
10745 static void
10746f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10747{
10748 ch_raw_common(argvars, rettv, FALSE);
10749}
10750
10751/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010752 * "ch_setoptions()" function
10753 */
10754 static void
10755f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10756{
10757 channel_T *channel;
10758 jobopt_T opt;
10759
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010760 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010761 if (channel == NULL)
10762 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010763 clear_job_options(&opt);
10764 if (get_job_options(&argvars[1], &opt,
10765 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010766 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010767 channel_set_options(channel, &opt);
10768}
10769
10770/*
10771 * "ch_status()" function
10772 */
10773 static void
10774f_ch_status(typval_T *argvars, typval_T *rettv)
10775{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010776 channel_T *channel;
10777
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010778 /* return an empty string by default */
10779 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010780 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010781
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010782 channel = get_channel_arg(&argvars[0], FALSE);
10783 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010784}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010785#endif
10786
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010787/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010788 * "changenr()" function
10789 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010792{
10793 rettv->vval.v_number = curbuf->b_u_seq_cur;
10794}
10795
10796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 * "char2nr(string)" function
10798 */
10799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010800f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802#ifdef FEAT_MBYTE
10803 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010804 {
10805 int utf8 = 0;
10806
10807 if (argvars[1].v_type != VAR_UNKNOWN)
10808 utf8 = get_tv_number_chk(&argvars[1], NULL);
10809
10810 if (utf8)
10811 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10812 else
10813 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 else
10816#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010817 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818}
10819
10820/*
10821 * "cindent(lnum)" function
10822 */
10823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010824f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826#ifdef FEAT_CINDENT
10827 pos_T pos;
10828 linenr_T lnum;
10829
10830 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10833 {
10834 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010835 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 curwin->w_cursor = pos;
10837 }
10838 else
10839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010844 * "clearmatches()" function
10845 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010848{
10849#ifdef FEAT_SEARCH_EXTRA
10850 clear_matches(curwin);
10851#endif
10852}
10853
10854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 * "col(string)" function
10856 */
10857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010858f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860 colnr_T col = 0;
10861 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010862 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010864 fp = var2fpos(&argvars[0], FALSE, &fnum);
10865 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
10867 if (fp->col == MAXCOL)
10868 {
10869 /* '> can be MAXCOL, get the length of the line then */
10870 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010871 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 else
10873 col = MAXCOL;
10874 }
10875 else
10876 {
10877 col = fp->col + 1;
10878#ifdef FEAT_VIRTUALEDIT
10879 /* col(".") when the cursor is on the NUL at the end of the line
10880 * because of "coladd" can be seen as an extra column. */
10881 if (virtual_active() && fp == &curwin->w_cursor)
10882 {
10883 char_u *p = ml_get_cursor();
10884
10885 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10886 curwin->w_virtcol - curwin->w_cursor.coladd))
10887 {
10888# ifdef FEAT_MBYTE
10889 int l;
10890
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010891 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 col += l;
10893# else
10894 if (*p != NUL && p[1] == NUL)
10895 ++col;
10896# endif
10897 }
10898 }
10899#endif
10900 }
10901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903}
10904
Bram Moolenaar572cb562005-08-05 21:35:02 +000010905#if defined(FEAT_INS_EXPAND)
10906/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010907 * "complete()" function
10908 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010910f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010911{
10912 int startcol;
10913
10914 if ((State & INSERT) == 0)
10915 {
10916 EMSG(_("E785: complete() can only be used in Insert mode"));
10917 return;
10918 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010919
10920 /* Check for undo allowed here, because if something was already inserted
10921 * the line was already saved for undo and this check isn't done. */
10922 if (!undo_allowed())
10923 return;
10924
Bram Moolenaarade00832006-03-10 21:46:58 +000010925 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10926 {
10927 EMSG(_(e_invarg));
10928 return;
10929 }
10930
10931 startcol = get_tv_number_chk(&argvars[0], NULL);
10932 if (startcol <= 0)
10933 return;
10934
10935 set_completion(startcol - 1, argvars[1].vval.v_list);
10936}
10937
10938/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010939 * "complete_add()" function
10940 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010942f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010943{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010944 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010945}
10946
10947/*
10948 * "complete_check()" function
10949 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010951f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010952{
10953 int saved = RedrawingDisabled;
10954
10955 RedrawingDisabled = 0;
10956 ins_compl_check_keys(0);
10957 rettv->vval.v_number = compl_interrupted;
10958 RedrawingDisabled = saved;
10959}
10960#endif
10961
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962/*
10963 * "confirm(message, buttons[, default [, type]])" function
10964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010966f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10969 char_u *message;
10970 char_u *buttons = NULL;
10971 char_u buf[NUMBUFLEN];
10972 char_u buf2[NUMBUFLEN];
10973 int def = 1;
10974 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 char_u *typestr;
10976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 message = get_tv_string_chk(&argvars[0]);
10979 if (message == NULL)
10980 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010981 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10984 if (buttons == NULL)
10985 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010989 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10992 if (typestr == NULL)
10993 error = TRUE;
10994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 switch (TOUPPER_ASC(*typestr))
10997 {
10998 case 'E': type = VIM_ERROR; break;
10999 case 'Q': type = VIM_QUESTION; break;
11000 case 'I': type = VIM_INFO; break;
11001 case 'W': type = VIM_WARNING; break;
11002 case 'G': type = VIM_GENERIC; break;
11003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 }
11005 }
11006 }
11007 }
11008
11009 if (buttons == NULL || *buttons == NUL)
11010 buttons = (char_u *)_("&Ok");
11011
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011012 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010011014 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015#endif
11016}
11017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011018/*
11019 * "copy()" function
11020 */
11021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011023{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011024 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011025}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011027#ifdef FEAT_FLOAT
11028/*
11029 * "cos()" function
11030 */
11031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011032f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011033{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011034 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011035
11036 rettv->v_type = VAR_FLOAT;
11037 if (get_float_arg(argvars, &f) == OK)
11038 rettv->vval.v_float = cos(f);
11039 else
11040 rettv->vval.v_float = 0.0;
11041}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011042
11043/*
11044 * "cosh()" function
11045 */
11046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011047f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011048{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011049 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011050
11051 rettv->v_type = VAR_FLOAT;
11052 if (get_float_arg(argvars, &f) == OK)
11053 rettv->vval.v_float = cosh(f);
11054 else
11055 rettv->vval.v_float = 0.0;
11056}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011057#endif
11058
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011060 * "count()" function
11061 */
11062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011063f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011064{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011065 long n = 0;
11066 int ic = FALSE;
11067
Bram Moolenaare9a41262005-01-15 22:18:47 +000011068 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011069 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011070 listitem_T *li;
11071 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011073
Bram Moolenaare9a41262005-01-15 22:18:47 +000011074 if ((l = argvars[0].vval.v_list) != NULL)
11075 {
11076 li = l->lv_first;
11077 if (argvars[2].v_type != VAR_UNKNOWN)
11078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011079 int error = FALSE;
11080
11081 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011082 if (argvars[3].v_type != VAR_UNKNOWN)
11083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011084 idx = get_tv_number_chk(&argvars[3], &error);
11085 if (!error)
11086 {
11087 li = list_find(l, idx);
11088 if (li == NULL)
11089 EMSGN(_(e_listidx), idx);
11090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011091 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 if (error)
11093 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 }
11095
11096 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011097 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098 ++n;
11099 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011100 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 else if (argvars[0].v_type == VAR_DICT)
11102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 int todo;
11104 dict_T *d;
11105 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011106
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011107 if ((d = argvars[0].vval.v_dict) != NULL)
11108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 int error = FALSE;
11110
Bram Moolenaare9a41262005-01-15 22:18:47 +000011111 if (argvars[2].v_type != VAR_UNKNOWN)
11112 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011114 if (argvars[3].v_type != VAR_UNKNOWN)
11115 EMSG(_(e_invarg));
11116 }
11117
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011118 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011119 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011120 {
11121 if (!HASHITEM_EMPTY(hi))
11122 {
11123 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011124 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011125 ++n;
11126 }
11127 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 }
11129 }
11130 else
11131 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011132 rettv->vval.v_number = n;
11133}
11134
11135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11137 *
11138 * Checks the existence of a cscope connection.
11139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143#ifdef FEAT_CSCOPE
11144 int num = 0;
11145 char_u *dbpath = NULL;
11146 char_u *prepend = NULL;
11147 char_u buf[NUMBUFLEN];
11148
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149 if (argvars[0].v_type != VAR_UNKNOWN
11150 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 num = (int)get_tv_number(&argvars[0]);
11153 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159#endif
11160}
11161
11162/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011163 * "cursor(lnum, col)" function, or
11164 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011166 * Moves the cursor to the specified line and column.
11167 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011170f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
11172 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011173#ifdef FEAT_VIRTUALEDIT
11174 long coladd = 0;
11175#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011176 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011178 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011179 if (argvars[1].v_type == VAR_UNKNOWN)
11180 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011181 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011182 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011183
Bram Moolenaar493c1782014-05-28 14:34:46 +020011184 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011185 {
11186 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011187 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011188 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011189 line = pos.lnum;
11190 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011191#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011192 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011193#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011194 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011195 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011196 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011197 set_curswant = FALSE;
11198 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011199 }
11200 else
11201 {
11202 line = get_tv_lnum(argvars);
11203 col = get_tv_number_chk(&argvars[1], NULL);
11204#ifdef FEAT_VIRTUALEDIT
11205 if (argvars[2].v_type != VAR_UNKNOWN)
11206 coladd = get_tv_number_chk(&argvars[2], NULL);
11207#endif
11208 }
11209 if (line < 0 || col < 0
11210#ifdef FEAT_VIRTUALEDIT
11211 || coladd < 0
11212#endif
11213 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 if (line > 0)
11216 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 if (col > 0)
11218 curwin->w_cursor.col = col - 1;
11219#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011220 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221#endif
11222
11223 /* Make sure the cursor is in a valid position. */
11224 check_cursor();
11225#ifdef FEAT_MBYTE
11226 /* Correct cursor for multi-byte character. */
11227 if (has_mbyte)
11228 mb_adjust_cursor();
11229#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011230
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011231 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011232 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233}
11234
11235/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011236 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 */
11238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011239f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011241 int noref = 0;
11242
11243 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011244 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011245 if (noref < 0 || noref > 1)
11246 EMSG(_(e_invarg));
11247 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011248 {
11249 current_copyID += COPYID_INC;
11250 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
11255 * "delete()" function
11256 */
11257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011258f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011260 char_u nbuf[NUMBUFLEN];
11261 char_u *name;
11262 char_u *flags;
11263
11264 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011266 return;
11267
11268 name = get_tv_string(&argvars[0]);
11269 if (name == NULL || *name == NUL)
11270 {
11271 EMSG(_(e_invarg));
11272 return;
11273 }
11274
11275 if (argvars[1].v_type != VAR_UNKNOWN)
11276 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011278 flags = (char_u *)"";
11279
11280 if (*flags == NUL)
11281 /* delete a file */
11282 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11283 else if (STRCMP(flags, "d") == 0)
11284 /* delete an empty directory */
11285 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11286 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011287 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011288 rettv->vval.v_number = delete_recursive(name);
11289 else
11290 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291}
11292
11293/*
11294 * "did_filetype()" function
11295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011297f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298{
11299#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301#endif
11302}
11303
11304/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011305 * "diff_filler()" function
11306 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011308f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011309{
11310#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011312#endif
11313}
11314
11315/*
11316 * "diff_hlID()" function
11317 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011319f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011320{
11321#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011323 static linenr_T prev_lnum = 0;
11324 static int changedtick = 0;
11325 static int fnum = 0;
11326 static int change_start = 0;
11327 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011328 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011329 int filler_lines;
11330 int col;
11331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 if (lnum < 0) /* ignore type error in {lnum} arg */
11333 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011334 if (lnum != prev_lnum
11335 || changedtick != curbuf->b_changedtick
11336 || fnum != curbuf->b_fnum)
11337 {
11338 /* New line, buffer, change: need to get the values. */
11339 filler_lines = diff_check(curwin, lnum);
11340 if (filler_lines < 0)
11341 {
11342 if (filler_lines == -1)
11343 {
11344 change_start = MAXCOL;
11345 change_end = -1;
11346 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11347 hlID = HLF_ADD; /* added line */
11348 else
11349 hlID = HLF_CHD; /* changed line */
11350 }
11351 else
11352 hlID = HLF_ADD; /* added line */
11353 }
11354 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011355 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011356 prev_lnum = lnum;
11357 changedtick = curbuf->b_changedtick;
11358 fnum = curbuf->b_fnum;
11359 }
11360
11361 if (hlID == HLF_CHD || hlID == HLF_TXD)
11362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011364 if (col >= change_start && col <= change_end)
11365 hlID = HLF_TXD; /* changed text */
11366 else
11367 hlID = HLF_CHD; /* changed line */
11368 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011369 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011370#endif
11371}
11372
11373/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011374 * "disable_char_avail_for_testing({expr})" function
11375 */
11376 static void
11377f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11378{
11379 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11380}
11381
11382/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011383 * "empty({expr})" function
11384 */
11385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011386f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011387{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011388 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011389
11390 switch (argvars[0].v_type)
11391 {
11392 case VAR_STRING:
11393 case VAR_FUNC:
11394 n = argvars[0].vval.v_string == NULL
11395 || *argvars[0].vval.v_string == NUL;
11396 break;
11397 case VAR_NUMBER:
11398 n = argvars[0].vval.v_number == 0;
11399 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011400 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011401#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011402 n = argvars[0].vval.v_float == 0.0;
11403 break;
11404#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011405 case VAR_LIST:
11406 n = argvars[0].vval.v_list == NULL
11407 || argvars[0].vval.v_list->lv_first == NULL;
11408 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011409 case VAR_DICT:
11410 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011411 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011413 case VAR_SPECIAL:
11414 n = argvars[0].vval.v_number != VVAL_TRUE;
11415 break;
11416
Bram Moolenaar835dc632016-02-07 14:27:38 +010011417 case VAR_JOB:
11418#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011419 n = argvars[0].vval.v_job == NULL
11420 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11421 break;
11422#endif
11423 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011424#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011425 n = argvars[0].vval.v_channel == NULL
11426 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011427 break;
11428#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011429 case VAR_UNKNOWN:
11430 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11431 n = TRUE;
11432 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011433 }
11434
11435 rettv->vval.v_number = n;
11436}
11437
11438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 * "escape({string}, {chars})" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u buf[NUMBUFLEN];
11445
Bram Moolenaar758711c2005-02-02 23:11:38 +000011446 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11447 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
11451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011452 * "eval()" function
11453 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011455f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011457 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 s = get_tv_string_chk(&argvars[0]);
11460 if (s != NULL)
11461 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011462
Bram Moolenaar615b9972015-01-14 17:15:05 +010011463 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11465 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011466 if (p != NULL && !aborting())
11467 EMSG2(_(e_invexpr2), p);
11468 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011470 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011471 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011472 else if (*s != NUL)
11473 EMSG(_(e_trailing));
11474}
11475
11476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 * "eventhandler()" function
11478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011480f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483}
11484
11485/*
11486 * "executable()" function
11487 */
11488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011489f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011491 char_u *name = get_tv_string(&argvars[0]);
11492
11493 /* Check in $PATH and also check directly if there is a directory name. */
11494 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11495 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011496}
11497
11498/*
11499 * "exepath()" function
11500 */
11501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011502f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011503{
11504 char_u *p = NULL;
11505
Bram Moolenaarb5971142015-03-21 17:32:19 +010011506 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011507 rettv->v_type = VAR_STRING;
11508 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509}
11510
11511/*
11512 * "exists()" function
11513 */
11514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011515f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
11517 char_u *p;
11518 char_u *name;
11519 int n = FALSE;
11520 int len = 0;
11521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 if (*p == '$') /* environment variable */
11524 {
11525 /* first try "normal" environment variables (fast) */
11526 if (mch_getenv(p + 1) != NULL)
11527 n = TRUE;
11528 else
11529 {
11530 /* try expanding things like $VIM and ${HOME} */
11531 p = expand_env_save(p);
11532 if (p != NULL && *p != '$')
11533 n = TRUE;
11534 vim_free(p);
11535 }
11536 }
11537 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011540 if (*skipwhite(p) != NUL)
11541 n = FALSE; /* trailing garbage */
11542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 else if (*p == '*') /* internal or user defined function */
11544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011545 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 }
11547 else if (*p == ':')
11548 {
11549 n = cmd_exists(p + 1);
11550 }
11551 else if (*p == '#')
11552 {
11553#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011554 if (p[1] == '#')
11555 n = autocmd_supported(p + 2);
11556 else
11557 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558#endif
11559 }
11560 else /* internal variable */
11561 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011562 char_u *tofree;
11563 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011565 /* get_name_len() takes care of expanding curly braces */
11566 name = p;
11567 len = get_name_len(&p, &tofree, TRUE, FALSE);
11568 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011570 if (tofree != NULL)
11571 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011572 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011573 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011575 /* handle d.key, l[idx], f(expr) */
11576 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11577 if (n)
11578 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 }
11580 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011581 if (*p != NUL)
11582 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 }
11586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011587 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588}
11589
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011590#ifdef FEAT_FLOAT
11591/*
11592 * "exp()" function
11593 */
11594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011595f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011596{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011597 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011598
11599 rettv->v_type = VAR_FLOAT;
11600 if (get_float_arg(argvars, &f) == OK)
11601 rettv->vval.v_float = exp(f);
11602 else
11603 rettv->vval.v_float = 0.0;
11604}
11605#endif
11606
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607/*
11608 * "expand()" function
11609 */
11610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011611f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612{
11613 char_u *s;
11614 int len;
11615 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011616 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011619 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011622 if (argvars[1].v_type != VAR_UNKNOWN
11623 && argvars[2].v_type != VAR_UNKNOWN
11624 && get_tv_number_chk(&argvars[2], &error)
11625 && !error)
11626 {
11627 rettv->v_type = VAR_LIST;
11628 rettv->vval.v_list = NULL;
11629 }
11630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 if (*s == '%' || *s == '#' || *s == '<')
11633 {
11634 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011635 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011637 if (rettv->v_type == VAR_LIST)
11638 {
11639 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11640 list_append_string(rettv->vval.v_list, result, -1);
11641 else
11642 vim_free(result);
11643 }
11644 else
11645 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 }
11647 else
11648 {
11649 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011650 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 if (argvars[1].v_type != VAR_UNKNOWN
11652 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011653 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 if (!error)
11655 {
11656 ExpandInit(&xpc);
11657 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011658 if (p_wic)
11659 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011660 if (rettv->v_type == VAR_STRING)
11661 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11662 options, WILD_ALL);
11663 else if (rettv_list_alloc(rettv) != FAIL)
11664 {
11665 int i;
11666
11667 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11668 for (i = 0; i < xpc.xp_numfiles; i++)
11669 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11670 ExpandCleanup(&xpc);
11671 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 }
11673 else
11674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 }
11676}
11677
11678/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011679 * Go over all entries in "d2" and add them to "d1".
11680 * When "action" is "error" then a duplicate key is an error.
11681 * When "action" is "force" then a duplicate key is overwritten.
11682 * Otherwise duplicate keys are ignored ("action" is "keep").
11683 */
11684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011685dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011686{
11687 dictitem_T *di1;
11688 hashitem_T *hi2;
11689 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011690 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011691
11692 todo = (int)d2->dv_hashtab.ht_used;
11693 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11694 {
11695 if (!HASHITEM_EMPTY(hi2))
11696 {
11697 --todo;
11698 di1 = dict_find(d1, hi2->hi_key, -1);
11699 if (d1->dv_scope != 0)
11700 {
11701 /* Disallow replacing a builtin function in l: and g:.
11702 * Check the key to be valid when adding to any
11703 * scope. */
11704 if (d1->dv_scope == VAR_DEF_SCOPE
11705 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11706 && var_check_func_name(hi2->hi_key,
11707 di1 == NULL))
11708 break;
11709 if (!valid_varname(hi2->hi_key))
11710 break;
11711 }
11712 if (di1 == NULL)
11713 {
11714 di1 = dictitem_copy(HI2DI(hi2));
11715 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11716 dictitem_free(di1);
11717 }
11718 else if (*action == 'e')
11719 {
11720 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11721 break;
11722 }
11723 else if (*action == 'f' && HI2DI(hi2) != di1)
11724 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011725 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11726 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011727 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011728 clear_tv(&di1->di_tv);
11729 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11730 }
11731 }
11732 }
11733}
11734
11735/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011736 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011738 */
11739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011741{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011742 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011743
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011745 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011746 list_T *l1, *l2;
11747 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011750
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 l1 = argvars[0].vval.v_list;
11752 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011753 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011754 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 {
11756 if (argvars[2].v_type != VAR_UNKNOWN)
11757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011758 before = get_tv_number_chk(&argvars[2], &error);
11759 if (error)
11760 return; /* type error; errmsg already given */
11761
Bram Moolenaar758711c2005-02-02 23:11:38 +000011762 if (before == l1->lv_len)
11763 item = NULL;
11764 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011766 item = list_find(l1, before);
11767 if (item == NULL)
11768 {
11769 EMSGN(_(e_listidx), before);
11770 return;
11771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 }
11773 }
11774 else
11775 item = NULL;
11776 list_extend(l1, l2, item);
11777
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 copy_tv(&argvars[0], rettv);
11779 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011780 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011781 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11782 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011783 dict_T *d1, *d2;
11784 char_u *action;
11785 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786
11787 d1 = argvars[0].vval.v_dict;
11788 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011789 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011791 {
11792 /* Check the third argument. */
11793 if (argvars[2].v_type != VAR_UNKNOWN)
11794 {
11795 static char *(av[]) = {"keep", "force", "error"};
11796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 action = get_tv_string_chk(&argvars[2]);
11798 if (action == NULL)
11799 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011800 for (i = 0; i < 3; ++i)
11801 if (STRCMP(action, av[i]) == 0)
11802 break;
11803 if (i == 3)
11804 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011805 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011806 return;
11807 }
11808 }
11809 else
11810 action = (char_u *)"force";
11811
Bram Moolenaara9922d62013-05-30 13:01:18 +020011812 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011813
Bram Moolenaare9a41262005-01-15 22:18:47 +000011814 copy_tv(&argvars[0], rettv);
11815 }
11816 }
11817 else
11818 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011819}
11820
11821/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011822 * "feedkeys()" function
11823 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011825f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011826{
11827 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011828 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011829 char_u *keys, *flags;
11830 char_u nbuf[NUMBUFLEN];
11831 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011832 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011833 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011834
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011835 /* This is not allowed in the sandbox. If the commands would still be
11836 * executed in the sandbox it would be OK, but it probably happens later,
11837 * when "sandbox" is no longer set. */
11838 if (check_secure())
11839 return;
11840
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011841 keys = get_tv_string(&argvars[0]);
11842 if (*keys != NUL)
11843 {
11844 if (argvars[1].v_type != VAR_UNKNOWN)
11845 {
11846 flags = get_tv_string_buf(&argvars[1], nbuf);
11847 for ( ; *flags != NUL; ++flags)
11848 {
11849 switch (*flags)
11850 {
11851 case 'n': remap = FALSE; break;
11852 case 'm': remap = TRUE; break;
11853 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011854 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011855 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011856 }
11857 }
11858 }
11859
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011860 /* Need to escape K_SPECIAL and CSI before putting the string in the
11861 * typeahead buffer. */
11862 keys_esc = vim_strsave_escape_csi(keys);
11863 if (keys_esc != NULL)
11864 {
11865 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011866 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011867 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011868 if (vgetc_busy)
11869 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011870 if (execute)
11871 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011872 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011873 }
11874}
11875
11876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 * "filereadable()" function
11878 */
11879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011882 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 char_u *p;
11884 int n;
11885
Bram Moolenaarc236c162008-07-13 17:41:49 +000011886#ifndef O_NONBLOCK
11887# define O_NONBLOCK 0
11888#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011889 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011890 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11891 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 {
11893 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011894 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895 }
11896 else
11897 n = FALSE;
11898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011900}
11901
11902/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011903 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 * rights to write into.
11905 */
11906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011907f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011909 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910}
11911
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011912 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011913findfilendir(
11914 typval_T *argvars UNUSED,
11915 typval_T *rettv,
11916 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011917{
11918#ifdef FEAT_SEARCHPATH
11919 char_u *fname;
11920 char_u *fresult = NULL;
11921 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11922 char_u *p;
11923 char_u pathbuf[NUMBUFLEN];
11924 int count = 1;
11925 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011926 int error = FALSE;
11927#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011928
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011929 rettv->vval.v_string = NULL;
11930 rettv->v_type = VAR_STRING;
11931
11932#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011934
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11938 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011939 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011940 else
11941 {
11942 if (*p != NUL)
11943 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011946 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011948 }
11949
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011950 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11951 error = TRUE;
11952
11953 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 do
11956 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011957 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011958 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 fresult = find_file_in_path_option(first ? fname : NULL,
11960 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011961 0, first, path,
11962 find_what,
11963 curbuf->b_ffname,
11964 find_what == FINDFILE_DIR
11965 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011967
11968 if (fresult != NULL && rettv->v_type == VAR_LIST)
11969 list_append_string(rettv->vval.v_list, fresult, -1);
11970
11971 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011973
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011974 if (rettv->v_type == VAR_STRING)
11975 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011976#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011977}
11978
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011979static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11980static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011981
11982/*
11983 * Implementation of map() and filter().
11984 */
11985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011986filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987{
11988 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011989 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 listitem_T *li, *nli;
11991 list_T *l = NULL;
11992 dictitem_T *di;
11993 hashtab_T *ht;
11994 hashitem_T *hi;
11995 dict_T *d = NULL;
11996 typval_T save_val;
11997 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011998 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011999 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012000 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012001 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012002 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012003 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012004 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012005
Bram Moolenaare9a41262005-01-15 22:18:47 +000012006 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012007 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012008 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012009 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012010 return;
12011 }
12012 else if (argvars[0].v_type == VAR_DICT)
12013 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012014 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012015 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016 return;
12017 }
12018 else
12019 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012020 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012021 return;
12022 }
12023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 expr = get_tv_string_buf_chk(&argvars[1], buf);
12025 /* On type errors, the preceding call has already displayed an error
12026 * message. Avoid a misleading error message for an empty string that
12027 * was not passed as argument. */
12028 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012030 prepare_vimvar(VV_VAL, &save_val);
12031 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012032
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012033 /* We reset "did_emsg" to be able to detect whether an error
12034 * occurred during evaluation of the expression. */
12035 save_did_emsg = did_emsg;
12036 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012037
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012038 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 vimvars[VV_KEY].vv_type = VAR_STRING;
12042
12043 ht = &d->dv_hashtab;
12044 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012045 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012048 if (!HASHITEM_EMPTY(hi))
12049 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012050 int r;
12051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 --todo;
12053 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012054 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012055 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12056 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 break;
12058 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012059 r = filter_map_one(&di->di_tv, expr, map, &rem);
12060 clear_tv(&vimvars[VV_KEY].vv_tv);
12061 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062 break;
12063 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012064 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012065 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12066 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012067 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012070 }
12071 }
12072 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 }
12074 else
12075 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012076 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12077
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012078 for (li = l->lv_first; li != NULL; li = nli)
12079 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012080 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012081 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012083 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012084 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012085 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012086 break;
12087 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012089 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012090 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012091 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012092
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012093 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012095
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012096 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012098
12099 copy_tv(&argvars[0], rettv);
12100}
12101
12102 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012103filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104{
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012106 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012107 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110 s = expr;
12111 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012112 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113 if (*s != NUL) /* check for trailing chars after expr */
12114 {
12115 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012116 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012117 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118 }
12119 if (map)
12120 {
12121 /* map(): replace the list item value */
12122 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012123 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124 *tv = rettv;
12125 }
12126 else
12127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012128 int error = FALSE;
12129
Bram Moolenaare9a41262005-01-15 22:18:47 +000012130 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012131 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012132 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012133 /* On type error, nothing has been removed; return FAIL to stop the
12134 * loop. The error message was given by get_tv_number_chk(). */
12135 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012136 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012137 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012138 retval = OK;
12139theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012141 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012142}
12143
12144/*
12145 * "filter()" function
12146 */
12147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012148f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012149{
12150 filter_map(argvars, rettv, FALSE);
12151}
12152
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154 * "finddir({fname}[, {path}[, {count}]])" function
12155 */
12156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012157f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012159 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012160}
12161
12162/*
12163 * "findfile({fname}[, {path}[, {count}]])" function
12164 */
12165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012166f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012167{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012168 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169}
12170
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012171#ifdef FEAT_FLOAT
12172/*
12173 * "float2nr({float})" function
12174 */
12175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012176f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012177{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012178 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012179
12180 if (get_float_arg(argvars, &f) == OK)
12181 {
12182 if (f < -0x7fffffff)
12183 rettv->vval.v_number = -0x7fffffff;
12184 else if (f > 0x7fffffff)
12185 rettv->vval.v_number = 0x7fffffff;
12186 else
12187 rettv->vval.v_number = (varnumber_T)f;
12188 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012189}
12190
12191/*
12192 * "floor({float})" function
12193 */
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012196{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012197 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012198
12199 rettv->v_type = VAR_FLOAT;
12200 if (get_float_arg(argvars, &f) == OK)
12201 rettv->vval.v_float = floor(f);
12202 else
12203 rettv->vval.v_float = 0.0;
12204}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012205
12206/*
12207 * "fmod()" function
12208 */
12209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012210f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012211{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012212 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012213
12214 rettv->v_type = VAR_FLOAT;
12215 if (get_float_arg(argvars, &fx) == OK
12216 && get_float_arg(&argvars[1], &fy) == OK)
12217 rettv->vval.v_float = fmod(fx, fy);
12218 else
12219 rettv->vval.v_float = 0.0;
12220}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012221#endif
12222
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012224 * "fnameescape({string})" function
12225 */
12226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012227f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012228{
12229 rettv->vval.v_string = vim_strsave_fnameescape(
12230 get_tv_string(&argvars[0]), FALSE);
12231 rettv->v_type = VAR_STRING;
12232}
12233
12234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 * "fnamemodify({fname}, {mods})" function
12236 */
12237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012238f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239{
12240 char_u *fname;
12241 char_u *mods;
12242 int usedlen = 0;
12243 int len;
12244 char_u *fbuf = NULL;
12245 char_u buf[NUMBUFLEN];
12246
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012247 fname = get_tv_string_chk(&argvars[0]);
12248 mods = get_tv_string_buf_chk(&argvars[1], buf);
12249 if (fname == NULL || mods == NULL)
12250 fname = NULL;
12251 else
12252 {
12253 len = (int)STRLEN(fname);
12254 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 vim_free(fbuf);
12263}
12264
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012265static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266
12267/*
12268 * "foldclosed()" function
12269 */
12270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012271foldclosed_both(
12272 typval_T *argvars UNUSED,
12273 typval_T *rettv,
12274 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
12276#ifdef FEAT_FOLDING
12277 linenr_T lnum;
12278 linenr_T first, last;
12279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012280 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12282 {
12283 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12284 {
12285 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012288 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 return;
12290 }
12291 }
12292#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294}
12295
12296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297 * "foldclosed()" function
12298 */
12299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012300f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301{
12302 foldclosed_both(argvars, rettv, FALSE);
12303}
12304
12305/*
12306 * "foldclosedend()" function
12307 */
12308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012309f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012310{
12311 foldclosed_both(argvars, rettv, TRUE);
12312}
12313
12314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 * "foldlevel()" function
12316 */
12317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012318f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319{
12320#ifdef FEAT_FOLDING
12321 linenr_T lnum;
12322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
12330 * "foldtext()" function
12331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
12335#ifdef FEAT_FOLDING
12336 linenr_T lnum;
12337 char_u *s;
12338 char_u *r;
12339 int len;
12340 char *txt;
12341#endif
12342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->v_type = VAR_STRING;
12344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012346 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12347 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12348 <= curbuf->b_ml.ml_line_count
12349 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 {
12351 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012352 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12353 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 {
12355 if (!linewhite(lnum))
12356 break;
12357 ++lnum;
12358 }
12359
12360 /* Find interesting text in this line. */
12361 s = skipwhite(ml_get(lnum));
12362 /* skip C comment-start */
12363 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012366 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012367 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012368 {
12369 s = skipwhite(ml_get(lnum + 1));
12370 if (*s == '*')
12371 s = skipwhite(s + 1);
12372 }
12373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 txt = _("+-%s%3ld lines: ");
12375 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012376 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 + 20 /* for %3ld */
12378 + STRLEN(s))); /* concatenated */
12379 if (r != NULL)
12380 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012381 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12382 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12383 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 len = (int)STRLEN(r);
12385 STRCAT(r, s);
12386 /* remove 'foldmarker' and 'commentstring' */
12387 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 }
12390 }
12391#endif
12392}
12393
12394/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012395 * "foldtextresult(lnum)" function
12396 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012398f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012399{
12400#ifdef FEAT_FOLDING
12401 linenr_T lnum;
12402 char_u *text;
12403 char_u buf[51];
12404 foldinfo_T foldinfo;
12405 int fold_count;
12406#endif
12407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012408 rettv->v_type = VAR_STRING;
12409 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012410#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412 /* treat illegal types and illegal string values for {lnum} the same */
12413 if (lnum < 0)
12414 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012415 fold_count = foldedCount(curwin, lnum, &foldinfo);
12416 if (fold_count > 0)
12417 {
12418 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12419 &foldinfo, buf);
12420 if (text == buf)
12421 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012423 }
12424#endif
12425}
12426
12427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 * "foreground()" function
12429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012431f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433#ifdef FEAT_GUI
12434 if (gui.in_use)
12435 gui_mch_set_foreground();
12436#else
12437# ifdef WIN32
12438 win32_set_foreground();
12439# endif
12440#endif
12441}
12442
12443/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012444 * "function()" function
12445 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012447f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448{
12449 char_u *s;
12450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012452 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012453 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012454 /* Don't check an autoload name for existence here. */
12455 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012456 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012457 else
12458 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012459 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012460 {
12461 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012462 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012463
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012464 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12465 * also be called from another script. Using trans_function_name()
12466 * would also work, but some plugins depend on the name being
12467 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012468 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012469 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012470 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012471 if (rettv->vval.v_string != NULL)
12472 {
12473 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012474 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012475 }
12476 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012477 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012478 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012480 }
12481}
12482
12483/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012484 * "garbagecollect()" function
12485 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012487f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012488{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012489 /* This is postponed until we are back at the toplevel, because we may be
12490 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12491 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012492
12493 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12494 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012495}
12496
12497/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498 * "get()" function
12499 */
12500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012501f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502{
Bram Moolenaar33570922005-01-25 22:26:29 +000012503 listitem_T *li;
12504 list_T *l;
12505 dictitem_T *di;
12506 dict_T *d;
12507 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
Bram Moolenaare9a41262005-01-15 22:18:47 +000012509 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012510 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012511 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513 int error = FALSE;
12514
12515 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12516 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012517 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012520 else if (argvars[0].v_type == VAR_DICT)
12521 {
12522 if ((d = argvars[0].vval.v_dict) != NULL)
12523 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012524 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012525 if (di != NULL)
12526 tv = &di->di_tv;
12527 }
12528 }
12529 else
12530 EMSG2(_(e_listdictarg), "get()");
12531
12532 if (tv == NULL)
12533 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012534 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012535 copy_tv(&argvars[2], rettv);
12536 }
12537 else
12538 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012539}
12540
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012541static 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 +000012542
12543/*
12544 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012545 * Return a range (from start to end) of lines in rettv from the specified
12546 * buffer.
12547 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012548 */
12549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012550get_buffer_lines(
12551 buf_T *buf,
12552 linenr_T start,
12553 linenr_T end,
12554 int retlist,
12555 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012556{
12557 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012558
Bram Moolenaar959a1432013-12-14 12:17:38 +010012559 rettv->v_type = VAR_STRING;
12560 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012561 if (retlist && rettv_list_alloc(rettv) == FAIL)
12562 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012563
12564 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12565 return;
12566
12567 if (!retlist)
12568 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012569 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12570 p = ml_get_buf(buf, start, FALSE);
12571 else
12572 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012573 rettv->vval.v_string = vim_strsave(p);
12574 }
12575 else
12576 {
12577 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012578 return;
12579
12580 if (start < 1)
12581 start = 1;
12582 if (end > buf->b_ml.ml_line_count)
12583 end = buf->b_ml.ml_line_count;
12584 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012585 if (list_append_string(rettv->vval.v_list,
12586 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012587 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012588 }
12589}
12590
12591/*
12592 * "getbufline()" function
12593 */
12594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012595f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012596{
12597 linenr_T lnum;
12598 linenr_T end;
12599 buf_T *buf;
12600
12601 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12602 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012603 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012604 --emsg_off;
12605
Bram Moolenaar661b1822005-07-28 22:36:45 +000012606 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012607 if (argvars[2].v_type == VAR_UNKNOWN)
12608 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012609 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012610 end = get_tv_lnum_buf(&argvars[2], buf);
12611
Bram Moolenaar342337a2005-07-21 21:11:17 +000012612 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012613}
12614
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615/*
12616 * "getbufvar()" function
12617 */
12618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012619f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620{
12621 buf_T *buf;
12622 buf_T *save_curbuf;
12623 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012624 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12628 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012630 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012632 rettv->v_type = VAR_STRING;
12633 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012634
12635 if (buf != NULL && varname != NULL)
12636 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012637 /* set curbuf to be our buf, temporarily */
12638 save_curbuf = curbuf;
12639 curbuf = buf;
12640
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012642 {
12643 if (get_option_tv(&varname, rettv, TRUE) == OK)
12644 done = TRUE;
12645 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012646 else if (STRCMP(varname, "changedtick") == 0)
12647 {
12648 rettv->v_type = VAR_NUMBER;
12649 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012650 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012651 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652 else
12653 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012654 /* Look up the variable. */
12655 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12656 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12657 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012659 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012660 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012661 done = TRUE;
12662 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012663 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012664
12665 /* restore previous notion of curbuf */
12666 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 }
12668
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012669 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12670 /* use the default value */
12671 copy_tv(&argvars[2], rettv);
12672
Bram Moolenaar0d660222005-01-07 21:51:51 +000012673 --emsg_off;
12674}
12675
12676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 * "getchar()" function
12678 */
12679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012680f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681{
12682 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012683 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012685 /* Position the cursor. Needed after a message that ends in a space. */
12686 windgoto(msg_row, msg_col);
12687
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 ++no_mapping;
12689 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012690 for (;;)
12691 {
12692 if (argvars[0].v_type == VAR_UNKNOWN)
12693 /* getchar(): blocking wait. */
12694 n = safe_vgetc();
12695 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12696 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012697 n = vpeekc_any();
12698 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012699 /* illegal argument or getchar(0) and no char avail: return zero */
12700 n = 0;
12701 else
12702 /* getchar(0) and char avail: return char */
12703 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012704
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012705 if (n == K_IGNORE)
12706 continue;
12707 break;
12708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 --no_mapping;
12710 --allow_keys;
12711
Bram Moolenaar219b8702006-11-01 14:32:36 +000012712 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12713 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12714 vimvars[VV_MOUSE_COL].vv_nr = 0;
12715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 if (IS_SPECIAL(n) || mod_mask != 0)
12718 {
12719 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12720 int i = 0;
12721
12722 /* Turn a special key into three bytes, plus modifier. */
12723 if (mod_mask != 0)
12724 {
12725 temp[i++] = K_SPECIAL;
12726 temp[i++] = KS_MODIFIER;
12727 temp[i++] = mod_mask;
12728 }
12729 if (IS_SPECIAL(n))
12730 {
12731 temp[i++] = K_SPECIAL;
12732 temp[i++] = K_SECOND(n);
12733 temp[i++] = K_THIRD(n);
12734 }
12735#ifdef FEAT_MBYTE
12736 else if (has_mbyte)
12737 i += (*mb_char2bytes)(n, temp + i);
12738#endif
12739 else
12740 temp[i++] = n;
12741 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->v_type = VAR_STRING;
12743 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012744
12745#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012746 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012747 {
12748 int row = mouse_row;
12749 int col = mouse_col;
12750 win_T *win;
12751 linenr_T lnum;
12752# ifdef FEAT_WINDOWS
12753 win_T *wp;
12754# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012755 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012756
12757 if (row >= 0 && col >= 0)
12758 {
12759 /* Find the window at the mouse coordinates and compute the
12760 * text position. */
12761 win = mouse_find_win(&row, &col);
12762 (void)mouse_comp_pos(win, &row, &col, &lnum);
12763# ifdef FEAT_WINDOWS
12764 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012765 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012766# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012767 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012768 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12769 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12770 }
12771 }
12772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 }
12774}
12775
12776/*
12777 * "getcharmod()" function
12778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012780f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783}
12784
12785/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012786 * "getcharsearch()" function
12787 */
12788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012790{
12791 if (rettv_dict_alloc(rettv) != FAIL)
12792 {
12793 dict_T *dict = rettv->vval.v_dict;
12794
12795 dict_add_nr_str(dict, "char", 0L, last_csearch());
12796 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12797 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12798 }
12799}
12800
12801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 * "getcmdline()" function
12803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012805f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->v_type = VAR_STRING;
12808 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809}
12810
12811/*
12812 * "getcmdpos()" function
12813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012815f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818}
12819
12820/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012821 * "getcmdtype()" function
12822 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012824f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012825{
12826 rettv->v_type = VAR_STRING;
12827 rettv->vval.v_string = alloc(2);
12828 if (rettv->vval.v_string != NULL)
12829 {
12830 rettv->vval.v_string[0] = get_cmdline_type();
12831 rettv->vval.v_string[1] = NUL;
12832 }
12833}
12834
12835/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012836 * "getcmdwintype()" function
12837 */
12838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012839f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012840{
12841 rettv->v_type = VAR_STRING;
12842 rettv->vval.v_string = NULL;
12843#ifdef FEAT_CMDWIN
12844 rettv->vval.v_string = alloc(2);
12845 if (rettv->vval.v_string != NULL)
12846 {
12847 rettv->vval.v_string[0] = cmdwin_type;
12848 rettv->vval.v_string[1] = NUL;
12849 }
12850#endif
12851}
12852
12853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 * "getcwd()" function
12855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012857f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012859 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012860 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012863 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012864
12865 wp = find_tabwin(&argvars[0], &argvars[1]);
12866 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012868 if (wp->w_localdir != NULL)
12869 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12870 else if(globaldir != NULL)
12871 rettv->vval.v_string = vim_strsave(globaldir);
12872 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012873 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012874 cwd = alloc(MAXPATHL);
12875 if (cwd != NULL)
12876 {
12877 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12878 rettv->vval.v_string = vim_strsave(cwd);
12879 vim_free(cwd);
12880 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012881 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012882#ifdef BACKSLASH_IN_FILENAME
12883 if (rettv->vval.v_string != NULL)
12884 slash_adjust(rettv->vval.v_string);
12885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 }
12887}
12888
12889/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012890 * "getfontname()" function
12891 */
12892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012893f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012894{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->v_type = VAR_STRING;
12896 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012897#ifdef FEAT_GUI
12898 if (gui.in_use)
12899 {
12900 GuiFont font;
12901 char_u *name = NULL;
12902
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012903 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012904 {
12905 /* Get the "Normal" font. Either the name saved by
12906 * hl_set_font_name() or from the font ID. */
12907 font = gui.norm_font;
12908 name = hl_get_font_name();
12909 }
12910 else
12911 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012913 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12914 return;
12915 font = gui_mch_get_font(name, FALSE);
12916 if (font == NOFONT)
12917 return; /* Invalid font name, return empty string. */
12918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012920 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012921 gui_mch_free_font(font);
12922 }
12923#endif
12924}
12925
12926/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012927 * "getfperm({fname})" function
12928 */
12929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012930f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012931{
12932 char_u *fname;
12933 struct stat st;
12934 char_u *perm = NULL;
12935 char_u flags[] = "rwx";
12936 int i;
12937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012941 if (mch_stat((char *)fname, &st) >= 0)
12942 {
12943 perm = vim_strsave((char_u *)"---------");
12944 if (perm != NULL)
12945 {
12946 for (i = 0; i < 9; i++)
12947 {
12948 if (st.st_mode & (1 << (8 - i)))
12949 perm[i] = flags[i % 3];
12950 }
12951 }
12952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012953 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012954}
12955
12956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 * "getfsize({fname})" function
12958 */
12959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012960f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961{
12962 char_u *fname;
12963 struct stat st;
12964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
12969 if (mch_stat((char *)fname, &st) >= 0)
12970 {
12971 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012976
12977 /* non-perfect check for overflow */
12978 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12979 rettv->vval.v_number = -2;
12980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 }
12982 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984}
12985
12986/*
12987 * "getftime({fname})" function
12988 */
12989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012990f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991{
12992 char_u *fname;
12993 struct stat st;
12994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996
12997 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001}
13002
13003/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013004 * "getftype({fname})" function
13005 */
13006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013007f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013008{
13009 char_u *fname;
13010 struct stat st;
13011 char_u *type = NULL;
13012 char *t;
13013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013016 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013017 if (mch_lstat((char *)fname, &st) >= 0)
13018 {
13019#ifdef S_ISREG
13020 if (S_ISREG(st.st_mode))
13021 t = "file";
13022 else if (S_ISDIR(st.st_mode))
13023 t = "dir";
13024# ifdef S_ISLNK
13025 else if (S_ISLNK(st.st_mode))
13026 t = "link";
13027# endif
13028# ifdef S_ISBLK
13029 else if (S_ISBLK(st.st_mode))
13030 t = "bdev";
13031# endif
13032# ifdef S_ISCHR
13033 else if (S_ISCHR(st.st_mode))
13034 t = "cdev";
13035# endif
13036# ifdef S_ISFIFO
13037 else if (S_ISFIFO(st.st_mode))
13038 t = "fifo";
13039# endif
13040# ifdef S_ISSOCK
13041 else if (S_ISSOCK(st.st_mode))
13042 t = "fifo";
13043# endif
13044 else
13045 t = "other";
13046#else
13047# ifdef S_IFMT
13048 switch (st.st_mode & S_IFMT)
13049 {
13050 case S_IFREG: t = "file"; break;
13051 case S_IFDIR: t = "dir"; break;
13052# ifdef S_IFLNK
13053 case S_IFLNK: t = "link"; break;
13054# endif
13055# ifdef S_IFBLK
13056 case S_IFBLK: t = "bdev"; break;
13057# endif
13058# ifdef S_IFCHR
13059 case S_IFCHR: t = "cdev"; break;
13060# endif
13061# ifdef S_IFIFO
13062 case S_IFIFO: t = "fifo"; break;
13063# endif
13064# ifdef S_IFSOCK
13065 case S_IFSOCK: t = "socket"; break;
13066# endif
13067 default: t = "other";
13068 }
13069# else
13070 if (mch_isdir(fname))
13071 t = "dir";
13072 else
13073 t = "file";
13074# endif
13075#endif
13076 type = vim_strsave((char_u *)t);
13077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013078 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013079}
13080
13081/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013082 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083 */
13084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013085f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013086{
13087 linenr_T lnum;
13088 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013089 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090
13091 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013092 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013093 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013094 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013095 retlist = FALSE;
13096 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013097 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013098 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013099 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013100 retlist = TRUE;
13101 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013102
Bram Moolenaar342337a2005-07-21 21:11:17 +000013103 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013104}
13105
13106/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013107 * "getmatches()" function
13108 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013110f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013111{
13112#ifdef FEAT_SEARCH_EXTRA
13113 dict_T *dict;
13114 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013115 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013116
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013117 if (rettv_list_alloc(rettv) == OK)
13118 {
13119 while (cur != NULL)
13120 {
13121 dict = dict_alloc();
13122 if (dict == NULL)
13123 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013124 if (cur->match.regprog == NULL)
13125 {
13126 /* match added with matchaddpos() */
13127 for (i = 0; i < MAXPOSMATCH; ++i)
13128 {
13129 llpos_T *llpos;
13130 char buf[6];
13131 list_T *l;
13132
13133 llpos = &cur->pos.pos[i];
13134 if (llpos->lnum == 0)
13135 break;
13136 l = list_alloc();
13137 if (l == NULL)
13138 break;
13139 list_append_number(l, (varnumber_T)llpos->lnum);
13140 if (llpos->col > 0)
13141 {
13142 list_append_number(l, (varnumber_T)llpos->col);
13143 list_append_number(l, (varnumber_T)llpos->len);
13144 }
13145 sprintf(buf, "pos%d", i + 1);
13146 dict_add_list(dict, buf, l);
13147 }
13148 }
13149 else
13150 {
13151 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13152 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013153 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013154 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13155 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013156# ifdef FEAT_CONCEAL
13157 if (cur->conceal_char)
13158 {
13159 char_u buf[MB_MAXBYTES + 1];
13160
13161 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13162 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13163 }
13164# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013165 list_append_dict(rettv->vval.v_list, dict);
13166 cur = cur->next;
13167 }
13168 }
13169#endif
13170}
13171
13172/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013173 * "getpid()" function
13174 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013176f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013177{
13178 rettv->vval.v_number = mch_get_pid();
13179}
13180
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013181static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013182
13183/*
13184 * "getcurpos()" function
13185 */
13186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013187f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013188{
13189 getpos_both(argvars, rettv, TRUE);
13190}
13191
Bram Moolenaar18081e32008-02-20 19:11:07 +000013192/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013193 * "getpos(string)" function
13194 */
13195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013196f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013197{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013198 getpos_both(argvars, rettv, FALSE);
13199}
13200
13201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202getpos_both(
13203 typval_T *argvars,
13204 typval_T *rettv,
13205 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013206{
Bram Moolenaara5525202006-03-02 22:52:09 +000013207 pos_T *fp;
13208 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013209 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013210
13211 if (rettv_list_alloc(rettv) == OK)
13212 {
13213 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013214 if (getcurpos)
13215 fp = &curwin->w_cursor;
13216 else
13217 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013218 if (fnum != -1)
13219 list_append_number(l, (varnumber_T)fnum);
13220 else
13221 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013222 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13223 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013224 list_append_number(l, (fp != NULL)
13225 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013226 : (varnumber_T)0);
13227 list_append_number(l,
13228#ifdef FEAT_VIRTUALEDIT
13229 (fp != NULL) ? (varnumber_T)fp->coladd :
13230#endif
13231 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013232 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013233 {
13234 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013235 list_append_number(l, curwin->w_curswant == MAXCOL ?
13236 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013237 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013238 }
13239 else
13240 rettv->vval.v_number = FALSE;
13241}
13242
13243/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013244 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013245 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013247f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013248{
13249#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013250 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013251#endif
13252
Bram Moolenaar2641f772005-03-25 21:58:17 +000013253#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013254 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013255 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013256 wp = NULL;
13257 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13258 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013259 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013260 if (wp == NULL)
13261 return;
13262 }
13263
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013264 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013265 }
13266#endif
13267}
13268
13269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 * "getreg()" function
13271 */
13272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013273f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274{
13275 char_u *strregname;
13276 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013277 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013278 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013281 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 strregname = get_tv_string_chk(&argvars[0]);
13284 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013285 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013288 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13289 return_list = get_tv_number_chk(&argvars[2], &error);
13290 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013293 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013294
13295 if (error)
13296 return;
13297
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 regname = (strregname == NULL ? '"' : *strregname);
13299 if (regname == 0)
13300 regname = '"';
13301
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013302 if (return_list)
13303 {
13304 rettv->v_type = VAR_LIST;
13305 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13306 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013307 if (rettv->vval.v_list != NULL)
13308 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013309 }
13310 else
13311 {
13312 rettv->v_type = VAR_STRING;
13313 rettv->vval.v_string = get_reg_contents(regname,
13314 arg2 ? GREG_EXPR_SRC : 0);
13315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316}
13317
13318/*
13319 * "getregtype()" function
13320 */
13321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013322f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323{
13324 char_u *strregname;
13325 int regname;
13326 char_u buf[NUMBUFLEN + 2];
13327 long reglen = 0;
13328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013329 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013330 {
13331 strregname = get_tv_string_chk(&argvars[0]);
13332 if (strregname == NULL) /* type error; errmsg already given */
13333 {
13334 rettv->v_type = VAR_STRING;
13335 rettv->vval.v_string = NULL;
13336 return;
13337 }
13338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 else
13340 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013341 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342
13343 regname = (strregname == NULL ? '"' : *strregname);
13344 if (regname == 0)
13345 regname = '"';
13346
13347 buf[0] = NUL;
13348 buf[1] = NUL;
13349 switch (get_reg_type(regname, &reglen))
13350 {
13351 case MLINE: buf[0] = 'V'; break;
13352 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353 case MBLOCK:
13354 buf[0] = Ctrl_V;
13355 sprintf((char *)buf + 1, "%ld", reglen + 1);
13356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013358 rettv->v_type = VAR_STRING;
13359 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360}
13361
13362/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013363 * "gettabvar()" function
13364 */
13365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013366f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013367{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013368 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013369 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013370 dictitem_T *v;
13371 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013372 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013373
13374 rettv->v_type = VAR_STRING;
13375 rettv->vval.v_string = NULL;
13376
13377 varname = get_tv_string_chk(&argvars[1]);
13378 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13379 if (tp != NULL && varname != NULL)
13380 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013381 /* Set tp to be our tabpage, temporarily. Also set the window to the
13382 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013383 if (switch_win(&oldcurwin, &oldtabpage,
13384 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013385 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013386 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013387 /* look up the variable */
13388 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13389 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13390 if (v != NULL)
13391 {
13392 copy_tv(&v->di_tv, rettv);
13393 done = TRUE;
13394 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013395 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013396
13397 /* restore previous notion of curwin */
13398 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013399 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013400
13401 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013402 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013403}
13404
13405/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013406 * "gettabwinvar()" function
13407 */
13408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013409f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013410{
13411 getwinvar(argvars, rettv, 1);
13412}
13413
13414/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 * "getwinposx()" function
13416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013418f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421#ifdef FEAT_GUI
13422 if (gui.in_use)
13423 {
13424 int x, y;
13425
13426 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013427 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 }
13429#endif
13430}
13431
13432/*
13433 * "getwinposy()" function
13434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013436f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef FEAT_GUI
13440 if (gui.in_use)
13441 {
13442 int x, y;
13443
13444 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013445 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446 }
13447#endif
13448}
13449
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013450/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013451 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013452 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013453 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013454find_win_by_nr(
13455 typval_T *vp,
13456 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013457{
13458#ifdef FEAT_WINDOWS
13459 win_T *wp;
13460#endif
13461 int nr;
13462
13463 nr = get_tv_number_chk(vp, NULL);
13464
13465#ifdef FEAT_WINDOWS
13466 if (nr < 0)
13467 return NULL;
13468 if (nr == 0)
13469 return curwin;
13470
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013471 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13472 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013473 if (--nr <= 0)
13474 break;
13475 return wp;
13476#else
13477 if (nr == 0 || nr == 1)
13478 return curwin;
13479 return NULL;
13480#endif
13481}
13482
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013484 * Find window specified by "wvp" in tabpage "tvp".
13485 */
13486 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013487find_tabwin(
13488 typval_T *wvp, /* VAR_UNKNOWN for current window */
13489 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013490{
13491 win_T *wp = NULL;
13492 tabpage_T *tp = NULL;
13493 long n;
13494
13495 if (wvp->v_type != VAR_UNKNOWN)
13496 {
13497 if (tvp->v_type != VAR_UNKNOWN)
13498 {
13499 n = get_tv_number(tvp);
13500 if (n >= 0)
13501 tp = find_tabpage(n);
13502 }
13503 else
13504 tp = curtab;
13505
13506 if (tp != NULL)
13507 wp = find_win_by_nr(wvp, tp);
13508 }
13509 else
13510 wp = curwin;
13511
13512 return wp;
13513}
13514
13515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 * "getwinvar()" function
13517 */
13518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013519f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013521 getwinvar(argvars, rettv, 0);
13522}
13523
13524/*
13525 * getwinvar() and gettabwinvar()
13526 */
13527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013528getwinvar(
13529 typval_T *argvars,
13530 typval_T *rettv,
13531 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013532{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013533 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013536 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013537 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013538#ifdef FEAT_WINDOWS
13539 win_T *oldcurwin;
13540 tabpage_T *oldtabpage;
13541 int need_switch_win;
13542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013544#ifdef FEAT_WINDOWS
13545 if (off == 1)
13546 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13547 else
13548 tp = curtab;
13549#endif
13550 win = find_win_by_nr(&argvars[off], tp);
13551 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013552 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013554 rettv->v_type = VAR_STRING;
13555 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556
13557 if (win != NULL && varname != NULL)
13558 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013559#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013560 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013561 * otherwise the window is not valid. Only do this when needed,
13562 * autocommands get blocked. */
13563 need_switch_win = !(tp == curtab && win == curwin);
13564 if (!need_switch_win
13565 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13566#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013567 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013568 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013569 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013570 if (get_option_tv(&varname, rettv, 1) == OK)
13571 done = TRUE;
13572 }
13573 else
13574 {
13575 /* Look up the variable. */
13576 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13577 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13578 varname, FALSE);
13579 if (v != NULL)
13580 {
13581 copy_tv(&v->di_tv, rettv);
13582 done = TRUE;
13583 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013586
Bram Moolenaarba117c22015-09-29 16:53:22 +020013587#ifdef FEAT_WINDOWS
13588 if (need_switch_win)
13589 /* restore previous notion of curwin */
13590 restore_win(oldcurwin, oldtabpage, TRUE);
13591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 }
13593
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013594 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13595 /* use the default return value */
13596 copy_tv(&argvars[off + 2], rettv);
13597
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 --emsg_off;
13599}
13600
13601/*
13602 * "glob()" function
13603 */
13604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013605f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013607 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013609 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013611 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013612 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013614 if (argvars[1].v_type != VAR_UNKNOWN)
13615 {
13616 if (get_tv_number_chk(&argvars[1], &error))
13617 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013618 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013619 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013620 if (get_tv_number_chk(&argvars[2], &error))
13621 {
13622 rettv->v_type = VAR_LIST;
13623 rettv->vval.v_list = NULL;
13624 }
13625 if (argvars[3].v_type != VAR_UNKNOWN
13626 && get_tv_number_chk(&argvars[3], &error))
13627 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013628 }
13629 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013630 if (!error)
13631 {
13632 ExpandInit(&xpc);
13633 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013634 if (p_wic)
13635 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013636 if (rettv->v_type == VAR_STRING)
13637 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013638 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013639 else if (rettv_list_alloc(rettv) != FAIL)
13640 {
13641 int i;
13642
13643 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13644 NULL, options, WILD_ALL_KEEP);
13645 for (i = 0; i < xpc.xp_numfiles; i++)
13646 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13647
13648 ExpandCleanup(&xpc);
13649 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013650 }
13651 else
13652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653}
13654
13655/*
13656 * "globpath()" function
13657 */
13658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013659f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013661 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013663 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013664 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013665 garray_T ga;
13666 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013668 /* When the optional second argument is non-zero, don't remove matches
13669 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013671 if (argvars[2].v_type != VAR_UNKNOWN)
13672 {
13673 if (get_tv_number_chk(&argvars[2], &error))
13674 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013675 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013676 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013677 if (get_tv_number_chk(&argvars[3], &error))
13678 {
13679 rettv->v_type = VAR_LIST;
13680 rettv->vval.v_list = NULL;
13681 }
13682 if (argvars[4].v_type != VAR_UNKNOWN
13683 && get_tv_number_chk(&argvars[4], &error))
13684 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013685 }
13686 }
13687 if (file != NULL && !error)
13688 {
13689 ga_init2(&ga, (int)sizeof(char_u *), 10);
13690 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13691 if (rettv->v_type == VAR_STRING)
13692 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13693 else if (rettv_list_alloc(rettv) != FAIL)
13694 for (i = 0; i < ga.ga_len; ++i)
13695 list_append_string(rettv->vval.v_list,
13696 ((char_u **)(ga.ga_data))[i], -1);
13697 ga_clear_strings(&ga);
13698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013700 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701}
13702
13703/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013704 * "glob2regpat()" function
13705 */
13706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013707f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013708{
13709 char_u *pat = get_tv_string_chk(&argvars[0]);
13710
13711 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013712 rettv->vval.v_string = (pat == NULL)
13713 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013714}
13715
13716/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 * "has()" function
13718 */
13719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013720f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721{
13722 int i;
13723 char_u *name;
13724 int n = FALSE;
13725 static char *(has_list[]) =
13726 {
13727#ifdef AMIGA
13728 "amiga",
13729# ifdef FEAT_ARP
13730 "arp",
13731# endif
13732#endif
13733#ifdef __BEOS__
13734 "beos",
13735#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013736#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 "mac",
13738#endif
13739#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013740 "macunix", /* built with 'darwin' enabled */
13741#endif
13742#if defined(__APPLE__) && __APPLE__ == 1
13743 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#ifdef __QNX__
13746 "qnx",
13747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#ifdef UNIX
13749 "unix",
13750#endif
13751#ifdef VMS
13752 "vms",
13753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#ifdef WIN32
13755 "win32",
13756#endif
13757#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13758 "win32unix",
13759#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013760#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761 "win64",
13762#endif
13763#ifdef EBCDIC
13764 "ebcdic",
13765#endif
13766#ifndef CASE_INSENSITIVE_FILENAME
13767 "fname_case",
13768#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013769#ifdef HAVE_ACL
13770 "acl",
13771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772#ifdef FEAT_ARABIC
13773 "arabic",
13774#endif
13775#ifdef FEAT_AUTOCMD
13776 "autocmd",
13777#endif
13778#ifdef FEAT_BEVAL
13779 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013780# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13781 "balloon_multiline",
13782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783#endif
13784#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13785 "builtin_terms",
13786# ifdef ALL_BUILTIN_TCAPS
13787 "all_builtin_terms",
13788# endif
13789#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013790#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13791 || defined(FEAT_GUI_W32) \
13792 || defined(FEAT_GUI_MOTIF))
13793 "browsefilter",
13794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795#ifdef FEAT_BYTEOFF
13796 "byte_offset",
13797#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013798#ifdef FEAT_CHANNEL
13799 "channel",
13800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801#ifdef FEAT_CINDENT
13802 "cindent",
13803#endif
13804#ifdef FEAT_CLIENTSERVER
13805 "clientserver",
13806#endif
13807#ifdef FEAT_CLIPBOARD
13808 "clipboard",
13809#endif
13810#ifdef FEAT_CMDL_COMPL
13811 "cmdline_compl",
13812#endif
13813#ifdef FEAT_CMDHIST
13814 "cmdline_hist",
13815#endif
13816#ifdef FEAT_COMMENTS
13817 "comments",
13818#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013819#ifdef FEAT_CONCEAL
13820 "conceal",
13821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822#ifdef FEAT_CRYPT
13823 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013824 "crypt-blowfish",
13825 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#endif
13827#ifdef FEAT_CSCOPE
13828 "cscope",
13829#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013830#ifdef FEAT_CURSORBIND
13831 "cursorbind",
13832#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013833#ifdef CURSOR_SHAPE
13834 "cursorshape",
13835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef DEBUG
13837 "debug",
13838#endif
13839#ifdef FEAT_CON_DIALOG
13840 "dialog_con",
13841#endif
13842#ifdef FEAT_GUI_DIALOG
13843 "dialog_gui",
13844#endif
13845#ifdef FEAT_DIFF
13846 "diff",
13847#endif
13848#ifdef FEAT_DIGRAPHS
13849 "digraphs",
13850#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013851#ifdef FEAT_DIRECTX
13852 "directx",
13853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854#ifdef FEAT_DND
13855 "dnd",
13856#endif
13857#ifdef FEAT_EMACS_TAGS
13858 "emacs_tags",
13859#endif
13860 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013861 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862#ifdef FEAT_SEARCH_EXTRA
13863 "extra_search",
13864#endif
13865#ifdef FEAT_FKMAP
13866 "farsi",
13867#endif
13868#ifdef FEAT_SEARCHPATH
13869 "file_in_path",
13870#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013871#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013872 "filterpipe",
13873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#ifdef FEAT_FIND_ID
13875 "find_in_path",
13876#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013877#ifdef FEAT_FLOAT
13878 "float",
13879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880#ifdef FEAT_FOLDING
13881 "folding",
13882#endif
13883#ifdef FEAT_FOOTER
13884 "footer",
13885#endif
13886#if !defined(USE_SYSTEM) && defined(UNIX)
13887 "fork",
13888#endif
13889#ifdef FEAT_GETTEXT
13890 "gettext",
13891#endif
13892#ifdef FEAT_GUI
13893 "gui",
13894#endif
13895#ifdef FEAT_GUI_ATHENA
13896# ifdef FEAT_GUI_NEXTAW
13897 "gui_neXtaw",
13898# else
13899 "gui_athena",
13900# endif
13901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902#ifdef FEAT_GUI_GTK
13903 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013904# ifdef USE_GTK3
13905 "gui_gtk3",
13906# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013910#ifdef FEAT_GUI_GNOME
13911 "gui_gnome",
13912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913#ifdef FEAT_GUI_MAC
13914 "gui_mac",
13915#endif
13916#ifdef FEAT_GUI_MOTIF
13917 "gui_motif",
13918#endif
13919#ifdef FEAT_GUI_PHOTON
13920 "gui_photon",
13921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922#ifdef FEAT_GUI_W32
13923 "gui_win32",
13924#endif
13925#ifdef FEAT_HANGULIN
13926 "hangul_input",
13927#endif
13928#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13929 "iconv",
13930#endif
13931#ifdef FEAT_INS_EXPAND
13932 "insert_expand",
13933#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013934#ifdef FEAT_JOB
13935 "job",
13936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937#ifdef FEAT_JUMPLIST
13938 "jumplist",
13939#endif
13940#ifdef FEAT_KEYMAP
13941 "keymap",
13942#endif
13943#ifdef FEAT_LANGMAP
13944 "langmap",
13945#endif
13946#ifdef FEAT_LIBCALL
13947 "libcall",
13948#endif
13949#ifdef FEAT_LINEBREAK
13950 "linebreak",
13951#endif
13952#ifdef FEAT_LISP
13953 "lispindent",
13954#endif
13955#ifdef FEAT_LISTCMDS
13956 "listcmds",
13957#endif
13958#ifdef FEAT_LOCALMAP
13959 "localmap",
13960#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013961#ifdef FEAT_LUA
13962# ifndef DYNAMIC_LUA
13963 "lua",
13964# endif
13965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013966#ifdef FEAT_MENU
13967 "menu",
13968#endif
13969#ifdef FEAT_SESSION
13970 "mksession",
13971#endif
13972#ifdef FEAT_MODIFY_FNAME
13973 "modify_fname",
13974#endif
13975#ifdef FEAT_MOUSE
13976 "mouse",
13977#endif
13978#ifdef FEAT_MOUSESHAPE
13979 "mouseshape",
13980#endif
13981#if defined(UNIX) || defined(VMS)
13982# ifdef FEAT_MOUSE_DEC
13983 "mouse_dec",
13984# endif
13985# ifdef FEAT_MOUSE_GPM
13986 "mouse_gpm",
13987# endif
13988# ifdef FEAT_MOUSE_JSB
13989 "mouse_jsbterm",
13990# endif
13991# ifdef FEAT_MOUSE_NET
13992 "mouse_netterm",
13993# endif
13994# ifdef FEAT_MOUSE_PTERM
13995 "mouse_pterm",
13996# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013997# ifdef FEAT_MOUSE_SGR
13998 "mouse_sgr",
13999# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014000# ifdef FEAT_SYSMOUSE
14001 "mouse_sysmouse",
14002# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014003# ifdef FEAT_MOUSE_URXVT
14004 "mouse_urxvt",
14005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006# ifdef FEAT_MOUSE_XTERM
14007 "mouse_xterm",
14008# endif
14009#endif
14010#ifdef FEAT_MBYTE
14011 "multi_byte",
14012#endif
14013#ifdef FEAT_MBYTE_IME
14014 "multi_byte_ime",
14015#endif
14016#ifdef FEAT_MULTI_LANG
14017 "multi_lang",
14018#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014019#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014020#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014021 "mzscheme",
14022#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024#ifdef FEAT_OLE
14025 "ole",
14026#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014027 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028#ifdef FEAT_PATH_EXTRA
14029 "path_extra",
14030#endif
14031#ifdef FEAT_PERL
14032#ifndef DYNAMIC_PERL
14033 "perl",
14034#endif
14035#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014036#ifdef FEAT_PERSISTENT_UNDO
14037 "persistent_undo",
14038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039#ifdef FEAT_PYTHON
14040#ifndef DYNAMIC_PYTHON
14041 "python",
14042#endif
14043#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014044#ifdef FEAT_PYTHON3
14045#ifndef DYNAMIC_PYTHON3
14046 "python3",
14047#endif
14048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049#ifdef FEAT_POSTSCRIPT
14050 "postscript",
14051#endif
14052#ifdef FEAT_PRINTER
14053 "printer",
14054#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014055#ifdef FEAT_PROFILE
14056 "profile",
14057#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014058#ifdef FEAT_RELTIME
14059 "reltime",
14060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061#ifdef FEAT_QUICKFIX
14062 "quickfix",
14063#endif
14064#ifdef FEAT_RIGHTLEFT
14065 "rightleft",
14066#endif
14067#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14068 "ruby",
14069#endif
14070#ifdef FEAT_SCROLLBIND
14071 "scrollbind",
14072#endif
14073#ifdef FEAT_CMDL_INFO
14074 "showcmd",
14075 "cmdline_info",
14076#endif
14077#ifdef FEAT_SIGNS
14078 "signs",
14079#endif
14080#ifdef FEAT_SMARTINDENT
14081 "smartindent",
14082#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014083#ifdef STARTUPTIME
14084 "startuptime",
14085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086#ifdef FEAT_STL_OPT
14087 "statusline",
14088#endif
14089#ifdef FEAT_SUN_WORKSHOP
14090 "sun_workshop",
14091#endif
14092#ifdef FEAT_NETBEANS_INTG
14093 "netbeans_intg",
14094#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014095#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014096 "spell",
14097#endif
14098#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 "syntax",
14100#endif
14101#if defined(USE_SYSTEM) || !defined(UNIX)
14102 "system",
14103#endif
14104#ifdef FEAT_TAG_BINS
14105 "tag_binary",
14106#endif
14107#ifdef FEAT_TAG_OLDSTATIC
14108 "tag_old_static",
14109#endif
14110#ifdef FEAT_TAG_ANYWHITE
14111 "tag_any_white",
14112#endif
14113#ifdef FEAT_TCL
14114# ifndef DYNAMIC_TCL
14115 "tcl",
14116# endif
14117#endif
14118#ifdef TERMINFO
14119 "terminfo",
14120#endif
14121#ifdef FEAT_TERMRESPONSE
14122 "termresponse",
14123#endif
14124#ifdef FEAT_TEXTOBJ
14125 "textobjects",
14126#endif
14127#ifdef HAVE_TGETENT
14128 "tgetent",
14129#endif
14130#ifdef FEAT_TITLE
14131 "title",
14132#endif
14133#ifdef FEAT_TOOLBAR
14134 "toolbar",
14135#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014136#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14137 "unnamedplus",
14138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139#ifdef FEAT_USR_CMDS
14140 "user-commands", /* was accidentally included in 5.4 */
14141 "user_commands",
14142#endif
14143#ifdef FEAT_VIMINFO
14144 "viminfo",
14145#endif
14146#ifdef FEAT_VERTSPLIT
14147 "vertsplit",
14148#endif
14149#ifdef FEAT_VIRTUALEDIT
14150 "virtualedit",
14151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153#ifdef FEAT_VISUALEXTRA
14154 "visualextra",
14155#endif
14156#ifdef FEAT_VREPLACE
14157 "vreplace",
14158#endif
14159#ifdef FEAT_WILDIGN
14160 "wildignore",
14161#endif
14162#ifdef FEAT_WILDMENU
14163 "wildmenu",
14164#endif
14165#ifdef FEAT_WINDOWS
14166 "windows",
14167#endif
14168#ifdef FEAT_WAK
14169 "winaltkeys",
14170#endif
14171#ifdef FEAT_WRITEBACKUP
14172 "writebackup",
14173#endif
14174#ifdef FEAT_XIM
14175 "xim",
14176#endif
14177#ifdef FEAT_XFONTSET
14178 "xfontset",
14179#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014180#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014181 "xpm",
14182 "xpm_w32", /* for backward compatibility */
14183#else
14184# if defined(HAVE_XPM)
14185 "xpm",
14186# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#ifdef USE_XSMP
14189 "xsmp",
14190#endif
14191#ifdef USE_XSMP_INTERACT
14192 "xsmp_interact",
14193#endif
14194#ifdef FEAT_XCLIPBOARD
14195 "xterm_clipboard",
14196#endif
14197#ifdef FEAT_XTERM_SAVE
14198 "xterm_save",
14199#endif
14200#if defined(UNIX) && defined(FEAT_X11)
14201 "X11",
14202#endif
14203 NULL
14204 };
14205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 for (i = 0; has_list[i] != NULL; ++i)
14208 if (STRICMP(name, has_list[i]) == 0)
14209 {
14210 n = TRUE;
14211 break;
14212 }
14213
14214 if (n == FALSE)
14215 {
14216 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014217 {
14218 if (name[5] == '-'
14219 && STRLEN(name) > 11
14220 && vim_isdigit(name[6])
14221 && vim_isdigit(name[8])
14222 && vim_isdigit(name[10]))
14223 {
14224 int major = atoi((char *)name + 6);
14225 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014226
14227 /* Expect "patch-9.9.01234". */
14228 n = (major < VIM_VERSION_MAJOR
14229 || (major == VIM_VERSION_MAJOR
14230 && (minor < VIM_VERSION_MINOR
14231 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014232 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014233 }
14234 else
14235 n = has_patch(atoi((char *)name + 5));
14236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 else if (STRICMP(name, "vim_starting") == 0)
14238 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014239#ifdef FEAT_MBYTE
14240 else if (STRICMP(name, "multi_byte_encoding") == 0)
14241 n = has_mbyte;
14242#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014243#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14244 else if (STRICMP(name, "balloon_multiline") == 0)
14245 n = multiline_balloon_available();
14246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247#ifdef DYNAMIC_TCL
14248 else if (STRICMP(name, "tcl") == 0)
14249 n = tcl_enabled(FALSE);
14250#endif
14251#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14252 else if (STRICMP(name, "iconv") == 0)
14253 n = iconv_enabled(FALSE);
14254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014255#ifdef DYNAMIC_LUA
14256 else if (STRICMP(name, "lua") == 0)
14257 n = lua_enabled(FALSE);
14258#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014259#ifdef DYNAMIC_MZSCHEME
14260 else if (STRICMP(name, "mzscheme") == 0)
14261 n = mzscheme_enabled(FALSE);
14262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263#ifdef DYNAMIC_RUBY
14264 else if (STRICMP(name, "ruby") == 0)
14265 n = ruby_enabled(FALSE);
14266#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014267#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268#ifdef DYNAMIC_PYTHON
14269 else if (STRICMP(name, "python") == 0)
14270 n = python_enabled(FALSE);
14271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014272#endif
14273#ifdef FEAT_PYTHON3
14274#ifdef DYNAMIC_PYTHON3
14275 else if (STRICMP(name, "python3") == 0)
14276 n = python3_enabled(FALSE);
14277#endif
14278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279#ifdef DYNAMIC_PERL
14280 else if (STRICMP(name, "perl") == 0)
14281 n = perl_enabled(FALSE);
14282#endif
14283#ifdef FEAT_GUI
14284 else if (STRICMP(name, "gui_running") == 0)
14285 n = (gui.in_use || gui.starting);
14286# ifdef FEAT_GUI_W32
14287 else if (STRICMP(name, "gui_win32s") == 0)
14288 n = gui_is_win32s();
14289# endif
14290# ifdef FEAT_BROWSE
14291 else if (STRICMP(name, "browse") == 0)
14292 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14293# endif
14294#endif
14295#ifdef FEAT_SYN_HL
14296 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014297 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298#endif
14299#if defined(WIN3264)
14300 else if (STRICMP(name, "win95") == 0)
14301 n = mch_windows95();
14302#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014303#ifdef FEAT_NETBEANS_INTG
14304 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014305 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 }
14308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310}
14311
14312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014313 * "has_key()" function
14314 */
14315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014316f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014317{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014318 if (argvars[0].v_type != VAR_DICT)
14319 {
14320 EMSG(_(e_dictreq));
14321 return;
14322 }
14323 if (argvars[0].vval.v_dict == NULL)
14324 return;
14325
14326 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014327 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328}
14329
14330/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014331 * "haslocaldir()" function
14332 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014334f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014335{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014336 win_T *wp = NULL;
14337
14338 wp = find_tabwin(&argvars[0], &argvars[1]);
14339 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014340}
14341
14342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 * "hasmapto()" function
14344 */
14345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014346f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347{
14348 char_u *name;
14349 char_u *mode;
14350 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014351 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 mode = (char_u *)"nvo";
14356 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014359 if (argvars[2].v_type != VAR_UNKNOWN)
14360 abbr = get_tv_number(&argvars[2]);
14361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar2c932302006-03-18 21:42:09 +000014363 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
14369/*
14370 * "histadd()" function
14371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375#ifdef FEAT_CMDHIST
14376 int histype;
14377 char_u *str;
14378 char_u buf[NUMBUFLEN];
14379#endif
14380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 if (check_restricted() || check_secure())
14383 return;
14384#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14386 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 if (histype >= 0)
14388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 if (*str != NUL)
14391 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014392 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 return;
14396 }
14397 }
14398#endif
14399}
14400
14401/*
14402 * "histdel()" function
14403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014405f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406{
14407#ifdef FEAT_CMDHIST
14408 int n;
14409 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014412 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14413 if (str == NULL)
14414 n = 0;
14415 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 else
14423 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 get_tv_string_buf(&argvars[1], buf));
14426 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427#endif
14428}
14429
14430/*
14431 * "histget()" function
14432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436#ifdef FEAT_CMDHIST
14437 int type;
14438 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14442 if (str == NULL)
14443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 {
14446 type = get_histtype(str);
14447 if (argvars[1].v_type == VAR_UNKNOWN)
14448 idx = get_history_idx(type);
14449 else
14450 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14451 /* -1 on type error */
14452 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458}
14459
14460/*
14461 * "histnr()" function
14462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
14466 int i;
14467
14468#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014469 char_u *history = get_tv_string_chk(&argvars[0]);
14470
14471 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 if (i >= HIST_CMD && i < HIST_COUNT)
14473 i = get_history_idx(i);
14474 else
14475#endif
14476 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
14480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 * "highlightID(name)" function
14482 */
14483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487}
14488
14489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 * "highlight_exists()" function
14491 */
14492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014493f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494{
14495 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14496}
14497
14498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 * "hostname()" function
14500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014502f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 char_u hostname[256];
14505
14506 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->v_type = VAR_STRING;
14508 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509}
14510
14511/*
14512 * iconv() function
14513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014515f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516{
14517#ifdef FEAT_MBYTE
14518 char_u buf1[NUMBUFLEN];
14519 char_u buf2[NUMBUFLEN];
14520 char_u *from, *to, *str;
14521 vimconv_T vimconv;
14522#endif
14523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->v_type = VAR_STRING;
14525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526
14527#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 str = get_tv_string(&argvars[0]);
14529 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14530 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 vimconv.vc_type = CONV_NONE;
14532 convert_setup(&vimconv, from, to);
14533
14534 /* If the encodings are equal, no conversion needed. */
14535 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
14540 convert_setup(&vimconv, NULL, NULL);
14541 vim_free(from);
14542 vim_free(to);
14543#endif
14544}
14545
14546/*
14547 * "indent()" function
14548 */
14549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014550f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551{
14552 linenr_T lnum;
14553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014561/*
14562 * "index()" function
14563 */
14564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014566{
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 list_T *l;
14568 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014569 long idx = 0;
14570 int ic = FALSE;
14571
14572 rettv->vval.v_number = -1;
14573 if (argvars[0].v_type != VAR_LIST)
14574 {
14575 EMSG(_(e_listreq));
14576 return;
14577 }
14578 l = argvars[0].vval.v_list;
14579 if (l != NULL)
14580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014581 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 int error = FALSE;
14585
Bram Moolenaar758711c2005-02-02 23:11:38 +000014586 /* Start at specified item. Use the cached index that list_find()
14587 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014589 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014591 ic = get_tv_number_chk(&argvars[3], &error);
14592 if (error)
14593 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014594 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014595
Bram Moolenaar758711c2005-02-02 23:11:38 +000014596 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014597 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014598 {
14599 rettv->vval.v_number = idx;
14600 break;
14601 }
14602 }
14603}
14604
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605static int inputsecret_flag = 0;
14606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014607static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014608
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014610 * This function is used by f_input() and f_inputdialog() functions. The third
14611 * argument to f_input() specifies the type of completion to use at the
14612 * prompt. The third argument to f_inputdialog() specifies the value to return
14613 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 */
14615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616get_user_input(
14617 typval_T *argvars,
14618 typval_T *rettv,
14619 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 char_u *p = NULL;
14623 int c;
14624 char_u buf[NUMBUFLEN];
14625 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014627 int xp_type = EXPAND_NOTHING;
14628 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
14633#ifdef NO_CONSOLE_INPUT
14634 /* While starting up, there is no place to enter text. */
14635 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637#endif
14638
14639 cmd_silent = FALSE; /* Want to see the prompt. */
14640 if (prompt != NULL)
14641 {
14642 /* Only the part of the message after the last NL is considered as
14643 * prompt for the command line */
14644 p = vim_strrchr(prompt, '\n');
14645 if (p == NULL)
14646 p = prompt;
14647 else
14648 {
14649 ++p;
14650 c = *p;
14651 *p = NUL;
14652 msg_start();
14653 msg_clr_eos();
14654 msg_puts_attr(prompt, echo_attr);
14655 msg_didout = FALSE;
14656 msg_starthere();
14657 *p = c;
14658 }
14659 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 if (argvars[1].v_type != VAR_UNKNOWN)
14662 {
14663 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14664 if (defstr != NULL)
14665 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014667 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014668 {
14669 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014670 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014671 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014672
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014673 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014674 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014675
Bram Moolenaar4463f292005-09-25 22:20:24 +000014676 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14677 if (xp_name == NULL)
14678 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014679
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014680 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014681
Bram Moolenaar4463f292005-09-25 22:20:24 +000014682 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14683 &xp_arg) == FAIL)
14684 return;
14685 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014686 }
14687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014689 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014690 int save_ex_normal_busy = ex_normal_busy;
14691 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014693 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14694 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014695 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014696 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014697 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014698 && argvars[1].v_type != VAR_UNKNOWN
14699 && argvars[2].v_type != VAR_UNKNOWN)
14700 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14701 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014702
14703 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 /* since the user typed this, no need to wait for return */
14706 need_wait_return = FALSE;
14707 msg_didout = FALSE;
14708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 cmd_silent = cmd_silent_save;
14710}
14711
14712/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014713 * "input()" function
14714 * Also handles inputsecret() when inputsecret is set.
14715 */
14716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014717f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014718{
14719 get_user_input(argvars, rettv, FALSE);
14720}
14721
14722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 * "inputdialog()" function
14724 */
14725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014726f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727{
14728#if defined(FEAT_GUI_TEXTDIALOG)
14729 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14730 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14731 {
14732 char_u *message;
14733 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014734 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014736 message = get_tv_string_chk(&argvars[0]);
14737 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014738 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014739 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 else
14741 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 if (message != NULL && defstr != NULL
14743 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014744 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 else
14747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 if (message != NULL && defstr != NULL
14749 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 rettv->vval.v_string = vim_strsave(
14752 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 }
14758 else
14759#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014760 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761}
14762
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014763/*
14764 * "inputlist()" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014768{
14769 listitem_T *li;
14770 int selected;
14771 int mouse_used;
14772
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014773#ifdef NO_CONSOLE_INPUT
14774 /* While starting up, there is no place to enter text. */
14775 if (no_console_input())
14776 return;
14777#endif
14778 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14779 {
14780 EMSG2(_(e_listarg), "inputlist()");
14781 return;
14782 }
14783
14784 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014785 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014786 lines_left = Rows; /* avoid more prompt */
14787 msg_scroll = TRUE;
14788 msg_clr_eos();
14789
14790 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14791 {
14792 msg_puts(get_tv_string(&li->li_tv));
14793 msg_putchar('\n');
14794 }
14795
14796 /* Ask for choice. */
14797 selected = prompt_for_number(&mouse_used);
14798 if (mouse_used)
14799 selected -= lines_left;
14800
14801 rettv->vval.v_number = selected;
14802}
14803
14804
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14806
14807/*
14808 * "inputrestore()" function
14809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014811f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812{
14813 if (ga_userinput.ga_len > 0)
14814 {
14815 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14817 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014818 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 }
14820 else if (p_verbose > 1)
14821 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014822 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 }
14825}
14826
14827/*
14828 * "inputsave()" function
14829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014833 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 if (ga_grow(&ga_userinput, 1) == OK)
14835 {
14836 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14837 + ga_userinput.ga_len);
14838 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014839 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 }
14841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843}
14844
14845/*
14846 * "inputsecret()" function
14847 */
14848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014849f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850{
14851 ++cmdline_star;
14852 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 --cmdline_star;
14855 --inputsecret_flag;
14856}
14857
14858/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014859 * "insert()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863{
14864 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 listitem_T *item;
14866 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014867 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868
14869 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014871 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014872 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 {
14874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875 before = get_tv_number_chk(&argvars[2], &error);
14876 if (error)
14877 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878
Bram Moolenaar758711c2005-02-02 23:11:38 +000014879 if (before == l->lv_len)
14880 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881 else
14882 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014883 item = list_find(l, before);
14884 if (item == NULL)
14885 {
14886 EMSGN(_(e_listidx), before);
14887 l = NULL;
14888 }
14889 }
14890 if (l != NULL)
14891 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014892 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014893 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014894 }
14895 }
14896}
14897
14898/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014899 * "invert(expr)" function
14900 */
14901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014902f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014903{
14904 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14905}
14906
14907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 * "isdirectory()" function
14909 */
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914}
14915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014916/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014917 * "islocked()" function
14918 */
14919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014921{
14922 lval_T lv;
14923 char_u *end;
14924 dictitem_T *di;
14925
14926 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014927 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14928 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014929 if (end != NULL && lv.ll_name != NULL)
14930 {
14931 if (*end != NUL)
14932 EMSG(_(e_trailing));
14933 else
14934 {
14935 if (lv.ll_tv == NULL)
14936 {
14937 if (check_changedtick(lv.ll_name))
14938 rettv->vval.v_number = 1; /* always locked */
14939 else
14940 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014941 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014942 if (di != NULL)
14943 {
14944 /* Consider a variable locked when:
14945 * 1. the variable itself is locked
14946 * 2. the value of the variable is locked.
14947 * 3. the List or Dict value is locked.
14948 */
14949 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14950 || tv_islocked(&di->di_tv));
14951 }
14952 }
14953 }
14954 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014955 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014956 else if (lv.ll_newkey != NULL)
14957 EMSG2(_(e_dictkey), lv.ll_newkey);
14958 else if (lv.ll_list != NULL)
14959 /* List item. */
14960 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14961 else
14962 /* Dictionary item. */
14963 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14964 }
14965 }
14966
14967 clear_lval(&lv);
14968}
14969
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014970#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14971/*
14972 * "isnan()" function
14973 */
14974 static void
14975f_isnan(typval_T *argvars, typval_T *rettv)
14976{
14977 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14978 && isnan(argvars[0].vval.v_float);
14979}
14980#endif
14981
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014982static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983
14984/*
14985 * Turn a dict into a list:
14986 * "what" == 0: list of keys
14987 * "what" == 1: list of values
14988 * "what" == 2: list of items
14989 */
14990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014992{
Bram Moolenaar33570922005-01-25 22:26:29 +000014993 list_T *l2;
14994 dictitem_T *di;
14995 hashitem_T *hi;
14996 listitem_T *li;
14997 listitem_T *li2;
14998 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014999 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015000
Bram Moolenaar8c711452005-01-14 21:53:12 +000015001 if (argvars[0].v_type != VAR_DICT)
15002 {
15003 EMSG(_(e_dictreq));
15004 return;
15005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015006 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015007 return;
15008
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015009 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015012 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015017 --todo;
15018 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015019
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015020 li = listitem_alloc();
15021 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015022 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015023 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 if (what == 0)
15026 {
15027 /* keys() */
15028 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015029 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015030 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15031 }
15032 else if (what == 1)
15033 {
15034 /* values() */
15035 copy_tv(&di->di_tv, &li->li_tv);
15036 }
15037 else
15038 {
15039 /* items() */
15040 l2 = list_alloc();
15041 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015042 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015043 li->li_tv.vval.v_list = l2;
15044 if (l2 == NULL)
15045 break;
15046 ++l2->lv_refcount;
15047
15048 li2 = listitem_alloc();
15049 if (li2 == NULL)
15050 break;
15051 list_append(l2, li2);
15052 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015053 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015054 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15055
15056 li2 = listitem_alloc();
15057 if (li2 == NULL)
15058 break;
15059 list_append(l2, li2);
15060 copy_tv(&di->di_tv, &li2->li_tv);
15061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015062 }
15063 }
15064}
15065
15066/*
15067 * "items(dict)" function
15068 */
15069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015070f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015071{
15072 dict_list(argvars, rettv, 2);
15073}
15074
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015075#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015076/*
15077 * Get the job from the argument.
15078 * Returns NULL if the job is invalid.
15079 */
15080 static job_T *
15081get_job_arg(typval_T *tv)
15082{
15083 job_T *job;
15084
15085 if (tv->v_type != VAR_JOB)
15086 {
15087 EMSG2(_(e_invarg2), get_tv_string(tv));
15088 return NULL;
15089 }
15090 job = tv->vval.v_job;
15091
15092 if (job == NULL)
15093 EMSG(_("E916: not a valid job"));
15094 return job;
15095}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015096
15097# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015099 * "job_getchannel()" function
15100 */
15101 static void
15102f_job_getchannel(typval_T *argvars, typval_T *rettv)
15103{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015104 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015105
Bram Moolenaar65edff82016-02-21 16:40:11 +010015106 if (job != NULL)
15107 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015108 rettv->v_type = VAR_CHANNEL;
15109 rettv->vval.v_channel = job->jv_channel;
15110 if (job->jv_channel != NULL)
15111 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015112 }
15113}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015114# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015115
15116/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015117 * "job_setoptions()" function
15118 */
15119 static void
15120f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15121{
15122 job_T *job = get_job_arg(&argvars[0]);
15123 jobopt_T opt;
15124
15125 if (job == NULL)
15126 return;
15127 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015128 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015129 return;
15130 job_set_options(job, &opt);
15131}
15132
15133/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015134 * "job_start()" function
15135 */
15136 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015137f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015138{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015139 job_T *job;
15140 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015141#if defined(UNIX)
15142# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015143 char **argv = NULL;
15144 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015145#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015146 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015148 jobopt_T opt;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015149 int part;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015150
15151 rettv->v_type = VAR_JOB;
15152 job = job_alloc();
15153 rettv->vval.v_job = job;
15154 if (job == NULL)
15155 return;
15156
15157 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015158
15159 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015160 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015161 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015162 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015163 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015164 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015165 return;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015166
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015167 /* Check that when io is "file" that there is a file name. */
15168 for (part = PART_OUT; part <= PART_IN; ++part)
15169 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
15170 && opt.jo_io[part] == JIO_FILE
15171 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
15172 || *opt.jo_io_name[part] == NUL))
15173 {
15174 EMSG(_("E920: -io file requires -name to be set"));
15175 return;
15176 }
15177
Bram Moolenaar014069a2016-03-03 22:51:40 +010015178 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15179 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015180 buf_T *buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015181
15182 /* check that we can find the buffer before starting the job */
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015183 if (opt.jo_set & JO_IN_BUF)
Bram Moolenaar014069a2016-03-03 22:51:40 +010015184 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015185 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
15186 if (buf == NULL)
15187 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015188 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015189 else if (!(opt.jo_set & JO_IN_NAME))
15190 {
15191 EMSG(_("E915: in-io buffer requires in-buf or in-name to be set"));
15192 }
15193 else
15194 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015195 if (buf == NULL)
15196 return;
15197 if (buf->b_ml.ml_mfp == NULL)
15198 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015199 char_u buf[NUMBUFLEN];
15200 char_u *s;
15201
15202 if (opt.jo_set & JO_IN_BUF)
15203 {
15204 sprintf((char *)buf, "%d", opt.jo_io_buf[PART_IN]);
15205 s = buf;
15206 }
15207 else
15208 s = opt.jo_io_name[PART_IN];
15209 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015210 return;
15211 }
15212 job->jv_in_buf = buf;
15213 }
15214
Bram Moolenaar65edff82016-02-21 16:40:11 +010015215 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015216
Bram Moolenaar835dc632016-02-07 14:27:38 +010015217#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015218 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015219#endif
15220
15221 if (argvars[0].v_type == VAR_STRING)
15222 {
15223 /* Command is a string. */
15224 cmd = argvars[0].vval.v_string;
15225#ifdef USE_ARGV
15226 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15227 return;
15228 argv[argc] = NULL;
15229#endif
15230 }
15231 else if (argvars[0].v_type != VAR_LIST
15232 || argvars[0].vval.v_list == NULL
15233 || argvars[0].vval.v_list->lv_len < 1)
15234 {
15235 EMSG(_(e_invarg));
15236 return;
15237 }
15238 else
15239 {
15240 list_T *l = argvars[0].vval.v_list;
15241 listitem_T *li;
15242 char_u *s;
15243
15244#ifdef USE_ARGV
15245 /* Pass argv[] to mch_call_shell(). */
15246 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15247 if (argv == NULL)
15248 return;
15249#endif
15250 for (li = l->lv_first; li != NULL; li = li->li_next)
15251 {
15252 s = get_tv_string_chk(&li->li_tv);
15253 if (s == NULL)
15254 goto theend;
15255#ifdef USE_ARGV
15256 argv[argc++] = (char *)s;
15257#else
Bram Moolenaara86f14a2016-02-29 21:05:48 +010015258 /* Only escape when needed, double quotes are not always allowed. */
15259 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015260 {
15261 s = vim_strsave_shellescape(s, FALSE, TRUE);
15262 if (s == NULL)
15263 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015264 ga_concat(&ga, s);
15265 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015266 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015267 else
15268 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015269 if (li->li_next != NULL)
15270 ga_append(&ga, ' ');
15271#endif
15272 }
15273#ifdef USE_ARGV
15274 argv[argc] = NULL;
15275#else
15276 cmd = ga.ga_data;
15277#endif
15278 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015279
Bram Moolenaar835dc632016-02-07 14:27:38 +010015280#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015281# ifdef FEAT_CHANNEL
15282 if (ch_log_active())
15283 {
15284 garray_T ga;
15285 int i;
15286
15287 ga_init2(&ga, (int)sizeof(char), 200);
15288 for (i = 0; i < argc; ++i)
15289 {
15290 if (i > 0)
15291 ga_concat(&ga, (char_u *)" ");
15292 ga_concat(&ga, (char_u *)argv[i]);
15293 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015294 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015295 ga_clear(&ga);
15296 }
15297# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015298 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015299#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015300# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015301 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015302# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015303 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015304#endif
15305
Bram Moolenaar014069a2016-03-03 22:51:40 +010015306#ifdef FEAT_CHANNEL
Bram Moolenaar839fd112016-03-06 21:34:03 +010015307 /* If the channel is reading from a buffer, write lines now. */
Bram Moolenaar4e329fc2016-03-07 15:24:03 +010015308 if (job->jv_channel != NULL)
15309 channel_write_in(job->jv_channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015310#endif
15311
Bram Moolenaar835dc632016-02-07 14:27:38 +010015312theend:
15313#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015314 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015315#else
15316 vim_free(ga.ga_data);
15317#endif
15318}
15319
15320/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015321 * Get the status of "job" and invoke the exit callback when needed.
15322 * The returned string is not allocated.
15323 */
15324 static char *
15325job_status(job_T *job)
15326{
15327 char *result;
15328
15329 if (job->jv_status == JOB_ENDED)
15330 /* No need to check, dead is dead. */
15331 result = "dead";
15332 else if (job->jv_status == JOB_FAILED)
15333 result = "fail";
15334 else
15335 {
15336 result = mch_job_status(job);
15337# ifdef FEAT_CHANNEL
15338 if (job->jv_status == JOB_ENDED)
15339 ch_log(job->jv_channel, "Job ended");
15340# endif
15341 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15342 {
15343 typval_T argv[3];
15344 typval_T rettv;
15345 int dummy;
15346
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015347 /* invoke the exit callback; make sure the refcount is > 0 */
15348 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015349 argv[0].v_type = VAR_JOB;
15350 argv[0].vval.v_job = job;
15351 argv[1].v_type = VAR_NUMBER;
15352 argv[1].vval.v_number = job->jv_exitval;
15353 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15354 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15355 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015356 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015357 }
15358 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15359 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015360 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015361 * freed. Careful: caller must not use "job" after this! */
15362 job_free(job);
15363 }
15364 }
15365 return result;
15366}
15367
15368/*
15369 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15370 */
15371 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015372job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015373{
15374 static time_t last_check = 0;
15375 time_t now;
15376 job_T *job;
15377 job_T *next;
15378
15379 /* Only do this once in 10 seconds. */
15380 now = time(NULL);
15381 if (last_check + 10 < now)
15382 {
15383 last_check = now;
15384 for (job = first_job; job != NULL; job = next)
15385 {
15386 next = job->jv_next;
15387 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15388 job_status(job); /* may free "job" */
15389 }
15390 }
15391}
15392
15393/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015394 * "job_status()" function
15395 */
15396 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015397f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015398{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015399 job_T *job = get_job_arg(&argvars[0]);
15400 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015401
Bram Moolenaar65edff82016-02-21 16:40:11 +010015402 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015403 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015404 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015405 rettv->v_type = VAR_STRING;
15406 rettv->vval.v_string = vim_strsave((char_u *)result);
15407 }
15408}
15409
15410/*
15411 * "job_stop()" function
15412 */
15413 static void
15414f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15415{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015416 job_T *job = get_job_arg(&argvars[0]);
15417
15418 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015419 {
15420 char_u *arg;
15421
15422 if (argvars[1].v_type == VAR_UNKNOWN)
15423 arg = (char_u *)"";
15424 else
15425 {
15426 arg = get_tv_string_chk(&argvars[1]);
15427 if (arg == NULL)
15428 {
15429 EMSG(_(e_invarg));
15430 return;
15431 }
15432 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015433# ifdef FEAT_CHANNEL
15434 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15435# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015436 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015437 rettv->vval.v_number = 0;
15438 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015439 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015440 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015441 /* Assume that "hup" does not kill the job. */
15442 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15443 job->jv_channel->ch_job_killed = TRUE;
15444 }
15445 /* We don't try freeing the job, obviously the caller still has a
15446 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015447 }
15448}
15449#endif
15450
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015452 * "join()" function
15453 */
15454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015455f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015456{
15457 garray_T ga;
15458 char_u *sep;
15459
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015460 if (argvars[0].v_type != VAR_LIST)
15461 {
15462 EMSG(_(e_listreq));
15463 return;
15464 }
15465 if (argvars[0].vval.v_list == NULL)
15466 return;
15467 if (argvars[1].v_type == VAR_UNKNOWN)
15468 sep = (char_u *)" ";
15469 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015471
15472 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015473
15474 if (sep != NULL)
15475 {
15476 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015477 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015478 ga_append(&ga, NUL);
15479 rettv->vval.v_string = (char_u *)ga.ga_data;
15480 }
15481 else
15482 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015483}
15484
15485/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015486 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015487 */
15488 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015489f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015490{
15491 js_read_T reader;
15492
15493 reader.js_buf = get_tv_string(&argvars[0]);
15494 reader.js_fill = NULL;
15495 reader.js_used = 0;
15496 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15497 EMSG(_(e_invarg));
15498}
15499
15500/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015501 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015502 */
15503 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015504f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015505{
15506 rettv->v_type = VAR_STRING;
15507 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15508}
15509
15510/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015511 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015512 */
15513 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015514f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015515{
15516 js_read_T reader;
15517
15518 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015519 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015520 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015521 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015522 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015523}
15524
15525/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015526 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015527 */
15528 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015529f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015530{
15531 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015532 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015533}
15534
15535/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015536 * "keys()" function
15537 */
15538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015539f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015540{
15541 dict_list(argvars, rettv, 0);
15542}
15543
15544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 * "last_buffer_nr()" function.
15546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549{
15550 int n = 0;
15551 buf_T *buf;
15552
15553 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15554 if (n < buf->b_fnum)
15555 n = buf->b_fnum;
15556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015558}
15559
15560/*
15561 * "len()" function
15562 */
15563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015564f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015565{
15566 switch (argvars[0].v_type)
15567 {
15568 case VAR_STRING:
15569 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 rettv->vval.v_number = (varnumber_T)STRLEN(
15571 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015572 break;
15573 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015575 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015576 case VAR_DICT:
15577 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15578 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015579 case VAR_UNKNOWN:
15580 case VAR_SPECIAL:
15581 case VAR_FLOAT:
15582 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015584 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015585 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 break;
15587 }
15588}
15589
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015590static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015591
15592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015593libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015594{
15595#ifdef FEAT_LIBCALL
15596 char_u *string_in;
15597 char_u **string_result;
15598 int nr_result;
15599#endif
15600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015601 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015602 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015603 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015604
15605 if (check_restricted() || check_secure())
15606 return;
15607
15608#ifdef FEAT_LIBCALL
15609 /* The first two args must be strings, otherwise its meaningless */
15610 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15611 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015612 string_in = NULL;
15613 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015614 string_in = argvars[2].vval.v_string;
15615 if (type == VAR_NUMBER)
15616 string_result = NULL;
15617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015619 if (mch_libcall(argvars[0].vval.v_string,
15620 argvars[1].vval.v_string,
15621 string_in,
15622 argvars[2].vval.v_number,
15623 string_result,
15624 &nr_result) == OK
15625 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015627 }
15628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629}
15630
15631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632 * "libcall()" function
15633 */
15634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015635f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636{
15637 libcall_common(argvars, rettv, VAR_STRING);
15638}
15639
15640/*
15641 * "libcallnr()" function
15642 */
15643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015644f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645{
15646 libcall_common(argvars, rettv, VAR_NUMBER);
15647}
15648
15649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 * "line(string)" function
15651 */
15652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654{
15655 linenr_T lnum = 0;
15656 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015657 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015659 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 if (fp != NULL)
15661 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015662 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663}
15664
15665/*
15666 * "line2byte(lnum)" function
15667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015669f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670{
15671#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673#else
15674 linenr_T lnum;
15675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015676 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15681 if (rettv->vval.v_number >= 0)
15682 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683#endif
15684}
15685
15686/*
15687 * "lispindent(lnum)" function
15688 */
15689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015690f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691{
15692#ifdef FEAT_LISP
15693 pos_T pos;
15694 linenr_T lnum;
15695
15696 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15699 {
15700 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 curwin->w_cursor = pos;
15703 }
15704 else
15705#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707}
15708
15709/*
15710 * "localtime()" function
15711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716}
15717
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015718static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719
15720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015721get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722{
15723 char_u *keys;
15724 char_u *which;
15725 char_u buf[NUMBUFLEN];
15726 char_u *keys_buf = NULL;
15727 char_u *rhs;
15728 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015729 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015730 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015731 mapblock_T *mp;
15732 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
15734 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015735 rettv->v_type = VAR_STRING;
15736 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 if (*keys == NUL)
15740 return;
15741
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015742 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015744 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015745 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015746 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015747 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015748 if (argvars[3].v_type != VAR_UNKNOWN)
15749 get_dict = get_tv_number(&argvars[3]);
15750 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 else
15753 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015754 if (which == NULL)
15755 return;
15756
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 mode = get_map_mode(&which, 0);
15758
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015759 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015760 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015762
15763 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015765 /* Return a string. */
15766 if (rhs != NULL)
15767 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768
Bram Moolenaarbd743252010-10-20 21:23:33 +020015769 }
15770 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15771 {
15772 /* Return a dictionary. */
15773 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15774 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15775 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776
Bram Moolenaarbd743252010-10-20 21:23:33 +020015777 dict_add_nr_str(dict, "lhs", 0L, lhs);
15778 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15779 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15780 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15781 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15782 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15783 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015784 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015785 dict_add_nr_str(dict, "mode", 0L, mapmode);
15786
15787 vim_free(lhs);
15788 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 }
15790}
15791
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015792#ifdef FEAT_FLOAT
15793/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015794 * "log()" function
15795 */
15796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015797f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015798{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015799 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015800
15801 rettv->v_type = VAR_FLOAT;
15802 if (get_float_arg(argvars, &f) == OK)
15803 rettv->vval.v_float = log(f);
15804 else
15805 rettv->vval.v_float = 0.0;
15806}
15807
15808/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015809 * "log10()" function
15810 */
15811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015812f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015813{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015814 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015815
15816 rettv->v_type = VAR_FLOAT;
15817 if (get_float_arg(argvars, &f) == OK)
15818 rettv->vval.v_float = log10(f);
15819 else
15820 rettv->vval.v_float = 0.0;
15821}
15822#endif
15823
Bram Moolenaar1dced572012-04-05 16:54:08 +020015824#ifdef FEAT_LUA
15825/*
15826 * "luaeval()" function
15827 */
15828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015829f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015830{
15831 char_u *str;
15832 char_u buf[NUMBUFLEN];
15833
15834 str = get_tv_string_buf(&argvars[0], buf);
15835 do_luaeval(str, argvars + 1, rettv);
15836}
15837#endif
15838
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 * "map()" function
15841 */
15842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015843f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015844{
15845 filter_map(argvars, rettv, TRUE);
15846}
15847
15848/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015849 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 */
15851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015852f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855}
15856
15857/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015858 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859 */
15860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015861f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864}
15865
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015866static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867
15868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015869find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015871 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015872 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015873 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 char_u *pat;
15875 regmatch_T regmatch;
15876 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015877 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878 char_u *save_cpo;
15879 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015880 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015881 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015882 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015883 list_T *l = NULL;
15884 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015885 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015886 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887
15888 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15889 save_cpo = p_cpo;
15890 p_cpo = (char_u *)"";
15891
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015892 rettv->vval.v_number = -1;
15893 if (type == 3)
15894 {
15895 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015896 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015897 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015898 }
15899 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015901 rettv->v_type = VAR_STRING;
15902 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015905 if (argvars[0].v_type == VAR_LIST)
15906 {
15907 if ((l = argvars[0].vval.v_list) == NULL)
15908 goto theend;
15909 li = l->lv_first;
15910 }
15911 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015912 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015913 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015914 len = (long)STRLEN(str);
15915 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015917 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15918 if (pat == NULL)
15919 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015920
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015921 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015923 int error = FALSE;
15924
15925 start = get_tv_number_chk(&argvars[2], &error);
15926 if (error)
15927 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015928 if (l != NULL)
15929 {
15930 li = list_find(l, start);
15931 if (li == NULL)
15932 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015933 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015934 }
15935 else
15936 {
15937 if (start < 0)
15938 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015939 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015940 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015941 /* When "count" argument is there ignore matches before "start",
15942 * otherwise skip part of the string. Differs when pattern is "^"
15943 * or "\<". */
15944 if (argvars[3].v_type != VAR_UNKNOWN)
15945 startcol = start;
15946 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015947 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015948 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015949 len -= start;
15950 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015951 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015953 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 nth = get_tv_number_chk(&argvars[3], &error);
15955 if (error)
15956 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 }
15958
15959 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15960 if (regmatch.regprog != NULL)
15961 {
15962 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015963
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015964 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015965 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015966 if (l != NULL)
15967 {
15968 if (li == NULL)
15969 {
15970 match = FALSE;
15971 break;
15972 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015973 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015974 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015975 if (str == NULL)
15976 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015977 }
15978
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015979 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015980
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015981 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015982 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015983 if (l == NULL && !match)
15984 break;
15985
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015986 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015987 if (l != NULL)
15988 {
15989 li = li->li_next;
15990 ++idx;
15991 }
15992 else
15993 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015994#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015995 startcol = (colnr_T)(regmatch.startp[0]
15996 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015997#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015998 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015999#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016000 if (startcol > (colnr_T)len
16001 || str + startcol <= regmatch.startp[0])
16002 {
16003 match = FALSE;
16004 break;
16005 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016006 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016007 }
16008
16009 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016011 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016012 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016013 int i;
16014
16015 /* return list with matched string and submatches */
16016 for (i = 0; i < NSUBEXP; ++i)
16017 {
16018 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016019 {
16020 if (list_append_string(rettv->vval.v_list,
16021 (char_u *)"", 0) == FAIL)
16022 break;
16023 }
16024 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016025 regmatch.startp[i],
16026 (int)(regmatch.endp[i] - regmatch.startp[i]))
16027 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016028 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 }
16030 }
16031 else if (type == 2)
16032 {
16033 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016034 if (l != NULL)
16035 copy_tv(&li->li_tv, rettv);
16036 else
16037 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016039 }
16040 else if (l != NULL)
16041 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 else
16043 {
16044 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016045 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 (varnumber_T)(regmatch.startp[0] - str);
16047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016050 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 }
16052 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016053 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 }
16055
16056theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016057 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 p_cpo = save_cpo;
16059}
16060
16061/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062 * "match()" function
16063 */
16064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016065f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016066{
16067 find_some_match(argvars, rettv, 1);
16068}
16069
16070/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016071 * "matchadd()" function
16072 */
16073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016074f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016075{
16076#ifdef FEAT_SEARCH_EXTRA
16077 char_u buf[NUMBUFLEN];
16078 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16079 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16080 int prio = 10; /* default priority */
16081 int id = -1;
16082 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016083 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016084
16085 rettv->vval.v_number = -1;
16086
16087 if (grp == NULL || pat == NULL)
16088 return;
16089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016090 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016091 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016092 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016093 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016094 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016095 if (argvars[4].v_type != VAR_UNKNOWN)
16096 {
16097 if (argvars[4].v_type != VAR_DICT)
16098 {
16099 EMSG(_(e_dictreq));
16100 return;
16101 }
16102 if (dict_find(argvars[4].vval.v_dict,
16103 (char_u *)"conceal", -1) != NULL)
16104 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16105 (char_u *)"conceal", FALSE);
16106 }
16107 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016108 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016109 if (error == TRUE)
16110 return;
16111 if (id >= 1 && id <= 3)
16112 {
16113 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16114 return;
16115 }
16116
Bram Moolenaar6561d522015-07-21 15:48:27 +020016117 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16118 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016119#endif
16120}
16121
16122/*
16123 * "matchaddpos()" function
16124 */
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016127{
16128#ifdef FEAT_SEARCH_EXTRA
16129 char_u buf[NUMBUFLEN];
16130 char_u *group;
16131 int prio = 10;
16132 int id = -1;
16133 int error = FALSE;
16134 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016135 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016136
16137 rettv->vval.v_number = -1;
16138
16139 group = get_tv_string_buf_chk(&argvars[0], buf);
16140 if (group == NULL)
16141 return;
16142
16143 if (argvars[1].v_type != VAR_LIST)
16144 {
16145 EMSG2(_(e_listarg), "matchaddpos()");
16146 return;
16147 }
16148 l = argvars[1].vval.v_list;
16149 if (l == NULL)
16150 return;
16151
16152 if (argvars[2].v_type != VAR_UNKNOWN)
16153 {
16154 prio = get_tv_number_chk(&argvars[2], &error);
16155 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016156 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016157 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016158 if (argvars[4].v_type != VAR_UNKNOWN)
16159 {
16160 if (argvars[4].v_type != VAR_DICT)
16161 {
16162 EMSG(_(e_dictreq));
16163 return;
16164 }
16165 if (dict_find(argvars[4].vval.v_dict,
16166 (char_u *)"conceal", -1) != NULL)
16167 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16168 (char_u *)"conceal", FALSE);
16169 }
16170 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016171 }
16172 if (error == TRUE)
16173 return;
16174
16175 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16176 if (id == 1 || id == 2)
16177 {
16178 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16179 return;
16180 }
16181
Bram Moolenaar6561d522015-07-21 15:48:27 +020016182 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16183 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016184#endif
16185}
16186
16187/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016188 * "matcharg()" function
16189 */
16190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016191f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016192{
16193 if (rettv_list_alloc(rettv) == OK)
16194 {
16195#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016196 int id = get_tv_number(&argvars[0]);
16197 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016198
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016199 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016200 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016201 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16202 {
16203 list_append_string(rettv->vval.v_list,
16204 syn_id2name(m->hlg_id), -1);
16205 list_append_string(rettv->vval.v_list, m->pattern, -1);
16206 }
16207 else
16208 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016209 list_append_string(rettv->vval.v_list, NULL, -1);
16210 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016211 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016212 }
16213#endif
16214 }
16215}
16216
16217/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016218 * "matchdelete()" function
16219 */
16220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016222{
16223#ifdef FEAT_SEARCH_EXTRA
16224 rettv->vval.v_number = match_delete(curwin,
16225 (int)get_tv_number(&argvars[0]), TRUE);
16226#endif
16227}
16228
16229/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230 * "matchend()" function
16231 */
16232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016233f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016234{
16235 find_some_match(argvars, rettv, 0);
16236}
16237
16238/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016239 * "matchlist()" function
16240 */
16241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016243{
16244 find_some_match(argvars, rettv, 3);
16245}
16246
16247/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248 * "matchstr()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016252{
16253 find_some_match(argvars, rettv, 2);
16254}
16255
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016256static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016257
16258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016259max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016260{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016261 long n = 0;
16262 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016264
16265 if (argvars[0].v_type == VAR_LIST)
16266 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 list_T *l;
16268 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016269
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016270 l = argvars[0].vval.v_list;
16271 if (l != NULL)
16272 {
16273 li = l->lv_first;
16274 if (li != NULL)
16275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016276 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016277 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016278 {
16279 li = li->li_next;
16280 if (li == NULL)
16281 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016282 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016283 if (domax ? i > n : i < n)
16284 n = i;
16285 }
16286 }
16287 }
16288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016289 else if (argvars[0].v_type == VAR_DICT)
16290 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016291 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016292 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016293 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016294 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016295
16296 d = argvars[0].vval.v_dict;
16297 if (d != NULL)
16298 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016299 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016300 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016302 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016304 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016305 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016306 if (first)
16307 {
16308 n = i;
16309 first = FALSE;
16310 }
16311 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016312 n = i;
16313 }
16314 }
16315 }
16316 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016317 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016318 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016319 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016320}
16321
16322/*
16323 * "max()" function
16324 */
16325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016326f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016327{
16328 max_min(argvars, rettv, TRUE);
16329}
16330
16331/*
16332 * "min()" function
16333 */
16334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016335f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016336{
16337 max_min(argvars, rettv, FALSE);
16338}
16339
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016340static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016341
16342/*
16343 * Create the directory in which "dir" is located, and higher levels when
16344 * needed.
16345 */
16346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016347mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016348{
16349 char_u *p;
16350 char_u *updir;
16351 int r = FAIL;
16352
16353 /* Get end of directory name in "dir".
16354 * We're done when it's "/" or "c:/". */
16355 p = gettail_sep(dir);
16356 if (p <= get_past_head(dir))
16357 return OK;
16358
16359 /* If the directory exists we're done. Otherwise: create it.*/
16360 updir = vim_strnsave(dir, (int)(p - dir));
16361 if (updir == NULL)
16362 return FAIL;
16363 if (mch_isdir(updir))
16364 r = OK;
16365 else if (mkdir_recurse(updir, prot) == OK)
16366 r = vim_mkdir_emsg(updir, prot);
16367 vim_free(updir);
16368 return r;
16369}
16370
16371#ifdef vim_mkdir
16372/*
16373 * "mkdir()" function
16374 */
16375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016376f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016377{
16378 char_u *dir;
16379 char_u buf[NUMBUFLEN];
16380 int prot = 0755;
16381
16382 rettv->vval.v_number = FAIL;
16383 if (check_restricted() || check_secure())
16384 return;
16385
16386 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016387 if (*dir == NUL)
16388 rettv->vval.v_number = FAIL;
16389 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016390 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016391 if (*gettail(dir) == NUL)
16392 /* remove trailing slashes */
16393 *gettail_sep(dir) = NUL;
16394
16395 if (argvars[1].v_type != VAR_UNKNOWN)
16396 {
16397 if (argvars[2].v_type != VAR_UNKNOWN)
16398 prot = get_tv_number_chk(&argvars[2], NULL);
16399 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16400 mkdir_recurse(dir, prot);
16401 }
16402 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016403 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016404}
16405#endif
16406
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408 * "mode()" function
16409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016413 char_u buf[3];
16414
16415 buf[1] = NUL;
16416 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 if (VIsual_active)
16419 {
16420 if (VIsual_select)
16421 buf[0] = VIsual_mode + 's' - 'v';
16422 else
16423 buf[0] = VIsual_mode;
16424 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016425 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016426 || State == CONFIRM)
16427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016429 if (State == ASKMORE)
16430 buf[1] = 'm';
16431 else if (State == CONFIRM)
16432 buf[1] = '?';
16433 }
16434 else if (State == EXTERNCMD)
16435 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436 else if (State & INSERT)
16437 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016438#ifdef FEAT_VREPLACE
16439 if (State & VREPLACE_FLAG)
16440 {
16441 buf[0] = 'R';
16442 buf[1] = 'v';
16443 }
16444 else
16445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 if (State & REPLACE_FLAG)
16447 buf[0] = 'R';
16448 else
16449 buf[0] = 'i';
16450 }
16451 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016452 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454 if (exmode_active)
16455 buf[1] = 'v';
16456 }
16457 else if (exmode_active)
16458 {
16459 buf[0] = 'c';
16460 buf[1] = 'e';
16461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016465 if (finish_op)
16466 buf[1] = 'o';
16467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016469 /* Clear out the minor mode when the argument is not a non-zero number or
16470 * non-empty string. */
16471 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016472 buf[1] = NUL;
16473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016474 rettv->vval.v_string = vim_strsave(buf);
16475 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476}
16477
Bram Moolenaar429fa852013-04-15 12:27:36 +020016478#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016479/*
16480 * "mzeval()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016484{
16485 char_u *str;
16486 char_u buf[NUMBUFLEN];
16487
16488 str = get_tv_string_buf(&argvars[0], buf);
16489 do_mzeval(str, rettv);
16490}
Bram Moolenaar75676462013-01-30 14:55:42 +010016491
16492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016493mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016494{
16495 typval_T argvars[3];
16496
16497 argvars[0].v_type = VAR_STRING;
16498 argvars[0].vval.v_string = name;
16499 copy_tv(args, &argvars[1]);
16500 argvars[2].v_type = VAR_UNKNOWN;
16501 f_call(argvars, rettv);
16502 clear_tv(&argvars[1]);
16503}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016504#endif
16505
Bram Moolenaar071d4272004-06-13 20:20:40 +000016506/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507 * "nextnonblank()" function
16508 */
16509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016510f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511{
16512 linenr_T lnum;
16513
16514 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016516 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 {
16518 lnum = 0;
16519 break;
16520 }
16521 if (*skipwhite(ml_get(lnum)) != NUL)
16522 break;
16523 }
16524 rettv->vval.v_number = lnum;
16525}
16526
16527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016528 * "nr2char()" function
16529 */
16530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016531f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532{
16533 char_u buf[NUMBUFLEN];
16534
16535#ifdef FEAT_MBYTE
16536 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016537 {
16538 int utf8 = 0;
16539
16540 if (argvars[1].v_type != VAR_UNKNOWN)
16541 utf8 = get_tv_number_chk(&argvars[1], NULL);
16542 if (utf8)
16543 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16544 else
16545 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016547 else
16548#endif
16549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016550 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016551 buf[1] = NUL;
16552 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016553 rettv->v_type = VAR_STRING;
16554 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016555}
16556
16557/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016558 * "or(expr, expr)" function
16559 */
16560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016561f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016562{
16563 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16564 | get_tv_number_chk(&argvars[1], NULL);
16565}
16566
16567/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016568 * "pathshorten()" function
16569 */
16570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016571f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016572{
16573 char_u *p;
16574
16575 rettv->v_type = VAR_STRING;
16576 p = get_tv_string_chk(&argvars[0]);
16577 if (p == NULL)
16578 rettv->vval.v_string = NULL;
16579 else
16580 {
16581 p = vim_strsave(p);
16582 rettv->vval.v_string = p;
16583 if (p != NULL)
16584 shorten_dir(p);
16585 }
16586}
16587
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016588#ifdef FEAT_PERL
16589/*
16590 * "perleval()" function
16591 */
16592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016593f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016594{
16595 char_u *str;
16596 char_u buf[NUMBUFLEN];
16597
16598 str = get_tv_string_buf(&argvars[0], buf);
16599 do_perleval(str, rettv);
16600}
16601#endif
16602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016603#ifdef FEAT_FLOAT
16604/*
16605 * "pow()" function
16606 */
16607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016608f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016609{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016610 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016611
16612 rettv->v_type = VAR_FLOAT;
16613 if (get_float_arg(argvars, &fx) == OK
16614 && get_float_arg(&argvars[1], &fy) == OK)
16615 rettv->vval.v_float = pow(fx, fy);
16616 else
16617 rettv->vval.v_float = 0.0;
16618}
16619#endif
16620
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016621/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016622 * "prevnonblank()" function
16623 */
16624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016625f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626{
16627 linenr_T lnum;
16628
16629 lnum = get_tv_lnum(argvars);
16630 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16631 lnum = 0;
16632 else
16633 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16634 --lnum;
16635 rettv->vval.v_number = lnum;
16636}
16637
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016638/* This dummy va_list is here because:
16639 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16640 * - locally in the function results in a "used before set" warning
16641 * - using va_start() to initialize it gives "function with fixed args" error */
16642static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016643
Bram Moolenaar8c711452005-01-14 21:53:12 +000016644/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016645 * "printf()" function
16646 */
16647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016648f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016649{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016650 char_u buf[NUMBUFLEN];
16651 int len;
16652 char_u *s;
16653 int saved_did_emsg = did_emsg;
16654 char *fmt;
16655
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016656 rettv->v_type = VAR_STRING;
16657 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016658
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016659 /* Get the required length, allocate the buffer and do it for real. */
16660 did_emsg = FALSE;
16661 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16662 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16663 if (!did_emsg)
16664 {
16665 s = alloc(len + 1);
16666 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016667 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016668 rettv->vval.v_string = s;
16669 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016670 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016671 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016672 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016673}
16674
16675/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016676 * "pumvisible()" function
16677 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016679f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016680{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016681#ifdef FEAT_INS_EXPAND
16682 if (pum_visible())
16683 rettv->vval.v_number = 1;
16684#endif
16685}
16686
Bram Moolenaardb913952012-06-29 12:54:53 +020016687#ifdef FEAT_PYTHON3
16688/*
16689 * "py3eval()" function
16690 */
16691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016692f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016693{
16694 char_u *str;
16695 char_u buf[NUMBUFLEN];
16696
16697 str = get_tv_string_buf(&argvars[0], buf);
16698 do_py3eval(str, rettv);
16699}
16700#endif
16701
16702#ifdef FEAT_PYTHON
16703/*
16704 * "pyeval()" function
16705 */
16706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016707f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016708{
16709 char_u *str;
16710 char_u buf[NUMBUFLEN];
16711
16712 str = get_tv_string_buf(&argvars[0], buf);
16713 do_pyeval(str, rettv);
16714}
16715#endif
16716
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016717/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016718 * "range()" function
16719 */
16720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016721f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016722{
16723 long start;
16724 long end;
16725 long stride = 1;
16726 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016727 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016730 if (argvars[1].v_type == VAR_UNKNOWN)
16731 {
16732 end = start - 1;
16733 start = 0;
16734 }
16735 else
16736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016738 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016739 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016740 }
16741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 if (error)
16743 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016744 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016745 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016746 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016747 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016748 else
16749 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016750 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016751 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 if (list_append_number(rettv->vval.v_list,
16753 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016754 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 }
16756}
16757
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016758/*
16759 * "readfile()" function
16760 */
16761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016762f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016763{
16764 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016765 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016766 char_u *fname;
16767 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016768 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16769 int io_size = sizeof(buf);
16770 int readlen; /* size of last fread() */
16771 char_u *prev = NULL; /* previously read bytes, if any */
16772 long prevlen = 0; /* length of data in prev */
16773 long prevsize = 0; /* size of prev buffer */
16774 long maxline = MAXLNUM;
16775 long cnt = 0;
16776 char_u *p; /* position in buf */
16777 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016778
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016779 if (argvars[1].v_type != VAR_UNKNOWN)
16780 {
16781 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16782 binary = TRUE;
16783 if (argvars[2].v_type != VAR_UNKNOWN)
16784 maxline = get_tv_number(&argvars[2]);
16785 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016786
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016787 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016788 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016789
16790 /* Always open the file in binary mode, library functions have a mind of
16791 * their own about CR-LF conversion. */
16792 fname = get_tv_string(&argvars[0]);
16793 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16794 {
16795 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16796 return;
16797 }
16798
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016799 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016800 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016801 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016802
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016803 /* This for loop processes what was read, but is also entered at end
16804 * of file so that either:
16805 * - an incomplete line gets written
16806 * - a "binary" file gets an empty line at the end if it ends in a
16807 * newline. */
16808 for (p = buf, start = buf;
16809 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16810 ++p)
16811 {
16812 if (*p == '\n' || readlen <= 0)
16813 {
16814 listitem_T *li;
16815 char_u *s = NULL;
16816 long_u len = p - start;
16817
16818 /* Finished a line. Remove CRs before NL. */
16819 if (readlen > 0 && !binary)
16820 {
16821 while (len > 0 && start[len - 1] == '\r')
16822 --len;
16823 /* removal may cross back to the "prev" string */
16824 if (len == 0)
16825 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16826 --prevlen;
16827 }
16828 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016829 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016830 else
16831 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016832 /* Change "prev" buffer to be the right size. This way
16833 * the bytes are only copied once, and very long lines are
16834 * allocated only once. */
16835 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016836 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016837 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016838 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016839 prev = NULL; /* the list will own the string */
16840 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016841 }
16842 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016843 if (s == NULL)
16844 {
16845 do_outofmem_msg((long_u) prevlen + len + 1);
16846 failed = TRUE;
16847 break;
16848 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016849
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016851 {
16852 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016853 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016854 break;
16855 }
16856 li->li_tv.v_type = VAR_STRING;
16857 li->li_tv.v_lock = 0;
16858 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016859 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016860
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016861 start = p + 1; /* step over newline */
16862 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016863 break;
16864 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016865 else if (*p == NUL)
16866 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016867#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016868 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16869 * when finding the BF and check the previous two bytes. */
16870 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016871 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016872 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16873 * + 1, these may be in the "prev" string. */
16874 char_u back1 = p >= buf + 1 ? p[-1]
16875 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16876 char_u back2 = p >= buf + 2 ? p[-2]
16877 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16878 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016879
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016880 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016881 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016882 char_u *dest = p - 2;
16883
16884 /* Usually a BOM is at the beginning of a file, and so at
16885 * the beginning of a line; then we can just step over it.
16886 */
16887 if (start == dest)
16888 start = p + 1;
16889 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016890 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016891 /* have to shuffle buf to close gap */
16892 int adjust_prevlen = 0;
16893
16894 if (dest < buf)
16895 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016896 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016897 dest = buf;
16898 }
16899 if (readlen > p - buf + 1)
16900 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16901 readlen -= 3 - adjust_prevlen;
16902 prevlen -= adjust_prevlen;
16903 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016904 }
16905 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016906 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016907#endif
16908 } /* for */
16909
16910 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16911 break;
16912 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016913 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016914 /* There's part of a line in buf, store it in "prev". */
16915 if (p - start + prevlen >= prevsize)
16916 {
16917 /* need bigger "prev" buffer */
16918 char_u *newprev;
16919
16920 /* A common use case is ordinary text files and "prev" gets a
16921 * fragment of a line, so the first allocation is made
16922 * small, to avoid repeatedly 'allocing' large and
16923 * 'reallocing' small. */
16924 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016925 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016926 else
16927 {
16928 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016929 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016930 prevsize = grow50pc > growmin ? grow50pc : growmin;
16931 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016932 newprev = prev == NULL ? alloc(prevsize)
16933 : vim_realloc(prev, prevsize);
16934 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016935 {
16936 do_outofmem_msg((long_u)prevsize);
16937 failed = TRUE;
16938 break;
16939 }
16940 prev = newprev;
16941 }
16942 /* Add the line part to end of "prev". */
16943 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016944 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016945 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016946 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016947
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016948 /*
16949 * For a negative line count use only the lines at the end of the file,
16950 * free the rest.
16951 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016952 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016953 while (cnt > -maxline)
16954 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016956 --cnt;
16957 }
16958
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016959 if (failed)
16960 {
16961 list_free(rettv->vval.v_list, TRUE);
16962 /* readfile doc says an empty list is returned on error */
16963 rettv->vval.v_list = list_alloc();
16964 }
16965
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016966 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016967 fclose(fd);
16968}
16969
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016970#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016971static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016972
16973/*
16974 * Convert a List to proftime_T.
16975 * Return FAIL when there is something wrong.
16976 */
16977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016978list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016979{
16980 long n1, n2;
16981 int error = FALSE;
16982
16983 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16984 || arg->vval.v_list->lv_len != 2)
16985 return FAIL;
16986 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16987 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16988# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016989 tm->HighPart = n1;
16990 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016991# else
16992 tm->tv_sec = n1;
16993 tm->tv_usec = n2;
16994# endif
16995 return error ? FAIL : OK;
16996}
16997#endif /* FEAT_RELTIME */
16998
16999/*
17000 * "reltime()" function
17001 */
17002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017003f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017004{
17005#ifdef FEAT_RELTIME
17006 proftime_T res;
17007 proftime_T start;
17008
17009 if (argvars[0].v_type == VAR_UNKNOWN)
17010 {
17011 /* No arguments: get current time. */
17012 profile_start(&res);
17013 }
17014 else if (argvars[1].v_type == VAR_UNKNOWN)
17015 {
17016 if (list2proftime(&argvars[0], &res) == FAIL)
17017 return;
17018 profile_end(&res);
17019 }
17020 else
17021 {
17022 /* Two arguments: compute the difference. */
17023 if (list2proftime(&argvars[0], &start) == FAIL
17024 || list2proftime(&argvars[1], &res) == FAIL)
17025 return;
17026 profile_sub(&res, &start);
17027 }
17028
17029 if (rettv_list_alloc(rettv) == OK)
17030 {
17031 long n1, n2;
17032
17033# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017034 n1 = res.HighPart;
17035 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017036# else
17037 n1 = res.tv_sec;
17038 n2 = res.tv_usec;
17039# endif
17040 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17041 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17042 }
17043#endif
17044}
17045
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017046#ifdef FEAT_FLOAT
17047/*
17048 * "reltimefloat()" function
17049 */
17050 static void
17051f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17052{
17053# ifdef FEAT_RELTIME
17054 proftime_T tm;
17055# endif
17056
17057 rettv->v_type = VAR_FLOAT;
17058 rettv->vval.v_float = 0;
17059# ifdef FEAT_RELTIME
17060 if (list2proftime(&argvars[0], &tm) == OK)
17061 rettv->vval.v_float = profile_float(&tm);
17062# endif
17063}
17064#endif
17065
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017066/*
17067 * "reltimestr()" function
17068 */
17069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017070f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017071{
17072#ifdef FEAT_RELTIME
17073 proftime_T tm;
17074#endif
17075
17076 rettv->v_type = VAR_STRING;
17077 rettv->vval.v_string = NULL;
17078#ifdef FEAT_RELTIME
17079 if (list2proftime(&argvars[0], &tm) == OK)
17080 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17081#endif
17082}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017083
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017085static void make_connection(void);
17086static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087
17088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017089make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090{
17091 if (X_DISPLAY == NULL
17092# ifdef FEAT_GUI
17093 && !gui.in_use
17094# endif
17095 )
17096 {
17097 x_force_connect = TRUE;
17098 setup_term_clip();
17099 x_force_connect = FALSE;
17100 }
17101}
17102
17103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017104check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105{
17106 make_connection();
17107 if (X_DISPLAY == NULL)
17108 {
17109 EMSG(_("E240: No connection to Vim server"));
17110 return FAIL;
17111 }
17112 return OK;
17113}
17114#endif
17115
17116#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017117static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118
17119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017120remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017121{
17122 char_u *server_name;
17123 char_u *keys;
17124 char_u *r = NULL;
17125 char_u buf[NUMBUFLEN];
17126# ifdef WIN32
17127 HWND w;
17128# else
17129 Window w;
17130# endif
17131
17132 if (check_restricted() || check_secure())
17133 return;
17134
17135# ifdef FEAT_X11
17136 if (check_connection() == FAIL)
17137 return;
17138# endif
17139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017140 server_name = get_tv_string_chk(&argvars[0]);
17141 if (server_name == NULL)
17142 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 keys = get_tv_string_buf(&argvars[1], buf);
17144# ifdef WIN32
17145 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17146# else
17147 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17148 < 0)
17149# endif
17150 {
17151 if (r != NULL)
17152 EMSG(r); /* sending worked but evaluation failed */
17153 else
17154 EMSG2(_("E241: Unable to send to %s"), server_name);
17155 return;
17156 }
17157
17158 rettv->vval.v_string = r;
17159
17160 if (argvars[2].v_type != VAR_UNKNOWN)
17161 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017162 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017163 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017164 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017166 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 v.di_tv.v_type = VAR_STRING;
17168 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 idvar = get_tv_string_chk(&argvars[2]);
17170 if (idvar != NULL)
17171 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 }
17174}
17175#endif
17176
17177/*
17178 * "remote_expr()" function
17179 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017181f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182{
17183 rettv->v_type = VAR_STRING;
17184 rettv->vval.v_string = NULL;
17185#ifdef FEAT_CLIENTSERVER
17186 remote_common(argvars, rettv, TRUE);
17187#endif
17188}
17189
17190/*
17191 * "remote_foreground()" function
17192 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196#ifdef FEAT_CLIENTSERVER
17197# ifdef WIN32
17198 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017199 {
17200 char_u *server_name = get_tv_string_chk(&argvars[0]);
17201
17202 if (server_name != NULL)
17203 serverForeground(server_name);
17204 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205# else
17206 /* Send a foreground() expression to the server. */
17207 argvars[1].v_type = VAR_STRING;
17208 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17209 argvars[2].v_type = VAR_UNKNOWN;
17210 remote_common(argvars, rettv, TRUE);
17211 vim_free(argvars[1].vval.v_string);
17212# endif
17213#endif
17214}
17215
Bram Moolenaar0d660222005-01-07 21:51:51 +000017216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017217f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218{
17219#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017220 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 char_u *s = NULL;
17222# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017223 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226
17227 if (check_restricted() || check_secure())
17228 {
17229 rettv->vval.v_number = -1;
17230 return;
17231 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 serverid = get_tv_string_chk(&argvars[0]);
17233 if (serverid == NULL)
17234 {
17235 rettv->vval.v_number = -1;
17236 return; /* type error; errmsg already given */
17237 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017238# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017239 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017240 if (n == 0)
17241 rettv->vval.v_number = -1;
17242 else
17243 {
17244 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17245 rettv->vval.v_number = (s != NULL);
17246 }
17247# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017248 if (check_connection() == FAIL)
17249 return;
17250
17251 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253# endif
17254
17255 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 char_u *retvar;
17258
Bram Moolenaar33570922005-01-25 22:26:29 +000017259 v.di_tv.v_type = VAR_STRING;
17260 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017261 retvar = get_tv_string_chk(&argvars[1]);
17262 if (retvar != NULL)
17263 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017265 }
17266#else
17267 rettv->vval.v_number = -1;
17268#endif
17269}
17270
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017272f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017273{
17274 char_u *r = NULL;
17275
17276#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017277 char_u *serverid = get_tv_string_chk(&argvars[0]);
17278
17279 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 {
17281# ifdef WIN32
17282 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017283 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017285 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017286 if (n != 0)
17287 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17288 if (r == NULL)
17289# else
17290 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017292# endif
17293 EMSG(_("E277: Unable to read a server reply"));
17294 }
17295#endif
17296 rettv->v_type = VAR_STRING;
17297 rettv->vval.v_string = r;
17298}
17299
17300/*
17301 * "remote_send()" function
17302 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017304f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305{
17306 rettv->v_type = VAR_STRING;
17307 rettv->vval.v_string = NULL;
17308#ifdef FEAT_CLIENTSERVER
17309 remote_common(argvars, rettv, FALSE);
17310#endif
17311}
17312
17313/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017314 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017315 */
17316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017317f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017318{
Bram Moolenaar33570922005-01-25 22:26:29 +000017319 list_T *l;
17320 listitem_T *item, *item2;
17321 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017322 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017324 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017325 dict_T *d;
17326 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017327 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017328
Bram Moolenaar8c711452005-01-14 21:53:12 +000017329 if (argvars[0].v_type == VAR_DICT)
17330 {
17331 if (argvars[2].v_type != VAR_UNKNOWN)
17332 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017333 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017334 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 key = get_tv_string_chk(&argvars[1]);
17337 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339 di = dict_find(d, key, -1);
17340 if (di == NULL)
17341 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017342 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17343 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017344 {
17345 *rettv = di->di_tv;
17346 init_tv(&di->di_tv);
17347 dictitem_remove(d, di);
17348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017349 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017350 }
17351 }
17352 else if (argvars[0].v_type != VAR_LIST)
17353 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017354 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017355 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 int error = FALSE;
17358
17359 idx = get_tv_number_chk(&argvars[1], &error);
17360 if (error)
17361 ; /* type error: do nothing, errmsg already given */
17362 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017363 EMSGN(_(e_listidx), idx);
17364 else
17365 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017366 if (argvars[2].v_type == VAR_UNKNOWN)
17367 {
17368 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017369 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017370 *rettv = item->li_tv;
17371 vim_free(item);
17372 }
17373 else
17374 {
17375 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 end = get_tv_number_chk(&argvars[2], &error);
17377 if (error)
17378 ; /* type error: do nothing */
17379 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017380 EMSGN(_(e_listidx), end);
17381 else
17382 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017383 int cnt = 0;
17384
17385 for (li = item; li != NULL; li = li->li_next)
17386 {
17387 ++cnt;
17388 if (li == item2)
17389 break;
17390 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017391 if (li == NULL) /* didn't find "item2" after "item" */
17392 EMSG(_(e_invrange));
17393 else
17394 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017395 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017397 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017398 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017399 l->lv_first = item;
17400 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017401 item->li_prev = NULL;
17402 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017403 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017404 }
17405 }
17406 }
17407 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017408 }
17409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410}
17411
17412/*
17413 * "rename({from}, {to})" function
17414 */
17415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017416f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417{
17418 char_u buf[NUMBUFLEN];
17419
17420 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017423 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17424 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425}
17426
17427/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017428 * "repeat()" function
17429 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017431f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017432{
17433 char_u *p;
17434 int n;
17435 int slen;
17436 int len;
17437 char_u *r;
17438 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017439
17440 n = get_tv_number(&argvars[1]);
17441 if (argvars[0].v_type == VAR_LIST)
17442 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017443 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017444 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017445 if (list_extend(rettv->vval.v_list,
17446 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017447 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017448 }
17449 else
17450 {
17451 p = get_tv_string(&argvars[0]);
17452 rettv->v_type = VAR_STRING;
17453 rettv->vval.v_string = NULL;
17454
17455 slen = (int)STRLEN(p);
17456 len = slen * n;
17457 if (len <= 0)
17458 return;
17459
17460 r = alloc(len + 1);
17461 if (r != NULL)
17462 {
17463 for (i = 0; i < n; i++)
17464 mch_memmove(r + i * slen, p, (size_t)slen);
17465 r[len] = NUL;
17466 }
17467
17468 rettv->vval.v_string = r;
17469 }
17470}
17471
17472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 * "resolve()" function
17474 */
17475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017476f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477{
17478 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017479#ifdef HAVE_READLINK
17480 char_u *buf = NULL;
17481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017483 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484#ifdef FEAT_SHORTCUT
17485 {
17486 char_u *v = NULL;
17487
17488 v = mch_resolve_shortcut(p);
17489 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017490 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493 }
17494#else
17495# ifdef HAVE_READLINK
17496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 char_u *cpy;
17498 int len;
17499 char_u *remain = NULL;
17500 char_u *q;
17501 int is_relative_to_current = FALSE;
17502 int has_trailing_pathsep = FALSE;
17503 int limit = 100;
17504
17505 p = vim_strsave(p);
17506
17507 if (p[0] == '.' && (vim_ispathsep(p[1])
17508 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17509 is_relative_to_current = TRUE;
17510
17511 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017512 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017515 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
17518 q = getnextcomp(p);
17519 if (*q != NUL)
17520 {
17521 /* Separate the first path component in "p", and keep the
17522 * remainder (beginning with the path separator). */
17523 remain = vim_strsave(q - 1);
17524 q[-1] = NUL;
17525 }
17526
Bram Moolenaard9462e32011-04-11 21:35:11 +020017527 buf = alloc(MAXPATHL + 1);
17528 if (buf == NULL)
17529 goto fail;
17530
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 for (;;)
17532 {
17533 for (;;)
17534 {
17535 len = readlink((char *)p, (char *)buf, MAXPATHL);
17536 if (len <= 0)
17537 break;
17538 buf[len] = NUL;
17539
17540 if (limit-- == 0)
17541 {
17542 vim_free(p);
17543 vim_free(remain);
17544 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 goto fail;
17547 }
17548
17549 /* Ensure that the result will have a trailing path separator
17550 * if the argument has one. */
17551 if (remain == NULL && has_trailing_pathsep)
17552 add_pathsep(buf);
17553
17554 /* Separate the first path component in the link value and
17555 * concatenate the remainders. */
17556 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17557 if (*q != NUL)
17558 {
17559 if (remain == NULL)
17560 remain = vim_strsave(q - 1);
17561 else
17562 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017563 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 if (cpy != NULL)
17565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 vim_free(remain);
17567 remain = cpy;
17568 }
17569 }
17570 q[-1] = NUL;
17571 }
17572
17573 q = gettail(p);
17574 if (q > p && *q == NUL)
17575 {
17576 /* Ignore trailing path separator. */
17577 q[-1] = NUL;
17578 q = gettail(p);
17579 }
17580 if (q > p && !mch_isFullName(buf))
17581 {
17582 /* symlink is relative to directory of argument */
17583 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17584 if (cpy != NULL)
17585 {
17586 STRCPY(cpy, p);
17587 STRCPY(gettail(cpy), buf);
17588 vim_free(p);
17589 p = cpy;
17590 }
17591 }
17592 else
17593 {
17594 vim_free(p);
17595 p = vim_strsave(buf);
17596 }
17597 }
17598
17599 if (remain == NULL)
17600 break;
17601
17602 /* Append the first path component of "remain" to "p". */
17603 q = getnextcomp(remain + 1);
17604 len = q - remain - (*q != NUL);
17605 cpy = vim_strnsave(p, STRLEN(p) + len);
17606 if (cpy != NULL)
17607 {
17608 STRNCAT(cpy, remain, len);
17609 vim_free(p);
17610 p = cpy;
17611 }
17612 /* Shorten "remain". */
17613 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017614 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 else
17616 {
17617 vim_free(remain);
17618 remain = NULL;
17619 }
17620 }
17621
17622 /* If the result is a relative path name, make it explicitly relative to
17623 * the current directory if and only if the argument had this form. */
17624 if (!vim_ispathsep(*p))
17625 {
17626 if (is_relative_to_current
17627 && *p != NUL
17628 && !(p[0] == '.'
17629 && (p[1] == NUL
17630 || vim_ispathsep(p[1])
17631 || (p[1] == '.'
17632 && (p[2] == NUL
17633 || vim_ispathsep(p[2]))))))
17634 {
17635 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017636 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 if (cpy != NULL)
17638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 vim_free(p);
17640 p = cpy;
17641 }
17642 }
17643 else if (!is_relative_to_current)
17644 {
17645 /* Strip leading "./". */
17646 q = p;
17647 while (q[0] == '.' && vim_ispathsep(q[1]))
17648 q += 2;
17649 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017650 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 }
17652 }
17653
17654 /* Ensure that the result will have no trailing path separator
17655 * if the argument had none. But keep "/" or "//". */
17656 if (!has_trailing_pathsep)
17657 {
17658 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017659 if (after_pathsep(p, q))
17660 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 }
17662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 }
17665# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667# endif
17668#endif
17669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017670 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
17672#ifdef HAVE_READLINK
17673fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017674 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
17679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017680 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684{
Bram Moolenaar33570922005-01-25 22:26:29 +000017685 list_T *l;
17686 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688 if (argvars[0].v_type != VAR_LIST)
17689 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017690 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017691 && !tv_check_lock(l->lv_lock,
17692 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693 {
17694 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017695 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017696 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697 while (li != NULL)
17698 {
17699 ni = li->li_prev;
17700 list_append(l, li);
17701 li = ni;
17702 }
17703 rettv->vval.v_list = l;
17704 rettv->v_type = VAR_LIST;
17705 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017706 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708}
17709
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017710#define SP_NOMOVE 0x01 /* don't move cursor */
17711#define SP_REPEAT 0x02 /* repeat to find outer pair */
17712#define SP_RETCOUNT 0x04 /* return matchcount */
17713#define SP_SETPCMARK 0x08 /* set previous context mark */
17714#define SP_START 0x10 /* accept match at start position */
17715#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17716#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017717#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017718
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017719static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017720
17721/*
17722 * Get flags for a search function.
17723 * Possibly sets "p_ws".
17724 * Returns BACKWARD, FORWARD or zero (for an error).
17725 */
17726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017727get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728{
17729 int dir = FORWARD;
17730 char_u *flags;
17731 char_u nbuf[NUMBUFLEN];
17732 int mask;
17733
17734 if (varp->v_type != VAR_UNKNOWN)
17735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017736 flags = get_tv_string_buf_chk(varp, nbuf);
17737 if (flags == NULL)
17738 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 while (*flags != NUL)
17740 {
17741 switch (*flags)
17742 {
17743 case 'b': dir = BACKWARD; break;
17744 case 'w': p_ws = TRUE; break;
17745 case 'W': p_ws = FALSE; break;
17746 default: mask = 0;
17747 if (flagsp != NULL)
17748 switch (*flags)
17749 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017750 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017751 case 'e': mask = SP_END; break;
17752 case 'm': mask = SP_RETCOUNT; break;
17753 case 'n': mask = SP_NOMOVE; break;
17754 case 'p': mask = SP_SUBPAT; break;
17755 case 'r': mask = SP_REPEAT; break;
17756 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017757 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758 }
17759 if (mask == 0)
17760 {
17761 EMSG2(_(e_invarg2), flags);
17762 dir = 0;
17763 }
17764 else
17765 *flagsp |= mask;
17766 }
17767 if (dir == 0)
17768 break;
17769 ++flags;
17770 }
17771 }
17772 return dir;
17773}
17774
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017776 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017779search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017781 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 char_u *pat;
17783 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017784 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 int save_p_ws = p_ws;
17786 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017787 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017788 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017789 proftime_T tm;
17790#ifdef FEAT_RELTIME
17791 long time_limit = 0;
17792#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017793 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017794 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017796 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017797 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017798 if (dir == 0)
17799 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017800 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017801 if (flags & SP_START)
17802 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017803 if (flags & SP_END)
17804 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017805 if (flags & SP_COLUMN)
17806 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017807
Bram Moolenaar76929292008-01-06 19:07:36 +000017808 /* Optional arguments: line number to stop searching and timeout. */
17809 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017810 {
17811 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17812 if (lnum_stop < 0)
17813 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017814#ifdef FEAT_RELTIME
17815 if (argvars[3].v_type != VAR_UNKNOWN)
17816 {
17817 time_limit = get_tv_number_chk(&argvars[3], NULL);
17818 if (time_limit < 0)
17819 goto theend;
17820 }
17821#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017822 }
17823
Bram Moolenaar76929292008-01-06 19:07:36 +000017824#ifdef FEAT_RELTIME
17825 /* Set the time limit, if there is one. */
17826 profile_setlimit(time_limit, &tm);
17827#endif
17828
Bram Moolenaar231334e2005-07-25 20:46:57 +000017829 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017830 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017831 * Check to make sure only those flags are set.
17832 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17833 * flags cannot be set. Check for that condition also.
17834 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017835 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017836 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017838 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017839 goto theend;
17840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017842 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017843 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017844 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017845 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017847 if (flags & SP_SUBPAT)
17848 retval = subpatnum;
17849 else
17850 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017851 if (flags & SP_SETPCMARK)
17852 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017854 if (match_pos != NULL)
17855 {
17856 /* Store the match cursor position */
17857 match_pos->lnum = pos.lnum;
17858 match_pos->col = pos.col + 1;
17859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 /* "/$" will put the cursor after the end of the line, may need to
17861 * correct that here */
17862 check_cursor();
17863 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017864
17865 /* If 'n' flag is used: restore cursor position. */
17866 if (flags & SP_NOMOVE)
17867 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017868 else
17869 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017870theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017872
17873 return retval;
17874}
17875
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017876#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017877
17878/*
17879 * round() is not in C90, use ceil() or floor() instead.
17880 */
17881 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017882vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017883{
17884 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17885}
17886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017887/*
17888 * "round({float})" function
17889 */
17890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017891f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017892{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017893 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017894
17895 rettv->v_type = VAR_FLOAT;
17896 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017897 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017898 else
17899 rettv->vval.v_float = 0.0;
17900}
17901#endif
17902
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017903/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017904 * "screenattr()" function
17905 */
17906 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017907f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017908{
17909 int row;
17910 int col;
17911 int c;
17912
17913 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17914 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17915 if (row < 0 || row >= screen_Rows
17916 || col < 0 || col >= screen_Columns)
17917 c = -1;
17918 else
17919 c = ScreenAttrs[LineOffset[row] + col];
17920 rettv->vval.v_number = c;
17921}
17922
17923/*
17924 * "screenchar()" function
17925 */
17926 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017927f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017928{
17929 int row;
17930 int col;
17931 int off;
17932 int c;
17933
17934 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17935 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17936 if (row < 0 || row >= screen_Rows
17937 || col < 0 || col >= screen_Columns)
17938 c = -1;
17939 else
17940 {
17941 off = LineOffset[row] + col;
17942#ifdef FEAT_MBYTE
17943 if (enc_utf8 && ScreenLinesUC[off] != 0)
17944 c = ScreenLinesUC[off];
17945 else
17946#endif
17947 c = ScreenLines[off];
17948 }
17949 rettv->vval.v_number = c;
17950}
17951
17952/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017953 * "screencol()" function
17954 *
17955 * First column is 1 to be consistent with virtcol().
17956 */
17957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017958f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017959{
17960 rettv->vval.v_number = screen_screencol() + 1;
17961}
17962
17963/*
17964 * "screenrow()" function
17965 */
17966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017968{
17969 rettv->vval.v_number = screen_screenrow() + 1;
17970}
17971
17972/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017973 * "search()" function
17974 */
17975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017976f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017978 int flags = 0;
17979
17980 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981}
17982
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017984 * "searchdecl()" function
17985 */
17986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017987f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017988{
17989 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017990 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017991 int error = FALSE;
17992 char_u *name;
17993
17994 rettv->vval.v_number = 1; /* default: FAIL */
17995
17996 name = get_tv_string_chk(&argvars[0]);
17997 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017998 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017999 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018000 if (!error && argvars[2].v_type != VAR_UNKNOWN)
18001 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
18002 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018003 if (!error && name != NULL)
18004 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018005 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018006}
18007
18008/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018012searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013{
18014 char_u *spat, *mpat, *epat;
18015 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 int dir;
18018 int flags = 0;
18019 char_u nbuf1[NUMBUFLEN];
18020 char_u nbuf2[NUMBUFLEN];
18021 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018022 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018023 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018024 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018027 spat = get_tv_string_chk(&argvars[0]);
18028 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18029 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18030 if (spat == NULL || mpat == NULL || epat == NULL)
18031 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 /* Handle the optional fourth argument: flags */
18034 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018035 if (dir == 0)
18036 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018037
18038 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018039 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18040 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018041 if ((flags & (SP_END | SP_SUBPAT)) != 0
18042 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018043 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018044 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018045 goto theend;
18046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018048 /* Using 'r' implies 'W', otherwise it doesn't work. */
18049 if (flags & SP_REPEAT)
18050 p_ws = FALSE;
18051
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018052 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018053 if (argvars[3].v_type == VAR_UNKNOWN
18054 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 skip = (char_u *)"";
18056 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018058 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018059 if (argvars[5].v_type != VAR_UNKNOWN)
18060 {
18061 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
18062 if (lnum_stop < 0)
18063 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018064#ifdef FEAT_RELTIME
18065 if (argvars[6].v_type != VAR_UNKNOWN)
18066 {
18067 time_limit = get_tv_number_chk(&argvars[6], NULL);
18068 if (time_limit < 0)
18069 goto theend;
18070 }
18071#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018072 }
18073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018074 if (skip == NULL)
18075 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018077 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018078 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018079
18080theend:
18081 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018082
18083 return retval;
18084}
18085
18086/*
18087 * "searchpair()" function
18088 */
18089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018090f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018091{
18092 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18093}
18094
18095/*
18096 * "searchpairpos()" function
18097 */
18098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018100{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018101 pos_T match_pos;
18102 int lnum = 0;
18103 int col = 0;
18104
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018105 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018106 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018107
18108 if (searchpair_cmn(argvars, &match_pos) > 0)
18109 {
18110 lnum = match_pos.lnum;
18111 col = match_pos.col;
18112 }
18113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018114 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18115 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018116}
18117
18118/*
18119 * Search for a start/middle/end thing.
18120 * Used by searchpair(), see its documentation for the details.
18121 * Returns 0 or -1 for no match,
18122 */
18123 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018124do_searchpair(
18125 char_u *spat, /* start pattern */
18126 char_u *mpat, /* middle pattern */
18127 char_u *epat, /* end pattern */
18128 int dir, /* BACKWARD or FORWARD */
18129 char_u *skip, /* skip expression */
18130 int flags, /* SP_SETPCMARK and other SP_ values */
18131 pos_T *match_pos,
18132 linenr_T lnum_stop, /* stop at this line if not zero */
18133 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018134{
18135 char_u *save_cpo;
18136 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18137 long retval = 0;
18138 pos_T pos;
18139 pos_T firstpos;
18140 pos_T foundpos;
18141 pos_T save_cursor;
18142 pos_T save_pos;
18143 int n;
18144 int r;
18145 int nest = 1;
18146 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018147 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018148 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018149
18150 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18151 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018152 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018153
Bram Moolenaar76929292008-01-06 19:07:36 +000018154#ifdef FEAT_RELTIME
18155 /* Set the time limit, if there is one. */
18156 profile_setlimit(time_limit, &tm);
18157#endif
18158
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018159 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18160 * start/middle/end (pat3, for the top pair). */
18161 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18162 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18163 if (pat2 == NULL || pat3 == NULL)
18164 goto theend;
18165 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18166 if (*mpat == NUL)
18167 STRCPY(pat3, pat2);
18168 else
18169 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18170 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018171 if (flags & SP_START)
18172 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018173
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 save_cursor = curwin->w_cursor;
18175 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018176 clearpos(&firstpos);
18177 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 pat = pat3;
18179 for (;;)
18180 {
18181 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018182 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18184 /* didn't find it or found the first match again: FAIL */
18185 break;
18186
18187 if (firstpos.lnum == 0)
18188 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018189 if (equalpos(pos, foundpos))
18190 {
18191 /* Found the same position again. Can happen with a pattern that
18192 * has "\zs" at the end and searching backwards. Advance one
18193 * character and try again. */
18194 if (dir == BACKWARD)
18195 decl(&pos);
18196 else
18197 incl(&pos);
18198 }
18199 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018201 /* clear the start flag to avoid getting stuck here */
18202 options &= ~SEARCH_START;
18203
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 /* If the skip pattern matches, ignore this match. */
18205 if (*skip != NUL)
18206 {
18207 save_pos = curwin->w_cursor;
18208 curwin->w_cursor = pos;
18209 r = eval_to_bool(skip, &err, NULL, FALSE);
18210 curwin->w_cursor = save_pos;
18211 if (err)
18212 {
18213 /* Evaluating {skip} caused an error, break here. */
18214 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018215 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 break;
18217 }
18218 if (r)
18219 continue;
18220 }
18221
18222 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18223 {
18224 /* Found end when searching backwards or start when searching
18225 * forward: nested pair. */
18226 ++nest;
18227 pat = pat2; /* nested, don't search for middle */
18228 }
18229 else
18230 {
18231 /* Found end when searching forward or start when searching
18232 * backward: end of (nested) pair; or found middle in outer pair. */
18233 if (--nest == 1)
18234 pat = pat3; /* outer level, search for middle */
18235 }
18236
18237 if (nest == 0)
18238 {
18239 /* Found the match: return matchcount or line number. */
18240 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018241 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018243 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018244 if (flags & SP_SETPCMARK)
18245 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 curwin->w_cursor = pos;
18247 if (!(flags & SP_REPEAT))
18248 break;
18249 nest = 1; /* search for next unmatched */
18250 }
18251 }
18252
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018253 if (match_pos != NULL)
18254 {
18255 /* Store the match cursor position */
18256 match_pos->lnum = curwin->w_cursor.lnum;
18257 match_pos->col = curwin->w_cursor.col + 1;
18258 }
18259
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018261 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 curwin->w_cursor = save_cursor;
18263
18264theend:
18265 vim_free(pat2);
18266 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018267 if (p_cpo == empty_option)
18268 p_cpo = save_cpo;
18269 else
18270 /* Darn, evaluating the {skip} expression changed the value. */
18271 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018272
18273 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274}
18275
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018276/*
18277 * "searchpos()" function
18278 */
18279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018280f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018281{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018282 pos_T match_pos;
18283 int lnum = 0;
18284 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018285 int n;
18286 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018287
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018288 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018289 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018290
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018291 n = search_cmn(argvars, &match_pos, &flags);
18292 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018293 {
18294 lnum = match_pos.lnum;
18295 col = match_pos.col;
18296 }
18297
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018298 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18299 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018300 if (flags & SP_SUBPAT)
18301 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018302}
18303
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018305f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307#ifdef FEAT_CLIENTSERVER
18308 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018309 char_u *server = get_tv_string_chk(&argvars[0]);
18310 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018313 if (server == NULL || reply == NULL)
18314 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315 if (check_restricted() || check_secure())
18316 return;
18317# ifdef FEAT_X11
18318 if (check_connection() == FAIL)
18319 return;
18320# endif
18321
18322 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018324 EMSG(_("E258: Unable to send to client"));
18325 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327 rettv->vval.v_number = 0;
18328#else
18329 rettv->vval.v_number = -1;
18330#endif
18331}
18332
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018334f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018335{
18336 char_u *r = NULL;
18337
18338#ifdef FEAT_CLIENTSERVER
18339# ifdef WIN32
18340 r = serverGetVimNames();
18341# else
18342 make_connection();
18343 if (X_DISPLAY != NULL)
18344 r = serverGetVimNames(X_DISPLAY);
18345# endif
18346#endif
18347 rettv->v_type = VAR_STRING;
18348 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349}
18350
18351/*
18352 * "setbufvar()" function
18353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018355f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356{
18357 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018360 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 char_u nbuf[NUMBUFLEN];
18362
18363 if (check_restricted() || check_secure())
18364 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018365 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18366 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018367 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 varp = &argvars[2];
18369
18370 if (buf != NULL && varname != NULL && varp != NULL)
18371 {
18372 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374
18375 if (*varname == '&')
18376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018377 long numval;
18378 char_u *strval;
18379 int error = FALSE;
18380
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018382 numval = get_tv_number_chk(varp, &error);
18383 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018384 if (!error && strval != NULL)
18385 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 }
18387 else
18388 {
18389 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18390 if (bufvarname != NULL)
18391 {
18392 STRCPY(bufvarname, "b:");
18393 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018394 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 vim_free(bufvarname);
18396 }
18397 }
18398
18399 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402}
18403
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018405f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018406{
18407 dict_T *d;
18408 dictitem_T *di;
18409 char_u *csearch;
18410
18411 if (argvars[0].v_type != VAR_DICT)
18412 {
18413 EMSG(_(e_dictreq));
18414 return;
18415 }
18416
18417 if ((d = argvars[0].vval.v_dict) != NULL)
18418 {
18419 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18420 if (csearch != NULL)
18421 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018422#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018423 if (enc_utf8)
18424 {
18425 int pcc[MAX_MCO];
18426 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018427
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018428 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18429 }
18430 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018431#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018432 set_last_csearch(PTR2CHAR(csearch),
18433 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018434 }
18435
18436 di = dict_find(d, (char_u *)"forward", -1);
18437 if (di != NULL)
18438 set_csearch_direction(get_tv_number(&di->di_tv)
18439 ? FORWARD : BACKWARD);
18440
18441 di = dict_find(d, (char_u *)"until", -1);
18442 if (di != NULL)
18443 set_csearch_until(!!get_tv_number(&di->di_tv));
18444 }
18445}
18446
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447/*
18448 * "setcmdpos()" function
18449 */
18450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018451f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018453 int pos = (int)get_tv_number(&argvars[0]) - 1;
18454
18455 if (pos >= 0)
18456 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457}
18458
18459/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018460 * "setfperm({fname}, {mode})" function
18461 */
18462 static void
18463f_setfperm(typval_T *argvars, typval_T *rettv)
18464{
18465 char_u *fname;
18466 char_u modebuf[NUMBUFLEN];
18467 char_u *mode_str;
18468 int i;
18469 int mask;
18470 int mode = 0;
18471
18472 rettv->vval.v_number = 0;
18473 fname = get_tv_string_chk(&argvars[0]);
18474 if (fname == NULL)
18475 return;
18476 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18477 if (mode_str == NULL)
18478 return;
18479 if (STRLEN(mode_str) != 9)
18480 {
18481 EMSG2(_(e_invarg2), mode_str);
18482 return;
18483 }
18484
18485 mask = 1;
18486 for (i = 8; i >= 0; --i)
18487 {
18488 if (mode_str[i] != '-')
18489 mode |= mask;
18490 mask = mask << 1;
18491 }
18492 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18493}
18494
18495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 * "setline()" function
18497 */
18498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018499f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500{
18501 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018502 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018503 list_T *l = NULL;
18504 listitem_T *li = NULL;
18505 long added = 0;
18506 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018508 lnum = get_tv_lnum(&argvars[0]);
18509 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018511 l = argvars[1].vval.v_list;
18512 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018514 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018515 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018516
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018517 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018518 for (;;)
18519 {
18520 if (l != NULL)
18521 {
18522 /* list argument, get next string */
18523 if (li == NULL)
18524 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018525 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018526 li = li->li_next;
18527 }
18528
18529 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018531 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018532
18533 /* When coming here from Insert mode, sync undo, so that this can be
18534 * undone separately from what was previously inserted. */
18535 if (u_sync_once == 2)
18536 {
18537 u_sync_once = 1; /* notify that u_sync() was called */
18538 u_sync(TRUE);
18539 }
18540
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018541 if (lnum <= curbuf->b_ml.ml_line_count)
18542 {
18543 /* existing line, replace it */
18544 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18545 {
18546 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018547 if (lnum == curwin->w_cursor.lnum)
18548 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018549 rettv->vval.v_number = 0; /* OK */
18550 }
18551 }
18552 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18553 {
18554 /* lnum is one past the last line, append the line */
18555 ++added;
18556 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18557 rettv->vval.v_number = 0; /* OK */
18558 }
18559
18560 if (l == NULL) /* only one string argument */
18561 break;
18562 ++lnum;
18563 }
18564
18565 if (added > 0)
18566 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567}
18568
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018569static 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 +000018570
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018572 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018573 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018575set_qf_ll_list(
18576 win_T *wp UNUSED,
18577 typval_T *list_arg UNUSED,
18578 typval_T *action_arg UNUSED,
18579 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018580{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018581#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018582 char_u *act;
18583 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018584#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018585
Bram Moolenaar2641f772005-03-25 21:58:17 +000018586 rettv->vval.v_number = -1;
18587
18588#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018589 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018590 EMSG(_(e_listreq));
18591 else
18592 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018593 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018594
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018595 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018596 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018597 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018598 if (act == NULL)
18599 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018600 if (*act == 'a' || *act == 'r')
18601 action = *act;
18602 }
18603
Bram Moolenaar81484f42012-12-05 15:16:47 +010018604 if (l != NULL && set_errorlist(wp, l, action,
18605 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018606 rettv->vval.v_number = 0;
18607 }
18608#endif
18609}
18610
18611/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018612 * "setloclist()" function
18613 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018615f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018616{
18617 win_T *win;
18618
18619 rettv->vval.v_number = -1;
18620
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018621 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018622 if (win != NULL)
18623 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18624}
18625
18626/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018627 * "setmatches()" function
18628 */
18629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018630f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018631{
18632#ifdef FEAT_SEARCH_EXTRA
18633 list_T *l;
18634 listitem_T *li;
18635 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018636 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018637
18638 rettv->vval.v_number = -1;
18639 if (argvars[0].v_type != VAR_LIST)
18640 {
18641 EMSG(_(e_listreq));
18642 return;
18643 }
18644 if ((l = argvars[0].vval.v_list) != NULL)
18645 {
18646
18647 /* To some extent make sure that we are dealing with a list from
18648 * "getmatches()". */
18649 li = l->lv_first;
18650 while (li != NULL)
18651 {
18652 if (li->li_tv.v_type != VAR_DICT
18653 || (d = li->li_tv.vval.v_dict) == NULL)
18654 {
18655 EMSG(_(e_invarg));
18656 return;
18657 }
18658 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018659 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18660 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018661 && dict_find(d, (char_u *)"priority", -1) != NULL
18662 && dict_find(d, (char_u *)"id", -1) != NULL))
18663 {
18664 EMSG(_(e_invarg));
18665 return;
18666 }
18667 li = li->li_next;
18668 }
18669
18670 clear_matches(curwin);
18671 li = l->lv_first;
18672 while (li != NULL)
18673 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018674 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018675 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018676 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018677 char_u *group;
18678 int priority;
18679 int id;
18680 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018681
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018682 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018683 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18684 {
18685 if (s == NULL)
18686 {
18687 s = list_alloc();
18688 if (s == NULL)
18689 return;
18690 }
18691
18692 /* match from matchaddpos() */
18693 for (i = 1; i < 9; i++)
18694 {
18695 sprintf((char *)buf, (char *)"pos%d", i);
18696 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18697 {
18698 if (di->di_tv.v_type != VAR_LIST)
18699 return;
18700
18701 list_append_tv(s, &di->di_tv);
18702 s->lv_refcount++;
18703 }
18704 else
18705 break;
18706 }
18707 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018708
18709 group = get_dict_string(d, (char_u *)"group", FALSE);
18710 priority = (int)get_dict_number(d, (char_u *)"priority");
18711 id = (int)get_dict_number(d, (char_u *)"id");
18712 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18713 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18714 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018715 if (i == 0)
18716 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018717 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018718 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018719 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018720 }
18721 else
18722 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018723 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018724 list_unref(s);
18725 s = NULL;
18726 }
18727
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018728 li = li->li_next;
18729 }
18730 rettv->vval.v_number = 0;
18731 }
18732#endif
18733}
18734
18735/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018736 * "setpos()" function
18737 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018739f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018740{
18741 pos_T pos;
18742 int fnum;
18743 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018744 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018745
Bram Moolenaar08250432008-02-13 11:42:46 +000018746 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018747 name = get_tv_string_chk(argvars);
18748 if (name != NULL)
18749 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018750 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018751 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018752 if (--pos.col < 0)
18753 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018754 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018755 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018756 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018757 if (fnum == curbuf->b_fnum)
18758 {
18759 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018760 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018761 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018762 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018763 curwin->w_set_curswant = FALSE;
18764 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018765 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018766 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018767 }
18768 else
18769 EMSG(_(e_invarg));
18770 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018771 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18772 {
18773 /* set mark */
18774 if (setmark_pos(name[1], &pos, fnum) == OK)
18775 rettv->vval.v_number = 0;
18776 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018777 else
18778 EMSG(_(e_invarg));
18779 }
18780 }
18781}
18782
18783/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018784 * "setqflist()" function
18785 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018787f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018788{
18789 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18790}
18791
18792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 * "setreg()" function
18794 */
18795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018796f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797{
18798 int regname;
18799 char_u *strregname;
18800 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018801 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802 int append;
18803 char_u yank_type;
18804 long block_len;
18805
18806 block_len = -1;
18807 yank_type = MAUTO;
18808 append = FALSE;
18809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018810 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 if (strregname == NULL)
18814 return; /* type error; errmsg already given */
18815 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 if (regname == 0 || regname == '@')
18817 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018821 stropt = get_tv_string_chk(&argvars[2]);
18822 if (stropt == NULL)
18823 return; /* type error */
18824 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 switch (*stropt)
18826 {
18827 case 'a': case 'A': /* append */
18828 append = TRUE;
18829 break;
18830 case 'v': case 'c': /* character-wise selection */
18831 yank_type = MCHAR;
18832 break;
18833 case 'V': case 'l': /* line-wise selection */
18834 yank_type = MLINE;
18835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 case 'b': case Ctrl_V: /* block-wise selection */
18837 yank_type = MBLOCK;
18838 if (VIM_ISDIGIT(stropt[1]))
18839 {
18840 ++stropt;
18841 block_len = getdigits(&stropt) - 1;
18842 --stropt;
18843 }
18844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 }
18846 }
18847
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018848 if (argvars[1].v_type == VAR_LIST)
18849 {
18850 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018851 char_u **allocval;
18852 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018853 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018854 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018855 int len = argvars[1].vval.v_list->lv_len;
18856 listitem_T *li;
18857
Bram Moolenaar7d647822014-04-05 21:28:56 +020018858 /* First half: use for pointers to result lines; second half: use for
18859 * pointers to allocated copies. */
18860 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018861 if (lstval == NULL)
18862 return;
18863 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018864 allocval = lstval + len + 2;
18865 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018866
18867 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18868 li = li->li_next)
18869 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018870 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018871 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018872 goto free_lstval;
18873 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018874 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018875 /* Need to make a copy, next get_tv_string_buf_chk() will
18876 * overwrite the string. */
18877 strval = vim_strsave(buf);
18878 if (strval == NULL)
18879 goto free_lstval;
18880 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018881 }
18882 *curval++ = strval;
18883 }
18884 *curval++ = NULL;
18885
18886 write_reg_contents_lst(regname, lstval, -1,
18887 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018888free_lstval:
18889 while (curallocval > allocval)
18890 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018891 vim_free(lstval);
18892 }
18893 else
18894 {
18895 strval = get_tv_string_chk(&argvars[1]);
18896 if (strval == NULL)
18897 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902}
18903
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018904/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018905 * "settabvar()" function
18906 */
18907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018908f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018909{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018910#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018911 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018912 tabpage_T *tp;
18913#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018914 char_u *varname, *tabvarname;
18915 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018916
18917 rettv->vval.v_number = 0;
18918
18919 if (check_restricted() || check_secure())
18920 return;
18921
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018922#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018923 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018924#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018925 varname = get_tv_string_chk(&argvars[1]);
18926 varp = &argvars[2];
18927
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018928 if (varname != NULL && varp != NULL
18929#ifdef FEAT_WINDOWS
18930 && tp != NULL
18931#endif
18932 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018933 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018934#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018935 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018936 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018937#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018938
18939 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18940 if (tabvarname != NULL)
18941 {
18942 STRCPY(tabvarname, "t:");
18943 STRCPY(tabvarname + 2, varname);
18944 set_var(tabvarname, varp, TRUE);
18945 vim_free(tabvarname);
18946 }
18947
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018948#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018949 /* Restore current tabpage */
18950 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018951 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018952#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018953 }
18954}
18955
18956/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018957 * "settabwinvar()" function
18958 */
18959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018961{
18962 setwinvar(argvars, rettv, 1);
18963}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
18965/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018966 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018969f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018971 setwinvar(argvars, rettv, 0);
18972}
18973
18974/*
18975 * "setwinvar()" and "settabwinvar()" functions
18976 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018977
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018979setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018980{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 win_T *win;
18982#ifdef FEAT_WINDOWS
18983 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018984 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018985 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986#endif
18987 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018988 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018990 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
18992 if (check_restricted() || check_secure())
18993 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018994
18995#ifdef FEAT_WINDOWS
18996 if (off == 1)
18997 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18998 else
18999 tp = curtab;
19000#endif
19001 win = find_win_by_nr(&argvars[off], tp);
19002 varname = get_tv_string_chk(&argvars[off + 1]);
19003 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004
19005 if (win != NULL && varname != NULL && varp != NULL)
19006 {
19007#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019008 need_switch_win = !(tp == curtab && win == curwin);
19009 if (!need_switch_win
19010 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019013 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019015 long numval;
19016 char_u *strval;
19017 int error = FALSE;
19018
19019 ++varname;
19020 numval = get_tv_number_chk(varp, &error);
19021 strval = get_tv_string_buf_chk(varp, nbuf);
19022 if (!error && strval != NULL)
19023 set_option_value(varname, numval, strval, OPT_LOCAL);
19024 }
19025 else
19026 {
19027 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19028 if (winvarname != NULL)
19029 {
19030 STRCPY(winvarname, "w:");
19031 STRCPY(winvarname + 2, varname);
19032 set_var(winvarname, varp, TRUE);
19033 vim_free(winvarname);
19034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 }
19036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019038 if (need_switch_win)
19039 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040#endif
19041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042}
19043
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019044#ifdef FEAT_CRYPT
19045/*
19046 * "sha256({string})" function
19047 */
19048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019049f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019050{
19051 char_u *p;
19052
19053 p = get_tv_string(&argvars[0]);
19054 rettv->vval.v_string = vim_strsave(
19055 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19056 rettv->v_type = VAR_STRING;
19057}
19058#endif /* FEAT_CRYPT */
19059
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019061 * "shellescape({string})" function
19062 */
19063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019064f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019065{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019066 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019067 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019068 rettv->v_type = VAR_STRING;
19069}
19070
19071/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019072 * shiftwidth() function
19073 */
19074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019075f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019076{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019077 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019078}
19079
19080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 */
19083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019084f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087
Bram Moolenaar0d660222005-01-07 21:51:51 +000019088 p = get_tv_string(&argvars[0]);
19089 rettv->vval.v_string = vim_strsave(p);
19090 simplify_filename(rettv->vval.v_string); /* simplify in place */
19091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092}
19093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019094#ifdef FEAT_FLOAT
19095/*
19096 * "sin()" function
19097 */
19098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019099f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019100{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019101 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019102
19103 rettv->v_type = VAR_FLOAT;
19104 if (get_float_arg(argvars, &f) == OK)
19105 rettv->vval.v_float = sin(f);
19106 else
19107 rettv->vval.v_float = 0.0;
19108}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019109
19110/*
19111 * "sinh()" function
19112 */
19113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019114f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019115{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019116 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019117
19118 rettv->v_type = VAR_FLOAT;
19119 if (get_float_arg(argvars, &f) == OK)
19120 rettv->vval.v_float = sinh(f);
19121 else
19122 rettv->vval.v_float = 0.0;
19123}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019124#endif
19125
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126static int
19127#ifdef __BORLANDC__
19128 _RTLENTRYF
19129#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019130 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131static int
19132#ifdef __BORLANDC__
19133 _RTLENTRYF
19134#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019135 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019137/* struct used in the array that's given to qsort() */
19138typedef struct
19139{
19140 listitem_T *item;
19141 int idx;
19142} sortItem_T;
19143
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144/* struct storing information about current sort */
19145typedef struct
19146{
19147 int item_compare_ic;
19148 int item_compare_numeric;
19149 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019150#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019151 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019152#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019153 char_u *item_compare_func;
19154 dict_T *item_compare_selfdict;
19155 int item_compare_func_err;
19156 int item_compare_keep_zero;
19157} sortinfo_T;
19158static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019159static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019160#define ITEM_COMPARE_FAIL 999
19161
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019163 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 static int
19166#ifdef __BORLANDC__
19167_RTLENTRYF
19168#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019169item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019172 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019173 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019174 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019175 int res;
19176 char_u numbuf1[NUMBUFLEN];
19177 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019179 si1 = (sortItem_T *)s1;
19180 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019181 tv1 = &si1->item->li_tv;
19182 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019183
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019185 {
19186 long v1 = get_tv_number(tv1);
19187 long v2 = get_tv_number(tv2);
19188
19189 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19190 }
19191
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019192#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019194 {
19195 float_T v1 = get_tv_float(tv1);
19196 float_T v2 = get_tv_float(tv2);
19197
19198 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19199 }
19200#endif
19201
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019202 /* tv2string() puts quotes around a string and allocates memory. Don't do
19203 * that for string variables. Use a single quote when comparing with a
19204 * non-string to do what the docs promise. */
19205 if (tv1->v_type == VAR_STRING)
19206 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019207 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019208 p1 = (char_u *)"'";
19209 else
19210 p1 = tv1->vval.v_string;
19211 }
19212 else
19213 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19214 if (tv2->v_type == VAR_STRING)
19215 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019216 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019217 p2 = (char_u *)"'";
19218 else
19219 p2 = tv2->vval.v_string;
19220 }
19221 else
19222 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019223 if (p1 == NULL)
19224 p1 = (char_u *)"";
19225 if (p2 == NULL)
19226 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019227 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019228 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019230 res = STRICMP(p1, p2);
19231 else
19232 res = STRCMP(p1, p2);
19233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019235 {
19236 double n1, n2;
19237 n1 = strtod((char *)p1, (char **)&p1);
19238 n2 = strtod((char *)p2, (char **)&p2);
19239 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19240 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019241
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019242 /* When the result would be zero, compare the item indexes. Makes the
19243 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019245 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019246
Bram Moolenaar0d660222005-01-07 21:51:51 +000019247 vim_free(tofree1);
19248 vim_free(tofree2);
19249 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250}
19251
19252 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019253#ifdef __BORLANDC__
19254_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019256item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019257{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019258 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019259 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019260 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019261 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019262 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019264 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019265 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019266 return 0;
19267
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019268 si1 = (sortItem_T *)s1;
19269 si2 = (sortItem_T *)s2;
19270
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019271 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019272 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019273 copy_tv(&si1->item->li_tv, &argv[0]);
19274 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019275
19276 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019277 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019278 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019279 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019280 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019281 clear_tv(&argv[0]);
19282 clear_tv(&argv[1]);
19283
19284 if (res == FAIL)
19285 res = ITEM_COMPARE_FAIL;
19286 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019287 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19288 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019289 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019290 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019291
19292 /* When the result would be zero, compare the pointers themselves. Makes
19293 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019294 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019295 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019296
Bram Moolenaar0d660222005-01-07 21:51:51 +000019297 return res;
19298}
19299
19300/*
19301 * "sort({list})" function
19302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019304do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305{
Bram Moolenaar33570922005-01-25 22:26:29 +000019306 list_T *l;
19307 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019308 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019309 sortinfo_T *old_sortinfo;
19310 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311 long len;
19312 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313
Bram Moolenaar0b962472016-02-22 22:51:33 +010019314 /* Pointer to current info struct used in compare function. Save and
19315 * restore the current one for nested calls. */
19316 old_sortinfo = sortinfo;
19317 sortinfo = &info;
19318
Bram Moolenaar0d660222005-01-07 21:51:51 +000019319 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019320 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 else
19322 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019323 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019324 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019325 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19326 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019327 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 rettv->vval.v_list = l;
19329 rettv->v_type = VAR_LIST;
19330 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331
Bram Moolenaar0d660222005-01-07 21:51:51 +000019332 len = list_len(l);
19333 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 info.item_compare_ic = FALSE;
19337 info.item_compare_numeric = FALSE;
19338 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019339#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019340 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019341#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019342 info.item_compare_func = NULL;
19343 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019344 if (argvars[1].v_type != VAR_UNKNOWN)
19345 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019346 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019347 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019348 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349 else
19350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019351 int error = FALSE;
19352
19353 i = get_tv_number_chk(&argvars[1], &error);
19354 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019355 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019357 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019358 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019359 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019360 else if (i != 0)
19361 {
19362 EMSG(_(e_invarg));
19363 goto theend;
19364 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019366 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019367 if (*info.item_compare_func == NUL)
19368 {
19369 /* empty string means default sort */
19370 info.item_compare_func = NULL;
19371 }
19372 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019373 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019374 info.item_compare_func = NULL;
19375 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019376 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019377 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019378 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019379 info.item_compare_func = NULL;
19380 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019381 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019382#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019383 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019384 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019385 info.item_compare_func = NULL;
19386 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019387 }
19388#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019389 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019390 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019391 info.item_compare_func = NULL;
19392 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019393 }
19394 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019396
19397 if (argvars[2].v_type != VAR_UNKNOWN)
19398 {
19399 /* optional third argument: {dict} */
19400 if (argvars[2].v_type != VAR_DICT)
19401 {
19402 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019403 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019404 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019405 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019406 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019410 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019412 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413
Bram Moolenaar327aa022014-03-25 18:24:23 +010019414 i = 0;
19415 if (sort)
19416 {
19417 /* sort(): ptrs will be the list to sort */
19418 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019419 {
19420 ptrs[i].item = li;
19421 ptrs[i].idx = i;
19422 ++i;
19423 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019424
Bram Moolenaar0b962472016-02-22 22:51:33 +010019425 info.item_compare_func_err = FALSE;
19426 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019427 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019428 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019429 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019430 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019431 EMSG(_("E702: Sort compare function failed"));
19432 else
19433 {
19434 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019435 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019436 info.item_compare_func == NULL
19437 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019438
Bram Moolenaar0b962472016-02-22 22:51:33 +010019439 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019440 {
19441 /* Clear the List and append the items in sorted order. */
19442 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19443 l->lv_len = 0;
19444 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019445 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019446 }
19447 }
19448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019450 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019451 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019452
19453 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019454 info.item_compare_func_err = FALSE;
19455 info.item_compare_keep_zero = TRUE;
19456 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019457 ? item_compare2 : item_compare;
19458
19459 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19460 li = li->li_next)
19461 {
19462 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19463 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019464 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019465 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019466 {
19467 EMSG(_("E882: Uniq compare function failed"));
19468 break;
19469 }
19470 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019471
Bram Moolenaar0b962472016-02-22 22:51:33 +010019472 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019473 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019474 while (--i >= 0)
19475 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019476 li = ptrs[i].item->li_next;
19477 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019478 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019479 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019480 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019481 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019482 list_fix_watch(l, li);
19483 listitem_free(li);
19484 l->lv_len--;
19485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019487 }
19488
19489 vim_free(ptrs);
19490 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019491theend:
19492 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019493}
19494
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019495/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019496 * "sort({list})" function
19497 */
19498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019499f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019500{
19501 do_sort_uniq(argvars, rettv, TRUE);
19502}
19503
19504/*
19505 * "uniq({list})" function
19506 */
19507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019508f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019509{
19510 do_sort_uniq(argvars, rettv, FALSE);
19511}
19512
19513/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019514 * "soundfold({word})" function
19515 */
19516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019517f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019518{
19519 char_u *s;
19520
19521 rettv->v_type = VAR_STRING;
19522 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019523#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019524 rettv->vval.v_string = eval_soundfold(s);
19525#else
19526 rettv->vval.v_string = vim_strsave(s);
19527#endif
19528}
19529
19530/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019531 * "spellbadword()" function
19532 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019534f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019535{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019536 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019537 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019538 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019539
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019540 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019541 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019542
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019543#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019544 if (argvars[0].v_type == VAR_UNKNOWN)
19545 {
19546 /* Find the start and length of the badly spelled word. */
19547 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19548 if (len != 0)
19549 word = ml_get_cursor();
19550 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019551 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019552 {
19553 char_u *str = get_tv_string_chk(&argvars[0]);
19554 int capcol = -1;
19555
19556 if (str != NULL)
19557 {
19558 /* Check the argument for spelling. */
19559 while (*str != NUL)
19560 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019561 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019562 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019563 {
19564 word = str;
19565 break;
19566 }
19567 str += len;
19568 }
19569 }
19570 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019571#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019573 list_append_string(rettv->vval.v_list, word, len);
19574 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019575 attr == HLF_SPB ? "bad" :
19576 attr == HLF_SPR ? "rare" :
19577 attr == HLF_SPL ? "local" :
19578 attr == HLF_SPC ? "caps" :
19579 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019580}
19581
19582/*
19583 * "spellsuggest()" function
19584 */
19585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019586f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019587{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019588#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019589 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019590 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019591 int maxcount;
19592 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019593 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019594 listitem_T *li;
19595 int need_capital = FALSE;
19596#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019598 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019599 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019600
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019601#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019602 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019603 {
19604 str = get_tv_string(&argvars[0]);
19605 if (argvars[1].v_type != VAR_UNKNOWN)
19606 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019607 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019608 if (maxcount <= 0)
19609 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019610 if (argvars[2].v_type != VAR_UNKNOWN)
19611 {
19612 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19613 if (typeerr)
19614 return;
19615 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019616 }
19617 else
19618 maxcount = 25;
19619
Bram Moolenaar4770d092006-01-12 23:22:24 +000019620 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019621
19622 for (i = 0; i < ga.ga_len; ++i)
19623 {
19624 str = ((char_u **)ga.ga_data)[i];
19625
19626 li = listitem_alloc();
19627 if (li == NULL)
19628 vim_free(str);
19629 else
19630 {
19631 li->li_tv.v_type = VAR_STRING;
19632 li->li_tv.v_lock = 0;
19633 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019634 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019635 }
19636 }
19637 ga_clear(&ga);
19638 }
19639#endif
19640}
19641
Bram Moolenaar0d660222005-01-07 21:51:51 +000019642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019643f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019644{
19645 char_u *str;
19646 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019647 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019648 regmatch_T regmatch;
19649 char_u patbuf[NUMBUFLEN];
19650 char_u *save_cpo;
19651 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019652 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019653 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019654 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019655
19656 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19657 save_cpo = p_cpo;
19658 p_cpo = (char_u *)"";
19659
19660 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019661 if (argvars[1].v_type != VAR_UNKNOWN)
19662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019663 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19664 if (pat == NULL)
19665 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019666 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019667 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019668 }
19669 if (pat == NULL || *pat == NUL)
19670 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019671
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019672 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019674 if (typeerr)
19675 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676
Bram Moolenaar0d660222005-01-07 21:51:51 +000019677 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19678 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019680 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019681 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019682 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019683 if (*str == NUL)
19684 match = FALSE; /* empty item at the end */
19685 else
19686 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019687 if (match)
19688 end = regmatch.startp[0];
19689 else
19690 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019691 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19692 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019693 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019694 if (list_append_string(rettv->vval.v_list, str,
19695 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019696 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019697 }
19698 if (!match)
19699 break;
19700 /* Advance to just after the match. */
19701 if (regmatch.endp[0] > str)
19702 col = 0;
19703 else
19704 {
19705 /* Don't get stuck at the same match. */
19706#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019707 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019708#else
19709 col = 1;
19710#endif
19711 }
19712 str = regmatch.endp[0];
19713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714
Bram Moolenaar473de612013-06-08 18:19:48 +020019715 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717
Bram Moolenaar0d660222005-01-07 21:51:51 +000019718 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019721#ifdef FEAT_FLOAT
19722/*
19723 * "sqrt()" function
19724 */
19725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019726f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019727{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019728 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019729
19730 rettv->v_type = VAR_FLOAT;
19731 if (get_float_arg(argvars, &f) == OK)
19732 rettv->vval.v_float = sqrt(f);
19733 else
19734 rettv->vval.v_float = 0.0;
19735}
19736
19737/*
19738 * "str2float()" function
19739 */
19740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019741f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019742{
19743 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19744
19745 if (*p == '+')
19746 p = skipwhite(p + 1);
19747 (void)string2float(p, &rettv->vval.v_float);
19748 rettv->v_type = VAR_FLOAT;
19749}
19750#endif
19751
Bram Moolenaar2c932302006-03-18 21:42:09 +000019752/*
19753 * "str2nr()" function
19754 */
19755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019756f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019757{
19758 int base = 10;
19759 char_u *p;
19760 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019761 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019762
19763 if (argvars[1].v_type != VAR_UNKNOWN)
19764 {
19765 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019766 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019767 {
19768 EMSG(_(e_invarg));
19769 return;
19770 }
19771 }
19772
19773 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019774 if (*p == '+')
19775 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019776 switch (base)
19777 {
19778 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19779 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19780 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19781 default: what = 0;
19782 }
19783 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019784 rettv->vval.v_number = n;
19785}
19786
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787#ifdef HAVE_STRFTIME
19788/*
19789 * "strftime({format}[, {time}])" function
19790 */
19791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019792f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793{
19794 char_u result_buf[256];
19795 struct tm *curtime;
19796 time_t seconds;
19797 char_u *p;
19798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019799 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 seconds = time(NULL);
19804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019806 curtime = localtime(&seconds);
19807 /* MSVC returns NULL for an invalid value of seconds. */
19808 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 else
19811 {
19812# ifdef FEAT_MBYTE
19813 vimconv_T conv;
19814 char_u *enc;
19815
19816 conv.vc_type = CONV_NONE;
19817 enc = enc_locale();
19818 convert_setup(&conv, p_enc, enc);
19819 if (conv.vc_type != CONV_NONE)
19820 p = string_convert(&conv, p, NULL);
19821# endif
19822 if (p != NULL)
19823 (void)strftime((char *)result_buf, sizeof(result_buf),
19824 (char *)p, curtime);
19825 else
19826 result_buf[0] = NUL;
19827
19828# ifdef FEAT_MBYTE
19829 if (conv.vc_type != CONV_NONE)
19830 vim_free(p);
19831 convert_setup(&conv, enc, p_enc);
19832 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 else
19835# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837
19838# ifdef FEAT_MBYTE
19839 /* Release conversion descriptors */
19840 convert_setup(&conv, NULL, NULL);
19841 vim_free(enc);
19842# endif
19843 }
19844}
19845#endif
19846
19847/*
19848 * "stridx()" function
19849 */
19850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019851f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852{
19853 char_u buf[NUMBUFLEN];
19854 char_u *needle;
19855 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019856 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019860 needle = get_tv_string_chk(&argvars[1]);
19861 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019863 if (needle == NULL || haystack == NULL)
19864 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019865
Bram Moolenaar33570922005-01-25 22:26:29 +000019866 if (argvars[2].v_type != VAR_UNKNOWN)
19867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019868 int error = FALSE;
19869
19870 start_idx = get_tv_number_chk(&argvars[2], &error);
19871 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019872 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019873 if (start_idx >= 0)
19874 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019875 }
19876
19877 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19878 if (pos != NULL)
19879 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880}
19881
19882/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019883 * "string()" function
19884 */
19885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019886f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887{
19888 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019889 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019891 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019892 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019893 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019894 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896}
19897
19898/*
19899 * "strlen()" function
19900 */
19901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019902f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904 rettv->vval.v_number = (varnumber_T)(STRLEN(
19905 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906}
19907
19908/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019909 * "strchars()" function
19910 */
19911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019912f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019913{
19914 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019915 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019916#ifdef FEAT_MBYTE
19917 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019918 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019919#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019920
19921 if (argvars[1].v_type != VAR_UNKNOWN)
19922 skipcc = get_tv_number_chk(&argvars[1], NULL);
19923 if (skipcc < 0 || skipcc > 1)
19924 EMSG(_(e_invarg));
19925 else
19926 {
19927#ifdef FEAT_MBYTE
19928 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19929 while (*s != NUL)
19930 {
19931 func_mb_ptr2char_adv(&s);
19932 ++len;
19933 }
19934 rettv->vval.v_number = len;
19935#else
19936 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19937#endif
19938 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019939}
19940
19941/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019942 * "strdisplaywidth()" function
19943 */
19944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019945f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019946{
19947 char_u *s = get_tv_string(&argvars[0]);
19948 int col = 0;
19949
19950 if (argvars[1].v_type != VAR_UNKNOWN)
19951 col = get_tv_number(&argvars[1]);
19952
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019953 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019954}
19955
19956/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019957 * "strwidth()" function
19958 */
19959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019960f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019961{
19962 char_u *s = get_tv_string(&argvars[0]);
19963
19964 rettv->vval.v_number = (varnumber_T)(
19965#ifdef FEAT_MBYTE
19966 mb_string2cells(s, -1)
19967#else
19968 STRLEN(s)
19969#endif
19970 );
19971}
19972
19973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 * "strpart()" function
19975 */
19976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019977f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978{
19979 char_u *p;
19980 int n;
19981 int len;
19982 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019983 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 slen = (int)STRLEN(p);
19987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019988 n = get_tv_number_chk(&argvars[1], &error);
19989 if (error)
19990 len = 0;
19991 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019992 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 else
19994 len = slen - n; /* default len: all bytes that are available. */
19995
19996 /*
19997 * Only return the overlap between the specified part and the actual
19998 * string.
19999 */
20000 if (n < 0)
20001 {
20002 len += n;
20003 n = 0;
20004 }
20005 else if (n > slen)
20006 n = slen;
20007 if (len < 0)
20008 len = 0;
20009 else if (n + len > slen)
20010 len = slen - n;
20011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012 rettv->v_type = VAR_STRING;
20013 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014}
20015
20016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020017 * "strridx()" function
20018 */
20019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020020f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020021{
20022 char_u buf[NUMBUFLEN];
20023 char_u *needle;
20024 char_u *haystack;
20025 char_u *rest;
20026 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020027 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020029 needle = get_tv_string_chk(&argvars[1]);
20030 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020031
20032 rettv->vval.v_number = -1;
20033 if (needle == NULL || haystack == NULL)
20034 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020035
20036 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020037 if (argvars[2].v_type != VAR_UNKNOWN)
20038 {
20039 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020040 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020041 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020042 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020043 }
20044 else
20045 end_idx = haystack_len;
20046
Bram Moolenaar0d660222005-01-07 21:51:51 +000020047 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020048 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020049 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020050 lastmatch = haystack + end_idx;
20051 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020052 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020053 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020054 for (rest = haystack; *rest != '\0'; ++rest)
20055 {
20056 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020057 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020058 break;
20059 lastmatch = rest;
20060 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020061 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020062
20063 if (lastmatch == NULL)
20064 rettv->vval.v_number = -1;
20065 else
20066 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20067}
20068
20069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070 * "strtrans()" function
20071 */
20072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020073f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->v_type = VAR_STRING;
20076 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020080 * "submatch()" function
20081 */
20082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020084{
Bram Moolenaar41571762014-04-02 19:00:58 +020020085 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020086 int no;
20087 int retList = 0;
20088
20089 no = (int)get_tv_number_chk(&argvars[0], &error);
20090 if (error)
20091 return;
20092 error = FALSE;
20093 if (argvars[1].v_type != VAR_UNKNOWN)
20094 retList = get_tv_number_chk(&argvars[1], &error);
20095 if (error)
20096 return;
20097
20098 if (retList == 0)
20099 {
20100 rettv->v_type = VAR_STRING;
20101 rettv->vval.v_string = reg_submatch(no);
20102 }
20103 else
20104 {
20105 rettv->v_type = VAR_LIST;
20106 rettv->vval.v_list = reg_submatch_list(no);
20107 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020108}
20109
20110/*
20111 * "substitute()" function
20112 */
20113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020114f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020115{
20116 char_u patbuf[NUMBUFLEN];
20117 char_u subbuf[NUMBUFLEN];
20118 char_u flagsbuf[NUMBUFLEN];
20119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020120 char_u *str = get_tv_string_chk(&argvars[0]);
20121 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20122 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20123 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20124
Bram Moolenaar0d660222005-01-07 21:51:51 +000020125 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020126 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20127 rettv->vval.v_string = NULL;
20128 else
20129 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020130}
20131
20132/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020133 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020136f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137{
20138 int id = 0;
20139#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020140 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 long col;
20142 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020143 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020145 lnum = get_tv_lnum(argvars); /* -1 on type error */
20146 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20147 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020149 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020150 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020151 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152#endif
20153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020154 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155}
20156
20157/*
20158 * "synIDattr(id, what [, mode])" function
20159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020161f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162{
20163 char_u *p = NULL;
20164#ifdef FEAT_SYN_HL
20165 int id;
20166 char_u *what;
20167 char_u *mode;
20168 char_u modebuf[NUMBUFLEN];
20169 int modec;
20170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020171 id = get_tv_number(&argvars[0]);
20172 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020173 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020177 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 modec = 0; /* replace invalid with current */
20179 }
20180 else
20181 {
20182#ifdef FEAT_GUI
20183 if (gui.in_use)
20184 modec = 'g';
20185 else
20186#endif
20187 if (t_colors > 1)
20188 modec = 'c';
20189 else
20190 modec = 't';
20191 }
20192
20193
20194 switch (TOLOWER_ASC(what[0]))
20195 {
20196 case 'b':
20197 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20198 p = highlight_color(id, what, modec);
20199 else /* bold */
20200 p = highlight_has_attr(id, HL_BOLD, modec);
20201 break;
20202
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020203 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 p = highlight_color(id, what, modec);
20205 break;
20206
20207 case 'i':
20208 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20209 p = highlight_has_attr(id, HL_INVERSE, modec);
20210 else /* italic */
20211 p = highlight_has_attr(id, HL_ITALIC, modec);
20212 break;
20213
20214 case 'n': /* name */
20215 p = get_highlight_name(NULL, id - 1);
20216 break;
20217
20218 case 'r': /* reverse */
20219 p = highlight_has_attr(id, HL_INVERSE, modec);
20220 break;
20221
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020222 case 's':
20223 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20224 p = highlight_color(id, what, modec);
20225 else /* standout */
20226 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 break;
20228
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020229 case 'u':
20230 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20231 /* underline */
20232 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20233 else
20234 /* undercurl */
20235 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 break;
20237 }
20238
20239 if (p != NULL)
20240 p = vim_strsave(p);
20241#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020242 rettv->v_type = VAR_STRING;
20243 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244}
20245
20246/*
20247 * "synIDtrans(id)" function
20248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020250f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251{
20252 int id;
20253
20254#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256
20257 if (id > 0)
20258 id = syn_get_final_id(id);
20259 else
20260#endif
20261 id = 0;
20262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264}
20265
20266/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020267 * "synconcealed(lnum, col)" function
20268 */
20269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020270f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020271{
20272#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20273 long lnum;
20274 long col;
20275 int syntax_flags = 0;
20276 int cchar;
20277 int matchid = 0;
20278 char_u str[NUMBUFLEN];
20279#endif
20280
20281 rettv->v_type = VAR_LIST;
20282 rettv->vval.v_list = NULL;
20283
20284#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20285 lnum = get_tv_lnum(argvars); /* -1 on type error */
20286 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20287
20288 vim_memset(str, NUL, sizeof(str));
20289
20290 if (rettv_list_alloc(rettv) != FAIL)
20291 {
20292 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20293 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20294 && curwin->w_p_cole > 0)
20295 {
20296 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20297 syntax_flags = get_syntax_info(&matchid);
20298
20299 /* get the conceal character */
20300 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20301 {
20302 cchar = syn_get_sub_char();
20303 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20304 cchar = lcs_conceal;
20305 if (cchar != NUL)
20306 {
20307# ifdef FEAT_MBYTE
20308 if (has_mbyte)
20309 (*mb_char2bytes)(cchar, str);
20310 else
20311# endif
20312 str[0] = cchar;
20313 }
20314 }
20315 }
20316
20317 list_append_number(rettv->vval.v_list,
20318 (syntax_flags & HL_CONCEAL) != 0);
20319 /* -1 to auto-determine strlen */
20320 list_append_string(rettv->vval.v_list, str, -1);
20321 list_append_number(rettv->vval.v_list, matchid);
20322 }
20323#endif
20324}
20325
20326/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020327 * "synstack(lnum, col)" function
20328 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020330f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020331{
20332#ifdef FEAT_SYN_HL
20333 long lnum;
20334 long col;
20335 int i;
20336 int id;
20337#endif
20338
20339 rettv->v_type = VAR_LIST;
20340 rettv->vval.v_list = NULL;
20341
20342#ifdef FEAT_SYN_HL
20343 lnum = get_tv_lnum(argvars); /* -1 on type error */
20344 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20345
20346 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020347 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020348 && rettv_list_alloc(rettv) != FAIL)
20349 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020350 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020351 for (i = 0; ; ++i)
20352 {
20353 id = syn_get_stack_item(i);
20354 if (id < 0)
20355 break;
20356 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20357 break;
20358 }
20359 }
20360#endif
20361}
20362
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020364get_cmd_output_as_rettv(
20365 typval_T *argvars,
20366 typval_T *rettv,
20367 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020369 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020371 char_u *infile = NULL;
20372 char_u buf[NUMBUFLEN];
20373 int err = FALSE;
20374 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020375 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020376 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378 rettv->v_type = VAR_STRING;
20379 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020380 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020381 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020383 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020384 {
20385 /*
20386 * Write the string to a temp file, to be used for input of the shell
20387 * command.
20388 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020389 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020390 {
20391 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020392 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020393 }
20394
20395 fd = mch_fopen((char *)infile, WRITEBIN);
20396 if (fd == NULL)
20397 {
20398 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020399 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020400 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020401 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020402 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020403 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20404 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020405 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020406 else
20407 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020408 size_t len;
20409
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020410 p = get_tv_string_buf_chk(&argvars[1], buf);
20411 if (p == NULL)
20412 {
20413 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020414 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020415 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020416 len = STRLEN(p);
20417 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020418 err = TRUE;
20419 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020420 if (fclose(fd) != 0)
20421 err = TRUE;
20422 if (err)
20423 {
20424 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020425 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020426 }
20427 }
20428
Bram Moolenaar52a72462014-08-29 15:53:52 +020020429 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20430 * echoes typeahead, that messes up the display. */
20431 if (!msg_silent)
20432 flags += SHELL_COOKED;
20433
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020434 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020436 int len;
20437 listitem_T *li;
20438 char_u *s = NULL;
20439 char_u *start;
20440 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020441 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442
Bram Moolenaar52a72462014-08-29 15:53:52 +020020443 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020444 if (res == NULL)
20445 goto errret;
20446
20447 list = list_alloc();
20448 if (list == NULL)
20449 goto errret;
20450
20451 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020453 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020454 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020455 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020456 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020458 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020459 if (s == NULL)
20460 goto errret;
20461
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020462 for (p = s; start < end; ++p, ++start)
20463 *p = *start == NUL ? NL : *start;
20464 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020465
20466 li = listitem_alloc();
20467 if (li == NULL)
20468 {
20469 vim_free(s);
20470 goto errret;
20471 }
20472 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020473 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020474 li->li_tv.vval.v_string = s;
20475 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020477
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020478 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020479 rettv->v_type = VAR_LIST;
20480 rettv->vval.v_list = list;
20481 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020483 else
20484 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020485 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020486#ifdef USE_CR
20487 /* translate <CR> into <NL> */
20488 if (res != NULL)
20489 {
20490 char_u *s;
20491
20492 for (s = res; *s; ++s)
20493 {
20494 if (*s == CAR)
20495 *s = NL;
20496 }
20497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498#else
20499# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020500 /* translate <CR><NL> into <NL> */
20501 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020503 char_u *s, *d;
20504
20505 d = res;
20506 for (s = res; *s; ++s)
20507 {
20508 if (s[0] == CAR && s[1] == NL)
20509 ++s;
20510 *d++ = *s;
20511 }
20512 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514# endif
20515#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020516 rettv->vval.v_string = res;
20517 res = NULL;
20518 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020519
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020520errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020521 if (infile != NULL)
20522 {
20523 mch_remove(infile);
20524 vim_free(infile);
20525 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020526 if (res != NULL)
20527 vim_free(res);
20528 if (list != NULL)
20529 list_free(list, TRUE);
20530}
20531
20532/*
20533 * "system()" function
20534 */
20535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020537{
20538 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20539}
20540
20541/*
20542 * "systemlist()" function
20543 */
20544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020546{
20547 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020548}
20549
20550/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020551 * "tabpagebuflist()" function
20552 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020554f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020555{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020556#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020557 tabpage_T *tp;
20558 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020559
20560 if (argvars[0].v_type == VAR_UNKNOWN)
20561 wp = firstwin;
20562 else
20563 {
20564 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20565 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020566 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020567 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020568 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020569 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020570 for (; wp != NULL; wp = wp->w_next)
20571 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020572 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020573 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574 }
20575#endif
20576}
20577
20578
20579/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020580 * "tabpagenr()" function
20581 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020583f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020584{
20585 int nr = 1;
20586#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020587 char_u *arg;
20588
20589 if (argvars[0].v_type != VAR_UNKNOWN)
20590 {
20591 arg = get_tv_string_chk(&argvars[0]);
20592 nr = 0;
20593 if (arg != NULL)
20594 {
20595 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020596 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020597 else
20598 EMSG2(_(e_invexpr2), arg);
20599 }
20600 }
20601 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020602 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020603#endif
20604 rettv->vval.v_number = nr;
20605}
20606
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020607
20608#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020609static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020610
20611/*
20612 * Common code for tabpagewinnr() and winnr().
20613 */
20614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020615get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020616{
20617 win_T *twin;
20618 int nr = 1;
20619 win_T *wp;
20620 char_u *arg;
20621
20622 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20623 if (argvar->v_type != VAR_UNKNOWN)
20624 {
20625 arg = get_tv_string_chk(argvar);
20626 if (arg == NULL)
20627 nr = 0; /* type error; errmsg already given */
20628 else if (STRCMP(arg, "$") == 0)
20629 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20630 else if (STRCMP(arg, "#") == 0)
20631 {
20632 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20633 if (twin == NULL)
20634 nr = 0;
20635 }
20636 else
20637 {
20638 EMSG2(_(e_invexpr2), arg);
20639 nr = 0;
20640 }
20641 }
20642
20643 if (nr > 0)
20644 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20645 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020646 {
20647 if (wp == NULL)
20648 {
20649 /* didn't find it in this tabpage */
20650 nr = 0;
20651 break;
20652 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020653 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020654 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020655 return nr;
20656}
20657#endif
20658
20659/*
20660 * "tabpagewinnr()" function
20661 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020663f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020664{
20665 int nr = 1;
20666#ifdef FEAT_WINDOWS
20667 tabpage_T *tp;
20668
20669 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20670 if (tp == NULL)
20671 nr = 0;
20672 else
20673 nr = get_winnr(tp, &argvars[1]);
20674#endif
20675 rettv->vval.v_number = nr;
20676}
20677
20678
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020679/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020680 * "tagfiles()" function
20681 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020683f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020684{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020685 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020686 tagname_T tn;
20687 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020688
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020689 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020690 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020691 fname = alloc(MAXPATHL);
20692 if (fname == NULL)
20693 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020694
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020695 for (first = TRUE; ; first = FALSE)
20696 if (get_tagfname(&tn, first, fname) == FAIL
20697 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020698 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020699 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020700 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020701}
20702
20703/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020704 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020705 */
20706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020707f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020708{
20709 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020710
20711 tag_pattern = get_tv_string(&argvars[0]);
20712
20713 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020714 if (*tag_pattern == NUL)
20715 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020716
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020717 if (rettv_list_alloc(rettv) == OK)
20718 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020719}
20720
20721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 * "tempname()" function
20723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020725f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726{
20727 static int x = 'A';
20728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020729 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020730 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731
20732 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20733 * names. Skip 'I' and 'O', they are used for shell redirection. */
20734 do
20735 {
20736 if (x == 'Z')
20737 x = '0';
20738 else if (x == '9')
20739 x = 'A';
20740 else
20741 {
20742#ifdef EBCDIC
20743 if (x == 'I')
20744 x = 'J';
20745 else if (x == 'R')
20746 x = 'S';
20747 else
20748#endif
20749 ++x;
20750 }
20751 } while (x == 'I' || x == 'O');
20752}
20753
20754/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020755 * "test(list)" function: Just checking the walls...
20756 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020758f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020759{
20760 /* Used for unit testing. Change the code below to your liking. */
20761#if 0
20762 listitem_T *li;
20763 list_T *l;
20764 char_u *bad, *good;
20765
20766 if (argvars[0].v_type != VAR_LIST)
20767 return;
20768 l = argvars[0].vval.v_list;
20769 if (l == NULL)
20770 return;
20771 li = l->lv_first;
20772 if (li == NULL)
20773 return;
20774 bad = get_tv_string(&li->li_tv);
20775 li = li->li_next;
20776 if (li == NULL)
20777 return;
20778 good = get_tv_string(&li->li_tv);
20779 rettv->vval.v_number = test_edit_score(bad, good);
20780#endif
20781}
20782
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020783#ifdef FEAT_FLOAT
20784/*
20785 * "tan()" function
20786 */
20787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020788f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020789{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020790 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020791
20792 rettv->v_type = VAR_FLOAT;
20793 if (get_float_arg(argvars, &f) == OK)
20794 rettv->vval.v_float = tan(f);
20795 else
20796 rettv->vval.v_float = 0.0;
20797}
20798
20799/*
20800 * "tanh()" function
20801 */
20802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020803f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020804{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020805 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020806
20807 rettv->v_type = VAR_FLOAT;
20808 if (get_float_arg(argvars, &f) == OK)
20809 rettv->vval.v_float = tanh(f);
20810 else
20811 rettv->vval.v_float = 0.0;
20812}
20813#endif
20814
Bram Moolenaard52d9742005-08-21 22:20:28 +000020815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 * "tolower(string)" function
20817 */
20818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020819f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820{
20821 char_u *p;
20822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020823 p = vim_strsave(get_tv_string(&argvars[0]));
20824 rettv->v_type = VAR_STRING;
20825 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826
20827 if (p != NULL)
20828 while (*p != NUL)
20829 {
20830#ifdef FEAT_MBYTE
20831 int l;
20832
20833 if (enc_utf8)
20834 {
20835 int c, lc;
20836
20837 c = utf_ptr2char(p);
20838 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020839 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 /* TODO: reallocate string when byte count changes. */
20841 if (utf_char2len(lc) == l)
20842 utf_char2bytes(lc, p);
20843 p += l;
20844 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020845 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 p += l; /* skip multi-byte character */
20847 else
20848#endif
20849 {
20850 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20851 ++p;
20852 }
20853 }
20854}
20855
20856/*
20857 * "toupper(string)" function
20858 */
20859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020860f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020862 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020863 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864}
20865
20866/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020867 * "tr(string, fromstr, tostr)" function
20868 */
20869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020870f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020871{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020872 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020873 char_u *fromstr;
20874 char_u *tostr;
20875 char_u *p;
20876#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020877 int inlen;
20878 int fromlen;
20879 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020880 int idx;
20881 char_u *cpstr;
20882 int cplen;
20883 int first = TRUE;
20884#endif
20885 char_u buf[NUMBUFLEN];
20886 char_u buf2[NUMBUFLEN];
20887 garray_T ga;
20888
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020889 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020890 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20891 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020892
20893 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 rettv->v_type = VAR_STRING;
20895 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020896 if (fromstr == NULL || tostr == NULL)
20897 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020898 ga_init2(&ga, (int)sizeof(char), 80);
20899
20900#ifdef FEAT_MBYTE
20901 if (!has_mbyte)
20902#endif
20903 /* not multi-byte: fromstr and tostr must be the same length */
20904 if (STRLEN(fromstr) != STRLEN(tostr))
20905 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020906#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020907error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020908#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020909 EMSG2(_(e_invarg2), fromstr);
20910 ga_clear(&ga);
20911 return;
20912 }
20913
20914 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020915 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020916 {
20917#ifdef FEAT_MBYTE
20918 if (has_mbyte)
20919 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020920 inlen = (*mb_ptr2len)(in_str);
20921 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020922 cplen = inlen;
20923 idx = 0;
20924 for (p = fromstr; *p != NUL; p += fromlen)
20925 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020926 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020927 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020928 {
20929 for (p = tostr; *p != NUL; p += tolen)
20930 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020931 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020932 if (idx-- == 0)
20933 {
20934 cplen = tolen;
20935 cpstr = p;
20936 break;
20937 }
20938 }
20939 if (*p == NUL) /* tostr is shorter than fromstr */
20940 goto error;
20941 break;
20942 }
20943 ++idx;
20944 }
20945
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020946 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020947 {
20948 /* Check that fromstr and tostr have the same number of
20949 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020950 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020951 first = FALSE;
20952 for (p = tostr; *p != NUL; p += tolen)
20953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020954 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020955 --idx;
20956 }
20957 if (idx != 0)
20958 goto error;
20959 }
20960
Bram Moolenaarcde88542015-08-11 19:14:00 +020020961 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020962 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020963 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020964
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020965 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020966 }
20967 else
20968#endif
20969 {
20970 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020971 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020972 if (p != NULL)
20973 ga_append(&ga, tostr[p - fromstr]);
20974 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020975 ga_append(&ga, *in_str);
20976 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020977 }
20978 }
20979
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020980 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020981 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020982 ga_append(&ga, NUL);
20983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020984 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020985}
20986
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987#ifdef FEAT_FLOAT
20988/*
20989 * "trunc({float})" function
20990 */
20991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020993{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020994 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020995
20996 rettv->v_type = VAR_FLOAT;
20997 if (get_float_arg(argvars, &f) == OK)
20998 /* trunc() is not in C90, use floor() or ceil() instead. */
20999 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21000 else
21001 rettv->vval.v_float = 0.0;
21002}
21003#endif
21004
Bram Moolenaar8299df92004-07-10 09:47:34 +000021005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 * "type(expr)" function
21007 */
21008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021011 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021012
21013 switch (argvars[0].v_type)
21014 {
21015 case VAR_NUMBER: n = 0; break;
21016 case VAR_STRING: n = 1; break;
21017 case VAR_FUNC: n = 2; break;
21018 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021019 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021020 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021021 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021022 if (argvars[0].vval.v_number == VVAL_FALSE
21023 || argvars[0].vval.v_number == VVAL_TRUE)
21024 n = 6;
21025 else
21026 n = 7;
21027 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021028 case VAR_JOB: n = 8; break;
21029 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021030 case VAR_UNKNOWN:
21031 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21032 n = -1;
21033 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021034 }
21035 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036}
21037
21038/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021039 * "undofile(name)" function
21040 */
21041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021042f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021043{
21044 rettv->v_type = VAR_STRING;
21045#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021046 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021047 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021048
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021049 if (*fname == NUL)
21050 {
21051 /* If there is no file name there will be no undo file. */
21052 rettv->vval.v_string = NULL;
21053 }
21054 else
21055 {
21056 char_u *ffname = FullName_save(fname, FALSE);
21057
21058 if (ffname != NULL)
21059 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21060 vim_free(ffname);
21061 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021062 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021063#else
21064 rettv->vval.v_string = NULL;
21065#endif
21066}
21067
21068/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021069 * "undotree()" function
21070 */
21071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021072f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021073{
21074 if (rettv_dict_alloc(rettv) == OK)
21075 {
21076 dict_T *dict = rettv->vval.v_dict;
21077 list_T *list;
21078
Bram Moolenaar730cde92010-06-27 05:18:54 +020021079 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021080 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021081 dict_add_nr_str(dict, "save_last",
21082 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021083 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21084 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021085 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021086
21087 list = list_alloc();
21088 if (list != NULL)
21089 {
21090 u_eval_tree(curbuf->b_u_oldhead, list);
21091 dict_add_list(dict, "entries", list);
21092 }
21093 }
21094}
21095
21096/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021097 * "values(dict)" function
21098 */
21099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021100f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021101{
21102 dict_list(argvars, rettv, 1);
21103}
21104
21105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 * "virtcol(string)" function
21107 */
21108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021109f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110{
21111 colnr_T vcol = 0;
21112 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021113 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021115 fp = var2fpos(&argvars[0], FALSE, &fnum);
21116 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21117 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 {
21119 getvvcol(curwin, fp, NULL, NULL, &vcol);
21120 ++vcol;
21121 }
21122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021123 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124}
21125
21126/*
21127 * "visualmode()" function
21128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021130f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132 char_u str[2];
21133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021134 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 str[0] = curbuf->b_visual_mode_eval;
21136 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021137 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138
21139 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021140 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142}
21143
21144/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021145 * "wildmenumode()" function
21146 */
21147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021148f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021149{
21150#ifdef FEAT_WILDMENU
21151 if (wild_menu_showing)
21152 rettv->vval.v_number = 1;
21153#endif
21154}
21155
21156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 * "winbufnr(nr)" function
21158 */
21159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021160f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161{
21162 win_T *wp;
21163
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021164 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021168 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169}
21170
21171/*
21172 * "wincol()" function
21173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021175f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176{
21177 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021178 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179}
21180
21181/*
21182 * "winheight(nr)" function
21183 */
21184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021185f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186{
21187 win_T *wp;
21188
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021189 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194}
21195
21196/*
21197 * "winline()" function
21198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021200f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201{
21202 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021203 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204}
21205
21206/*
21207 * "winnr()" function
21208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211{
21212 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021213
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021215 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218}
21219
21220/*
21221 * "winrestcmd()" function
21222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021224f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225{
21226#ifdef FEAT_WINDOWS
21227 win_T *wp;
21228 int winnr = 1;
21229 garray_T ga;
21230 char_u buf[50];
21231
21232 ga_init2(&ga, (int)sizeof(char), 70);
21233 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21234 {
21235 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21236 ga_concat(&ga, buf);
21237# ifdef FEAT_VERTSPLIT
21238 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21239 ga_concat(&ga, buf);
21240# endif
21241 ++winnr;
21242 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021243 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021247 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021249 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250}
21251
21252/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021253 * "winrestview()" function
21254 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021256f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021257{
21258 dict_T *dict;
21259
21260 if (argvars[0].v_type != VAR_DICT
21261 || (dict = argvars[0].vval.v_dict) == NULL)
21262 EMSG(_(e_invarg));
21263 else
21264 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021265 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21266 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21267 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21268 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021269#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021270 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21271 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021272#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021273 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21274 {
21275 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21276 curwin->w_set_curswant = FALSE;
21277 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021278
Bram Moolenaar82c25852014-05-28 16:47:16 +020021279 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21280 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021281#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021282 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21283 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021284#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021285 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21286 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21287 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21288 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021289
21290 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021291 win_new_height(curwin, curwin->w_height);
21292# ifdef FEAT_VERTSPLIT
21293 win_new_width(curwin, W_WIDTH(curwin));
21294# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021295 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021296
Bram Moolenaarb851a962014-10-31 15:45:52 +010021297 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021298 curwin->w_topline = 1;
21299 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21300 curwin->w_topline = curbuf->b_ml.ml_line_count;
21301#ifdef FEAT_DIFF
21302 check_topfill(curwin, TRUE);
21303#endif
21304 }
21305}
21306
21307/*
21308 * "winsaveview()" function
21309 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021311f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021312{
21313 dict_T *dict;
21314
Bram Moolenaara800b422010-06-27 01:15:55 +020021315 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021316 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021317 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021318
21319 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21320 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21321#ifdef FEAT_VIRTUALEDIT
21322 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21323#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021324 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021325 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21326
21327 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21328#ifdef FEAT_DIFF
21329 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21330#endif
21331 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21332 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21333}
21334
21335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 * "winwidth(nr)" function
21337 */
21338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340{
21341 win_T *wp;
21342
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021343 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021345 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 else
21347#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021348 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021350 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351#endif
21352}
21353
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021355 * "wordcount()" function
21356 */
21357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021358f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021359{
21360 if (rettv_dict_alloc(rettv) == FAIL)
21361 return;
21362 cursor_pos_info(rettv->vval.v_dict);
21363}
21364
21365/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021366 * Write list of strings to file
21367 */
21368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021369write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021370{
21371 listitem_T *li;
21372 int c;
21373 int ret = OK;
21374 char_u *s;
21375
21376 for (li = list->lv_first; li != NULL; li = li->li_next)
21377 {
21378 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21379 {
21380 if (*s == '\n')
21381 c = putc(NUL, fd);
21382 else
21383 c = putc(*s, fd);
21384 if (c == EOF)
21385 {
21386 ret = FAIL;
21387 break;
21388 }
21389 }
21390 if (!binary || li->li_next != NULL)
21391 if (putc('\n', fd) == EOF)
21392 {
21393 ret = FAIL;
21394 break;
21395 }
21396 if (ret == FAIL)
21397 {
21398 EMSG(_(e_write));
21399 break;
21400 }
21401 }
21402 return ret;
21403}
21404
21405/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021406 * "writefile()" function
21407 */
21408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021409f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021410{
21411 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021412 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021413 char_u *fname;
21414 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021415 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021416
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021417 if (check_restricted() || check_secure())
21418 return;
21419
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021420 if (argvars[0].v_type != VAR_LIST)
21421 {
21422 EMSG2(_(e_listarg), "writefile()");
21423 return;
21424 }
21425 if (argvars[0].vval.v_list == NULL)
21426 return;
21427
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021428 if (argvars[2].v_type != VAR_UNKNOWN)
21429 {
21430 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21431 binary = TRUE;
21432 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21433 append = TRUE;
21434 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021435
21436 /* Always open the file in binary mode, library functions have a mind of
21437 * their own about CR-LF conversion. */
21438 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021439 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21440 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021441 {
21442 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21443 ret = -1;
21444 }
21445 else
21446 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021447 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21448 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021449 fclose(fd);
21450 }
21451
21452 rettv->vval.v_number = ret;
21453}
21454
21455/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021456 * "xor(expr, expr)" function
21457 */
21458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021459f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021460{
21461 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21462 ^ get_tv_number_chk(&argvars[1], NULL);
21463}
21464
21465
21466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021468 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 */
21470 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471var2fpos(
21472 typval_T *varp,
21473 int dollar_lnum, /* TRUE when $ is last line */
21474 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021476 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021478 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479
Bram Moolenaara5525202006-03-02 22:52:09 +000021480 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021481 if (varp->v_type == VAR_LIST)
21482 {
21483 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021484 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021485 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021486 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021487
21488 l = varp->vval.v_list;
21489 if (l == NULL)
21490 return NULL;
21491
21492 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021493 pos.lnum = list_find_nr(l, 0L, &error);
21494 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021495 return NULL; /* invalid line number */
21496
21497 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021498 pos.col = list_find_nr(l, 1L, &error);
21499 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021500 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021501 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021502
21503 /* We accept "$" for the column number: last column. */
21504 li = list_find(l, 1L);
21505 if (li != NULL && li->li_tv.v_type == VAR_STRING
21506 && li->li_tv.vval.v_string != NULL
21507 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21508 pos.col = len + 1;
21509
Bram Moolenaara5525202006-03-02 22:52:09 +000021510 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021511 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021512 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021513 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021514
Bram Moolenaara5525202006-03-02 22:52:09 +000021515#ifdef FEAT_VIRTUALEDIT
21516 /* Get the virtual offset. Defaults to zero. */
21517 pos.coladd = list_find_nr(l, 2L, &error);
21518 if (error)
21519 pos.coladd = 0;
21520#endif
21521
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021522 return &pos;
21523 }
21524
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021525 name = get_tv_string_chk(varp);
21526 if (name == NULL)
21527 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021528 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021530 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21531 {
21532 if (VIsual_active)
21533 return &VIsual;
21534 return &curwin->w_cursor;
21535 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021536 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021538 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21540 return NULL;
21541 return pp;
21542 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021543
21544#ifdef FEAT_VIRTUALEDIT
21545 pos.coladd = 0;
21546#endif
21547
Bram Moolenaar477933c2007-07-17 14:32:23 +000021548 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021549 {
21550 pos.col = 0;
21551 if (name[1] == '0') /* "w0": first visible line */
21552 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021553 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021554 pos.lnum = curwin->w_topline;
21555 return &pos;
21556 }
21557 else if (name[1] == '$') /* "w$": last visible line */
21558 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021559 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021560 pos.lnum = curwin->w_botline - 1;
21561 return &pos;
21562 }
21563 }
21564 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021566 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 {
21568 pos.lnum = curbuf->b_ml.ml_line_count;
21569 pos.col = 0;
21570 }
21571 else
21572 {
21573 pos.lnum = curwin->w_cursor.lnum;
21574 pos.col = (colnr_T)STRLEN(ml_get_curline());
21575 }
21576 return &pos;
21577 }
21578 return NULL;
21579}
21580
21581/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021582 * Convert list in "arg" into a position and optional file number.
21583 * When "fnump" is NULL there is no file number, only 3 items.
21584 * Note that the column is passed on as-is, the caller may want to decrement
21585 * it to use 1 for the first column.
21586 * Return FAIL when conversion is not possible, doesn't check the position for
21587 * validity.
21588 */
21589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590list2fpos(
21591 typval_T *arg,
21592 pos_T *posp,
21593 int *fnump,
21594 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021595{
21596 list_T *l = arg->vval.v_list;
21597 long i = 0;
21598 long n;
21599
Bram Moolenaar493c1782014-05-28 14:34:46 +020021600 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21601 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021602 if (arg->v_type != VAR_LIST
21603 || l == NULL
21604 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021605 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021606 return FAIL;
21607
21608 if (fnump != NULL)
21609 {
21610 n = list_find_nr(l, i++, NULL); /* fnum */
21611 if (n < 0)
21612 return FAIL;
21613 if (n == 0)
21614 n = curbuf->b_fnum; /* current buffer */
21615 *fnump = n;
21616 }
21617
21618 n = list_find_nr(l, i++, NULL); /* lnum */
21619 if (n < 0)
21620 return FAIL;
21621 posp->lnum = n;
21622
21623 n = list_find_nr(l, i++, NULL); /* col */
21624 if (n < 0)
21625 return FAIL;
21626 posp->col = n;
21627
21628#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021629 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021630 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021631 posp->coladd = 0;
21632 else
21633 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021634#endif
21635
Bram Moolenaar493c1782014-05-28 14:34:46 +020021636 if (curswantp != NULL)
21637 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21638
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021639 return OK;
21640}
21641
21642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 * Get the length of an environment variable name.
21644 * Advance "arg" to the first character after the name.
21645 * Return 0 for error.
21646 */
21647 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021648get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649{
21650 char_u *p;
21651 int len;
21652
21653 for (p = *arg; vim_isIDc(*p); ++p)
21654 ;
21655 if (p == *arg) /* no name found */
21656 return 0;
21657
21658 len = (int)(p - *arg);
21659 *arg = p;
21660 return len;
21661}
21662
21663/*
21664 * Get the length of the name of a function or internal variable.
21665 * "arg" is advanced to the first non-white character after the name.
21666 * Return 0 if something is wrong.
21667 */
21668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021669get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670{
21671 char_u *p;
21672 int len;
21673
21674 /* Find the end of the name. */
21675 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021676 {
21677 if (*p == ':')
21678 {
21679 /* "s:" is start of "s:var", but "n:" is not and can be used in
21680 * slice "[n:]". Also "xx:" is not a namespace. */
21681 len = (int)(p - *arg);
21682 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21683 || len > 1)
21684 break;
21685 }
21686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 if (p == *arg) /* no name found */
21688 return 0;
21689
21690 len = (int)(p - *arg);
21691 *arg = skipwhite(p);
21692
21693 return len;
21694}
21695
21696/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021697 * Get the length of the name of a variable or function.
21698 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021700 * Return -1 if curly braces expansion failed.
21701 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 * If the name contains 'magic' {}'s, expand them and return the
21703 * expanded name in an allocated string via 'alias' - caller must free.
21704 */
21705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021706get_name_len(
21707 char_u **arg,
21708 char_u **alias,
21709 int evaluate,
21710 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711{
21712 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 char_u *p;
21714 char_u *expr_start;
21715 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716
21717 *alias = NULL; /* default to no alias */
21718
21719 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21720 && (*arg)[2] == (int)KE_SNR)
21721 {
21722 /* hard coded <SNR>, already translated */
21723 *arg += 3;
21724 return get_id_len(arg) + 3;
21725 }
21726 len = eval_fname_script(*arg);
21727 if (len > 0)
21728 {
21729 /* literal "<SID>", "s:" or "<SNR>" */
21730 *arg += len;
21731 }
21732
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021734 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021736 p = find_name_end(*arg, &expr_start, &expr_end,
21737 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 if (expr_start != NULL)
21739 {
21740 char_u *temp_string;
21741
21742 if (!evaluate)
21743 {
21744 len += (int)(p - *arg);
21745 *arg = skipwhite(p);
21746 return len;
21747 }
21748
21749 /*
21750 * Include any <SID> etc in the expanded string:
21751 * Thus the -len here.
21752 */
21753 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21754 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021755 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 *alias = temp_string;
21757 *arg = skipwhite(p);
21758 return (int)STRLEN(temp_string);
21759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760
21761 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021762 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 EMSG2(_(e_invexpr2), *arg);
21764
21765 return len;
21766}
21767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768/*
21769 * Find the end of a variable or function name, taking care of magic braces.
21770 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21771 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021772 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021773 * Return a pointer to just after the name. Equal to "arg" if there is no
21774 * valid name.
21775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021777find_name_end(
21778 char_u *arg,
21779 char_u **expr_start,
21780 char_u **expr_end,
21781 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021783 int mb_nest = 0;
21784 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021786 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021788 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021790 *expr_start = NULL;
21791 *expr_end = NULL;
21792 }
21793
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021794 /* Quick check for valid starting character. */
21795 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21796 return arg;
21797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021798 for (p = arg; *p != NUL
21799 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021800 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021801 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021802 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021803 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021805 if (*p == '\'')
21806 {
21807 /* skip over 'string' to avoid counting [ and ] inside it. */
21808 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21809 ;
21810 if (*p == NUL)
21811 break;
21812 }
21813 else if (*p == '"')
21814 {
21815 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21816 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21817 if (*p == '\\' && p[1] != NUL)
21818 ++p;
21819 if (*p == NUL)
21820 break;
21821 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021822 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21823 {
21824 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021825 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021826 len = (int)(p - arg);
21827 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021828 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021829 break;
21830 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021832 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021834 if (*p == '[')
21835 ++br_nest;
21836 else if (*p == ']')
21837 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021840 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021842 if (*p == '{')
21843 {
21844 mb_nest++;
21845 if (expr_start != NULL && *expr_start == NULL)
21846 *expr_start = p;
21847 }
21848 else if (*p == '}')
21849 {
21850 mb_nest--;
21851 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21852 *expr_end = p;
21853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 }
21856
21857 return p;
21858}
21859
21860/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021861 * Expands out the 'magic' {}'s in a variable/function name.
21862 * Note that this can call itself recursively, to deal with
21863 * constructs like foo{bar}{baz}{bam}
21864 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21865 * "in_start" ^
21866 * "expr_start" ^
21867 * "expr_end" ^
21868 * "in_end" ^
21869 *
21870 * Returns a new allocated string, which the caller must free.
21871 * Returns NULL for failure.
21872 */
21873 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021874make_expanded_name(
21875 char_u *in_start,
21876 char_u *expr_start,
21877 char_u *expr_end,
21878 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021879{
21880 char_u c1;
21881 char_u *retval = NULL;
21882 char_u *temp_result;
21883 char_u *nextcmd = NULL;
21884
21885 if (expr_end == NULL || in_end == NULL)
21886 return NULL;
21887 *expr_start = NUL;
21888 *expr_end = NUL;
21889 c1 = *in_end;
21890 *in_end = NUL;
21891
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021892 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021893 if (temp_result != NULL && nextcmd == NULL)
21894 {
21895 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21896 + (in_end - expr_end) + 1));
21897 if (retval != NULL)
21898 {
21899 STRCPY(retval, in_start);
21900 STRCAT(retval, temp_result);
21901 STRCAT(retval, expr_end + 1);
21902 }
21903 }
21904 vim_free(temp_result);
21905
21906 *in_end = c1; /* put char back for error messages */
21907 *expr_start = '{';
21908 *expr_end = '}';
21909
21910 if (retval != NULL)
21911 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021912 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021913 if (expr_start != NULL)
21914 {
21915 /* Further expansion! */
21916 temp_result = make_expanded_name(retval, expr_start,
21917 expr_end, temp_result);
21918 vim_free(retval);
21919 retval = temp_result;
21920 }
21921 }
21922
21923 return retval;
21924}
21925
21926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021928 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 */
21930 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021931eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021933 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21934}
21935
21936/*
21937 * Return TRUE if character "c" can be used as the first character in a
21938 * variable or function name (excluding '{' and '}').
21939 */
21940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021941eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021942{
21943 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944}
21945
21946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 * Set number v: variable to "val".
21948 */
21949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953}
21954
21955/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021956 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 */
21958 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021959get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021961 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962}
21963
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021964/*
21965 * Get string v: variable value. Uses a static buffer, can only be used once.
21966 */
21967 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021968get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021969{
21970 return get_tv_string(&vimvars[idx].vv_tv);
21971}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021972
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021974 * Get List v: variable value. Caller must take care of reference count when
21975 * needed.
21976 */
21977 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021978get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021979{
21980 return vimvars[idx].vv_list;
21981}
21982
21983/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021984 * Set v:char to character "c".
21985 */
21986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021988{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021989 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021990
21991#ifdef FEAT_MBYTE
21992 if (has_mbyte)
21993 buf[(*mb_char2bytes)(c, buf)] = NUL;
21994 else
21995#endif
21996 {
21997 buf[0] = c;
21998 buf[1] = NUL;
21999 }
22000 set_vim_var_string(VV_CHAR, buf, -1);
22001}
22002
22003/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022004 * Set v:count to "count" and v:count1 to "count1".
22005 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 */
22007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008set_vcount(
22009 long count,
22010 long count1,
22011 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022013 if (set_prevcount)
22014 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022015 vimvars[VV_COUNT].vv_nr = count;
22016 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017}
22018
22019/*
22020 * Set string v: variable to a copy of "val".
22021 */
22022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022023set_vim_var_string(
22024 int idx,
22025 char_u *val,
22026 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027{
Bram Moolenaara542c682016-01-31 16:28:04 +010022028 clear_tv(&vimvars[idx].vv_di.di_tv);
22029 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022031 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022033 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022035 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036}
22037
22038/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022039 * Set List v: variable to "val".
22040 */
22041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022042set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022043{
Bram Moolenaara542c682016-01-31 16:28:04 +010022044 clear_tv(&vimvars[idx].vv_di.di_tv);
22045 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022046 vimvars[idx].vv_list = val;
22047 if (val != NULL)
22048 ++val->lv_refcount;
22049}
22050
22051/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022052 * Set Dictionary v: variable to "val".
22053 */
22054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022055set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022056{
22057 int todo;
22058 hashitem_T *hi;
22059
Bram Moolenaara542c682016-01-31 16:28:04 +010022060 clear_tv(&vimvars[idx].vv_di.di_tv);
22061 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022062 vimvars[idx].vv_dict = val;
22063 if (val != NULL)
22064 {
22065 ++val->dv_refcount;
22066
22067 /* Set readonly */
22068 todo = (int)val->dv_hashtab.ht_used;
22069 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22070 {
22071 if (HASHITEM_EMPTY(hi))
22072 continue;
22073 --todo;
22074 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22075 }
22076 }
22077}
22078
22079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 * Set v:register if needed.
22081 */
22082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022083set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084{
22085 char_u regname;
22086
22087 if (c == 0 || c == ' ')
22088 regname = '"';
22089 else
22090 regname = c;
22091 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022092 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 set_vim_var_string(VV_REG, &regname, 1);
22094}
22095
22096/*
22097 * Get or set v:exception. If "oldval" == NULL, return the current value.
22098 * Otherwise, restore the value to "oldval" and return NULL.
22099 * Must always be called in pairs to save and restore v:exception! Does not
22100 * take care of memory allocations.
22101 */
22102 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022103v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104{
22105 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022106 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107
Bram Moolenaare9a41262005-01-15 22:18:47 +000022108 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 return NULL;
22110}
22111
22112/*
22113 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22114 * Otherwise, restore the value to "oldval" and return NULL.
22115 * Must always be called in pairs to save and restore v:throwpoint! Does not
22116 * take care of memory allocations.
22117 */
22118 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022119v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120{
22121 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022122 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123
Bram Moolenaare9a41262005-01-15 22:18:47 +000022124 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125 return NULL;
22126}
22127
22128#if defined(FEAT_AUTOCMD) || defined(PROTO)
22129/*
22130 * Set v:cmdarg.
22131 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22132 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22133 * Must always be called in pairs!
22134 */
22135 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022136set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137{
22138 char_u *oldval;
22139 char_u *newval;
22140 unsigned len;
22141
Bram Moolenaare9a41262005-01-15 22:18:47 +000022142 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022143 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022145 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022146 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022147 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 }
22149
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022150 if (eap->force_bin == FORCE_BIN)
22151 len = 6;
22152 else if (eap->force_bin == FORCE_NOBIN)
22153 len = 8;
22154 else
22155 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022156
22157 if (eap->read_edit)
22158 len += 7;
22159
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022160 if (eap->force_ff != 0)
22161 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22162# ifdef FEAT_MBYTE
22163 if (eap->force_enc != 0)
22164 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022165 if (eap->bad_char != 0)
22166 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022167# endif
22168
22169 newval = alloc(len + 1);
22170 if (newval == NULL)
22171 return NULL;
22172
22173 if (eap->force_bin == FORCE_BIN)
22174 sprintf((char *)newval, " ++bin");
22175 else if (eap->force_bin == FORCE_NOBIN)
22176 sprintf((char *)newval, " ++nobin");
22177 else
22178 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022179
22180 if (eap->read_edit)
22181 STRCAT(newval, " ++edit");
22182
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022183 if (eap->force_ff != 0)
22184 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22185 eap->cmd + eap->force_ff);
22186# ifdef FEAT_MBYTE
22187 if (eap->force_enc != 0)
22188 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22189 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022190 if (eap->bad_char == BAD_KEEP)
22191 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22192 else if (eap->bad_char == BAD_DROP)
22193 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22194 else if (eap->bad_char != 0)
22195 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022196# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022197 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022198 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199}
22200#endif
22201
22202/*
22203 * Get the value of internal variable "name".
22204 * Return OK or FAIL.
22205 */
22206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022207get_var_tv(
22208 char_u *name,
22209 int len, /* length of "name" */
22210 typval_T *rettv, /* NULL when only checking existence */
22211 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22212 int verbose, /* may give error message */
22213 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214{
22215 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 typval_T *tv = NULL;
22217 typval_T atv;
22218 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220
22221 /* truncate the name, so that we can use strcmp() */
22222 cc = name[len];
22223 name[len] = NUL;
22224
22225 /*
22226 * Check for "b:changedtick".
22227 */
22228 if (STRCMP(name, "b:changedtick") == 0)
22229 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022230 atv.v_type = VAR_NUMBER;
22231 atv.vval.v_number = curbuf->b_changedtick;
22232 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 }
22234
22235 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 * Check for user-defined variables.
22237 */
22238 else
22239 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022240 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022242 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022243 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022244 if (dip != NULL)
22245 *dip = v;
22246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 }
22248
Bram Moolenaare9a41262005-01-15 22:18:47 +000022249 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022251 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253 ret = FAIL;
22254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022255 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022256 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257
22258 name[len] = cc;
22259
22260 return ret;
22261}
22262
22263/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022264 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22265 * Also handle function call with Funcref variable: func(expr)
22266 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22267 */
22268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022269handle_subscript(
22270 char_u **arg,
22271 typval_T *rettv,
22272 int evaluate, /* do more than finding the end */
22273 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022274{
22275 int ret = OK;
22276 dict_T *selfdict = NULL;
22277 char_u *s;
22278 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022279 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022280
22281 while (ret == OK
22282 && (**arg == '['
22283 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022284 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022285 && !vim_iswhite(*(*arg - 1)))
22286 {
22287 if (**arg == '(')
22288 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022289 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022290 if (evaluate)
22291 {
22292 functv = *rettv;
22293 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022294
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022295 /* Invoke the function. Recursive! */
22296 s = functv.vval.v_string;
22297 }
22298 else
22299 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022300 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022301 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22302 &len, evaluate, selfdict);
22303
22304 /* Clear the funcref afterwards, so that deleting it while
22305 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022306 if (evaluate)
22307 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022308
22309 /* Stop the expression evaluation when immediately aborting on
22310 * error, or when an interrupt occurred or an exception was thrown
22311 * but not caught. */
22312 if (aborting())
22313 {
22314 if (ret == OK)
22315 clear_tv(rettv);
22316 ret = FAIL;
22317 }
22318 dict_unref(selfdict);
22319 selfdict = NULL;
22320 }
22321 else /* **arg == '[' || **arg == '.' */
22322 {
22323 dict_unref(selfdict);
22324 if (rettv->v_type == VAR_DICT)
22325 {
22326 selfdict = rettv->vval.v_dict;
22327 if (selfdict != NULL)
22328 ++selfdict->dv_refcount;
22329 }
22330 else
22331 selfdict = NULL;
22332 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22333 {
22334 clear_tv(rettv);
22335 ret = FAIL;
22336 }
22337 }
22338 }
22339 dict_unref(selfdict);
22340 return ret;
22341}
22342
22343/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022344 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022345 * value).
22346 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022347 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022349{
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351}
22352
22353/*
22354 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 * The string "s" must have been allocated, it is consumed.
22356 * Return NULL for out of memory, the variable otherwise.
22357 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022359alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360{
Bram Moolenaar33570922005-01-25 22:26:29 +000022361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022363 rettv = alloc_tv();
22364 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022366 rettv->v_type = VAR_STRING;
22367 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
22369 else
22370 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022371 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372}
22373
22374/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022375 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022378free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379{
22380 if (varp != NULL)
22381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022382 switch (varp->v_type)
22383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 func_unref(varp->vval.v_string);
22386 /*FALLTHROUGH*/
22387 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022388 vim_free(varp->vval.v_string);
22389 break;
22390 case VAR_LIST:
22391 list_unref(varp->vval.v_list);
22392 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 case VAR_DICT:
22394 dict_unref(varp->vval.v_dict);
22395 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022396 case VAR_JOB:
22397#ifdef FEAT_JOB
22398 job_unref(varp->vval.v_job);
22399 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022400#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022401 case VAR_CHANNEL:
22402#ifdef FEAT_CHANNEL
22403 channel_unref(varp->vval.v_channel);
22404 break;
22405#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022406 case VAR_NUMBER:
22407 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022408 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022409 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022410 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 vim_free(varp);
22413 }
22414}
22415
22416/*
22417 * Free the memory for a variable value and set the value to NULL or 0.
22418 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022420clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421{
22422 if (varp != NULL)
22423 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022426 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022427 func_unref(varp->vval.v_string);
22428 /*FALLTHROUGH*/
22429 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022430 vim_free(varp->vval.v_string);
22431 varp->vval.v_string = NULL;
22432 break;
22433 case VAR_LIST:
22434 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022435 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022436 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022437 case VAR_DICT:
22438 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022439 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022440 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022441 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022442 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022443 varp->vval.v_number = 0;
22444 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022445 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022446#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022447 varp->vval.v_float = 0.0;
22448 break;
22449#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022450 case VAR_JOB:
22451#ifdef FEAT_JOB
22452 job_unref(varp->vval.v_job);
22453 varp->vval.v_job = NULL;
22454#endif
22455 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022456 case VAR_CHANNEL:
22457#ifdef FEAT_CHANNEL
22458 channel_unref(varp->vval.v_channel);
22459 varp->vval.v_channel = NULL;
22460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 case VAR_UNKNOWN:
22462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022464 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 }
22466}
22467
22468/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022469 * Set the value of a variable to NULL without freeing items.
22470 */
22471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022472init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022473{
22474 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022476}
22477
22478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 * Get the number value of a variable.
22480 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022481 * For incompatible types, return 0.
22482 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22483 * caller of incompatible types: it sets *denote to TRUE if "denote"
22484 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 */
22486 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022487get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022489 int error = FALSE;
22490
22491 return get_tv_number_chk(varp, &error); /* return 0L on error */
22492}
22493
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022494 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022495get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022496{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022497 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022501 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022502 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022503 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022504#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022505 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022506 break;
22507#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022509 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022510 break;
22511 case VAR_STRING:
22512 if (varp->vval.v_string != NULL)
22513 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022514 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022515 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022516 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022517 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022518 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022519 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022520 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022521 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022522 case VAR_SPECIAL:
22523 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22524 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022525 case VAR_JOB:
22526#ifdef FEAT_JOB
22527 EMSG(_("E910: Using a Job as a Number"));
22528 break;
22529#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022530 case VAR_CHANNEL:
22531#ifdef FEAT_CHANNEL
22532 EMSG(_("E913: Using a Channel as a Number"));
22533 break;
22534#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022535 case VAR_UNKNOWN:
22536 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022539 if (denote == NULL) /* useful for values that must be unsigned */
22540 n = -1;
22541 else
22542 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022543 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544}
22545
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022546#ifdef FEAT_FLOAT
22547 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022548get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022549{
22550 switch (varp->v_type)
22551 {
22552 case VAR_NUMBER:
22553 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022554 case VAR_FLOAT:
22555 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022556 case VAR_FUNC:
22557 EMSG(_("E891: Using a Funcref as a Float"));
22558 break;
22559 case VAR_STRING:
22560 EMSG(_("E892: Using a String as a Float"));
22561 break;
22562 case VAR_LIST:
22563 EMSG(_("E893: Using a List as a Float"));
22564 break;
22565 case VAR_DICT:
22566 EMSG(_("E894: Using a Dictionary as a Float"));
22567 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022568 case VAR_SPECIAL:
22569 EMSG(_("E907: Using a special value as a Float"));
22570 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022571 case VAR_JOB:
22572# ifdef FEAT_JOB
22573 EMSG(_("E911: Using a Job as a Float"));
22574 break;
22575# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022576 case VAR_CHANNEL:
22577# ifdef FEAT_CHANNEL
22578 EMSG(_("E914: Using a Channel as a Float"));
22579 break;
22580# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022581 case VAR_UNKNOWN:
22582 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022583 break;
22584 }
22585 return 0;
22586}
22587#endif
22588
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022590 * Get the lnum from the first argument.
22591 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022592 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 */
22594 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022595get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596{
Bram Moolenaar33570922005-01-25 22:26:29 +000022597 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 linenr_T lnum;
22599
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022600 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 if (lnum == 0) /* no valid number, try using line() */
22602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022603 rettv.v_type = VAR_NUMBER;
22604 f_line(argvars, &rettv);
22605 lnum = rettv.vval.v_number;
22606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 }
22608 return lnum;
22609}
22610
22611/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022612 * Get the lnum from the first argument.
22613 * Also accepts "$", then "buf" is used.
22614 * Returns 0 on error.
22615 */
22616 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022617get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022618{
22619 if (argvars[0].v_type == VAR_STRING
22620 && argvars[0].vval.v_string != NULL
22621 && argvars[0].vval.v_string[0] == '$'
22622 && buf != NULL)
22623 return buf->b_ml.ml_line_count;
22624 return get_tv_number_chk(&argvars[0], NULL);
22625}
22626
22627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 * Get the string value of a variable.
22629 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022630 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22631 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 * If the String variable has never been set, return an empty string.
22633 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022634 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22635 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 */
22637 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639{
22640 static char_u mybuf[NUMBUFLEN];
22641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022642 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643}
22644
22645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022648 char_u *res = get_tv_string_buf_chk(varp, buf);
22649
22650 return res != NULL ? res : (char_u *)"";
22651}
22652
Bram Moolenaar7d647822014-04-05 21:28:56 +020022653/*
22654 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22655 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022658{
22659 static char_u mybuf[NUMBUFLEN];
22660
22661 return get_tv_string_buf_chk(varp, mybuf);
22662}
22663
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022664 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022665get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022666{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022667 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022669 case VAR_NUMBER:
22670 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22671 return buf;
22672 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022673 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022674 break;
22675 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022676 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022677 break;
22678 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022679 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022680 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022681 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022682#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022683 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022684 break;
22685#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022686 case VAR_STRING:
22687 if (varp->vval.v_string != NULL)
22688 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022689 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022690 case VAR_SPECIAL:
22691 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22692 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022693 case VAR_JOB:
22694#ifdef FEAT_JOB
22695 {
22696 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022697 char *status;
22698
22699 if (job == NULL)
22700 return (char_u *)"no process";
22701 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022702 : job->jv_status == JOB_ENDED ? "dead"
22703 : "run";
22704# ifdef UNIX
22705 vim_snprintf((char *)buf, NUMBUFLEN,
22706 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022707# elif defined(WIN32)
22708 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022709 "process %ld %s",
22710 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022711 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022712# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022713 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022714 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22715# endif
22716 return buf;
22717 }
22718#endif
22719 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022720 case VAR_CHANNEL:
22721#ifdef FEAT_CHANNEL
22722 {
22723 channel_T *channel = varp->vval.v_channel;
22724 char *status = channel_status(channel);
22725
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022726 if (channel == NULL)
22727 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22728 else
22729 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022730 "channel %d %s", channel->ch_id, status);
22731 return buf;
22732 }
22733#endif
22734 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022735 case VAR_UNKNOWN:
22736 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022739 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
22742/*
22743 * Find variable "name" in the list of variables.
22744 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022746 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022750find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
Bram Moolenaara7043832005-01-21 11:56:39 +000022755 ht = find_var_ht(name, &varname);
22756 if (htp != NULL)
22757 *htp = ht;
22758 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022760 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761}
22762
22763/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022764 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022765 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022768find_var_in_ht(
22769 hashtab_T *ht,
22770 int htname,
22771 char_u *varname,
22772 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022773{
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 hashitem_T *hi;
22775
22776 if (*varname == NUL)
22777 {
22778 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022779 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022781 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022782 case 'g': return &globvars_var;
22783 case 'v': return &vimvars_var;
22784 case 'b': return &curbuf->b_bufvar;
22785 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022786#ifdef FEAT_WINDOWS
22787 case 't': return &curtab->tp_winvar;
22788#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022789 case 'l': return current_funccal == NULL
22790 ? NULL : &current_funccal->l_vars_var;
22791 case 'a': return current_funccal == NULL
22792 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 }
22794 return NULL;
22795 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022796
22797 hi = hash_find(ht, varname);
22798 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 {
22800 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022801 * worked find the variable again. Don't auto-load a script if it was
22802 * loaded already, otherwise it would be loaded every time when
22803 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022804 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022805 {
22806 /* Note: script_autoload() may make "hi" invalid. It must either
22807 * be obtained again or not used. */
22808 if (!script_autoload(varname, FALSE) || aborting())
22809 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022810 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022811 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022812 if (HASHITEM_EMPTY(hi))
22813 return NULL;
22814 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022815 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022816}
22817
22818/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022819 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022820 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022821 * Set "varname" to the start of name without ':'.
22822 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022824find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022826 hashitem_T *hi;
22827
Bram Moolenaar73627d02015-08-11 15:46:09 +020022828 if (name[0] == NUL)
22829 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 if (name[1] != ':')
22831 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022832 /* The name must not start with a colon or #. */
22833 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 return NULL;
22835 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022836
22837 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022838 hi = hash_find(&compat_hashtab, name);
22839 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022840 return &compat_hashtab;
22841
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022844 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 }
22846 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022847 if (*name == 'g') /* global variable */
22848 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022849 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22850 */
22851 if (vim_strchr(name + 2, ':') != NULL
22852 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022855 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022857 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022858#ifdef FEAT_WINDOWS
22859 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022860 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022861#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022862 if (*name == 'v') /* v: variable */
22863 return &vimvarht;
22864 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022865 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022867 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 if (*name == 's' /* script variable */
22869 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22870 return &SCRIPT_VARS(current_SID);
22871 return NULL;
22872}
22873
22874/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022875 * Get function call environment based on bactrace debug level
22876 */
22877 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022878get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022879{
22880 int i;
22881 funccall_T *funccal;
22882 funccall_T *temp_funccal;
22883
22884 funccal = current_funccal;
22885 if (debug_backtrace_level > 0)
22886 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022887 for (i = 0; i < debug_backtrace_level; i++)
22888 {
22889 temp_funccal = funccal->caller;
22890 if (temp_funccal)
22891 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022892 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022893 /* backtrace level overflow. reset to max */
22894 debug_backtrace_level = i;
22895 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022896 }
22897 return funccal;
22898}
22899
22900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022902 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 * Returns NULL when it doesn't exist.
22904 */
22905 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022906get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907{
Bram Moolenaar33570922005-01-25 22:26:29 +000022908 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022910 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 if (v == NULL)
22912 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914}
22915
22916/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 * sourcing this script and when executing functions defined in the script.
22919 */
22920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022921new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922{
Bram Moolenaara7043832005-01-21 11:56:39 +000022923 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022924 hashtab_T *ht;
22925 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022926
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22928 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022929 /* Re-allocating ga_data means that an ht_array pointing to
22930 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022932 for (i = 1; i <= ga_scripts.ga_len; ++i)
22933 {
22934 ht = &SCRIPT_VARS(i);
22935 if (ht->ht_mask == HT_INIT_SIZE - 1)
22936 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022937 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022938 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022939 }
22940
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 while (ga_scripts.ga_len < id)
22942 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022943 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022944 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022945 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 }
22948 }
22949}
22950
22951/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022952 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22953 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 */
22955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022956init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957{
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022959 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022960 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022961 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022962 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022963 dict_var->di_tv.vval.v_dict = dict;
22964 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022965 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22967 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968}
22969
22970/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022971 * Unreference a dictionary initialized by init_var_dict().
22972 */
22973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022974unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022975{
22976 /* Now the dict needs to be freed if no one else is using it, go back to
22977 * normal reference counting. */
22978 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22979 dict_unref(dict);
22980}
22981
22982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022984 * Frees all allocated variables and the value they contain.
22985 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 */
22987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022988vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022989{
22990 vars_clear_ext(ht, TRUE);
22991}
22992
22993/*
22994 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22995 */
22996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022997vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998{
Bram Moolenaara7043832005-01-21 11:56:39 +000022999 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023000 hashitem_T *hi;
23001 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023004 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023005 for (hi = ht->ht_array; todo > 0; ++hi)
23006 {
23007 if (!HASHITEM_EMPTY(hi))
23008 {
23009 --todo;
23010
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023012 * ht_array might change then. hash_clear() takes care of it
23013 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 v = HI2DI(hi);
23015 if (free_val)
23016 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023017 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023019 }
23020 }
23021 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023022 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023}
23024
Bram Moolenaara7043832005-01-21 11:56:39 +000023025/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023026 * Delete a variable from hashtab "ht" at item "hi".
23027 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023030delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031{
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023033
23034 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 clear_tv(&di->di_tv);
23036 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037}
23038
23039/*
23040 * List the value of one internal variable.
23041 */
23042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023043list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023045 char_u *tofree;
23046 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023047 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023048
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023049 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023051 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023052 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053}
23054
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023056list_one_var_a(
23057 char_u *prefix,
23058 char_u *name,
23059 int type,
23060 char_u *string,
23061 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062{
Bram Moolenaar31859182007-08-14 20:41:13 +000023063 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23064 msg_start();
23065 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 if (name != NULL) /* "a:" vars don't have a name stored */
23067 msg_puts(name);
23068 msg_putchar(' ');
23069 msg_advance(22);
23070 if (type == VAR_NUMBER)
23071 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023072 else if (type == VAR_FUNC)
23073 msg_putchar('*');
23074 else if (type == VAR_LIST)
23075 {
23076 msg_putchar('[');
23077 if (*string == '[')
23078 ++string;
23079 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023080 else if (type == VAR_DICT)
23081 {
23082 msg_putchar('{');
23083 if (*string == '{')
23084 ++string;
23085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 else
23087 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023088
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023090
23091 if (type == VAR_FUNC)
23092 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023093 if (*first)
23094 {
23095 msg_clr_eos();
23096 *first = FALSE;
23097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098}
23099
23100/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023101 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 * If the variable already exists, the value is updated.
23103 * Otherwise the variable is created.
23104 */
23105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023106set_var(
23107 char_u *name,
23108 typval_T *tv,
23109 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110{
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023113 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023115 ht = find_var_ht(name, &varname);
23116 if (ht == NULL || *varname == NUL)
23117 {
23118 EMSG2(_(e_illvar), name);
23119 return;
23120 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023121 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023122
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023123 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
23124 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023129 if (var_check_ro(v->di_flags, name, FALSE)
23130 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 return;
23132 if (v->di_tv.v_type != tv->v_type
23133 && !((v->di_tv.v_type == VAR_STRING
23134 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023135 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023136 || tv->v_type == VAR_NUMBER))
23137#ifdef FEAT_FLOAT
23138 && !((v->di_tv.v_type == VAR_NUMBER
23139 || v->di_tv.v_type == VAR_FLOAT)
23140 && (tv->v_type == VAR_NUMBER
23141 || tv->v_type == VAR_FLOAT))
23142#endif
23143 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023144 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000023145 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023146 return;
23147 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023148
23149 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023150 * Handle setting internal v: variables separately where needed to
23151 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 */
23153 if (ht == &vimvarht)
23154 {
23155 if (v->di_tv.v_type == VAR_STRING)
23156 {
23157 vim_free(v->di_tv.vval.v_string);
23158 if (copy || tv->v_type != VAR_STRING)
23159 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23160 else
23161 {
23162 /* Take over the string to avoid an extra alloc/free. */
23163 v->di_tv.vval.v_string = tv->vval.v_string;
23164 tv->vval.v_string = NULL;
23165 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023166 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023167 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023168 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023171 if (STRCMP(varname, "searchforward") == 0)
23172 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023173#ifdef FEAT_SEARCH_EXTRA
23174 else if (STRCMP(varname, "hlsearch") == 0)
23175 {
23176 no_hlsearch = !v->di_tv.vval.v_number;
23177 redraw_all_later(SOME_VALID);
23178 }
23179#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023180 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023181 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023182 else if (v->di_tv.v_type != tv->v_type)
23183 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023184 }
23185
23186 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 }
23188 else /* add a new variable */
23189 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023190 /* Can't add "v:" variable. */
23191 if (ht == &vimvarht)
23192 {
23193 EMSG2(_(e_illvar), name);
23194 return;
23195 }
23196
Bram Moolenaar92124a32005-06-17 22:03:40 +000023197 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023198 if (!valid_varname(varname))
23199 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023200
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023201 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23202 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023203 if (v == NULL)
23204 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023205 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023206 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023208 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 return;
23210 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023211 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023213
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023214 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023215 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023216 else
23217 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023218 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023219 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023220 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222}
23223
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023225 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 * Also give an error message.
23227 */
23228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023229var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023230{
23231 if (flags & DI_FLAGS_RO)
23232 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023233 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 return TRUE;
23235 }
23236 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23237 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023238 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023239 return TRUE;
23240 }
23241 return FALSE;
23242}
23243
23244/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023245 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23246 * Also give an error message.
23247 */
23248 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023249var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023250{
23251 if (flags & DI_FLAGS_FIX)
23252 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023253 EMSG2(_("E795: Cannot delete variable %s"),
23254 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023255 return TRUE;
23256 }
23257 return FALSE;
23258}
23259
23260/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023261 * Check if a funcref is assigned to a valid variable name.
23262 * Return TRUE and give an error if not.
23263 */
23264 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023265var_check_func_name(
23266 char_u *name, /* points to start of variable name */
23267 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023268{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023269 /* Allow for w: b: s: and t:. */
23270 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023271 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23272 ? name[2] : name[0]))
23273 {
23274 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23275 name);
23276 return TRUE;
23277 }
23278 /* Don't allow hiding a function. When "v" is not NULL we might be
23279 * assigning another function to the same var, the type is checked
23280 * below. */
23281 if (new_var && function_exists(name))
23282 {
23283 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23284 name);
23285 return TRUE;
23286 }
23287 return FALSE;
23288}
23289
23290/*
23291 * Check if a variable name is valid.
23292 * Return FALSE and give an error if not.
23293 */
23294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023295valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023296{
23297 char_u *p;
23298
23299 for (p = varname; *p != NUL; ++p)
23300 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23301 && *p != AUTOLOAD_CHAR)
23302 {
23303 EMSG2(_(e_illvar), varname);
23304 return FALSE;
23305 }
23306 return TRUE;
23307}
23308
23309/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023310 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023311 * Also give an error message, using "name" or _("name") when use_gettext is
23312 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023313 */
23314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023315tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023316{
23317 if (lock & VAR_LOCKED)
23318 {
23319 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023320 name == NULL ? (char_u *)_("Unknown")
23321 : use_gettext ? (char_u *)_(name)
23322 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023323 return TRUE;
23324 }
23325 if (lock & VAR_FIXED)
23326 {
23327 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023328 name == NULL ? (char_u *)_("Unknown")
23329 : use_gettext ? (char_u *)_(name)
23330 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023331 return TRUE;
23332 }
23333 return FALSE;
23334}
23335
23336/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023338 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023339 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023340 * It is OK for "from" and "to" to point to the same item. This is used to
23341 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023342 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023344copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023346 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023347 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023348 switch (from->v_type)
23349 {
23350 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023351 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023352 to->vval.v_number = from->vval.v_number;
23353 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023354 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023355#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023356 to->vval.v_float = from->vval.v_float;
23357 break;
23358#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023359 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023360#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023361 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023362 if (to->vval.v_job != NULL)
23363 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023364 break;
23365#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023366 case VAR_CHANNEL:
23367#ifdef FEAT_CHANNEL
23368 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023369 if (to->vval.v_channel != NULL)
23370 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023371 break;
23372#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023373 case VAR_STRING:
23374 case VAR_FUNC:
23375 if (from->vval.v_string == NULL)
23376 to->vval.v_string = NULL;
23377 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023378 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023379 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023380 if (from->v_type == VAR_FUNC)
23381 func_ref(to->vval.v_string);
23382 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023383 break;
23384 case VAR_LIST:
23385 if (from->vval.v_list == NULL)
23386 to->vval.v_list = NULL;
23387 else
23388 {
23389 to->vval.v_list = from->vval.v_list;
23390 ++to->vval.v_list->lv_refcount;
23391 }
23392 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023393 case VAR_DICT:
23394 if (from->vval.v_dict == NULL)
23395 to->vval.v_dict = NULL;
23396 else
23397 {
23398 to->vval.v_dict = from->vval.v_dict;
23399 ++to->vval.v_dict->dv_refcount;
23400 }
23401 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023402 case VAR_UNKNOWN:
23403 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023404 break;
23405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406}
23407
23408/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023409 * Make a copy of an item.
23410 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023411 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23412 * reference to an already copied list/dict can be used.
23413 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023414 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023416item_copy(
23417 typval_T *from,
23418 typval_T *to,
23419 int deep,
23420 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023421{
23422 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023423 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023424
Bram Moolenaar33570922005-01-25 22:26:29 +000023425 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023426 {
23427 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023428 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023429 }
23430 ++recurse;
23431
23432 switch (from->v_type)
23433 {
23434 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023435 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023436 case VAR_STRING:
23437 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023438 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023439 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023440 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023441 copy_tv(from, to);
23442 break;
23443 case VAR_LIST:
23444 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023445 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023446 if (from->vval.v_list == NULL)
23447 to->vval.v_list = NULL;
23448 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23449 {
23450 /* use the copy made earlier */
23451 to->vval.v_list = from->vval.v_list->lv_copylist;
23452 ++to->vval.v_list->lv_refcount;
23453 }
23454 else
23455 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23456 if (to->vval.v_list == NULL)
23457 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023458 break;
23459 case VAR_DICT:
23460 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023461 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023462 if (from->vval.v_dict == NULL)
23463 to->vval.v_dict = NULL;
23464 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23465 {
23466 /* use the copy made earlier */
23467 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23468 ++to->vval.v_dict->dv_refcount;
23469 }
23470 else
23471 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23472 if (to->vval.v_dict == NULL)
23473 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023474 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023475 case VAR_UNKNOWN:
23476 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023477 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023478 }
23479 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023480 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023481}
23482
23483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 * ":echo expr1 ..." print each argument separated with a space, add a
23485 * newline at the end.
23486 * ":echon expr1 ..." print each argument plain.
23487 */
23488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023489ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490{
23491 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023493 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 char_u *p;
23495 int needclr = TRUE;
23496 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023497 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498
23499 if (eap->skip)
23500 ++emsg_skip;
23501 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23502 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023503 /* If eval1() causes an error message the text from the command may
23504 * still need to be cleared. E.g., "echo 22,44". */
23505 need_clr_eos = needclr;
23506
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023508 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 {
23510 /*
23511 * Report the invalid expression unless the expression evaluation
23512 * has been cancelled due to an aborting error, an interrupt, or an
23513 * exception.
23514 */
23515 if (!aborting())
23516 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023517 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 break;
23519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023520 need_clr_eos = FALSE;
23521
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 if (!eap->skip)
23523 {
23524 if (atstart)
23525 {
23526 atstart = FALSE;
23527 /* Call msg_start() after eval1(), evaluating the expression
23528 * may cause a message to appear. */
23529 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023530 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023531 /* Mark the saved text as finishing the line, so that what
23532 * follows is displayed on a new line when scrolling back
23533 * at the more prompt. */
23534 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 }
23538 else if (eap->cmdidx == CMD_echo)
23539 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023540 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023541 if (p != NULL)
23542 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023543 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023544 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023546 if (*p != TAB && needclr)
23547 {
23548 /* remove any text still there from the command */
23549 msg_clr_eos();
23550 needclr = FALSE;
23551 }
23552 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 }
23554 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023555 {
23556#ifdef FEAT_MBYTE
23557 if (has_mbyte)
23558 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023559 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023560
23561 (void)msg_outtrans_len_attr(p, i, echo_attr);
23562 p += i - 1;
23563 }
23564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023566 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023569 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023571 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 arg = skipwhite(arg);
23573 }
23574 eap->nextcmd = check_nextcmd(arg);
23575
23576 if (eap->skip)
23577 --emsg_skip;
23578 else
23579 {
23580 /* remove text that may still be there from the command */
23581 if (needclr)
23582 msg_clr_eos();
23583 if (eap->cmdidx == CMD_echo)
23584 msg_end();
23585 }
23586}
23587
23588/*
23589 * ":echohl {name}".
23590 */
23591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023592ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593{
23594 int id;
23595
23596 id = syn_name2id(eap->arg);
23597 if (id == 0)
23598 echo_attr = 0;
23599 else
23600 echo_attr = syn_id2attr(id);
23601}
23602
23603/*
23604 * ":execute expr1 ..." execute the result of an expression.
23605 * ":echomsg expr1 ..." Print a message
23606 * ":echoerr expr1 ..." Print an error
23607 * Each gets spaces around each argument and a newline at the end for
23608 * echo commands
23609 */
23610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023611ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612{
23613 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023614 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 int ret = OK;
23616 char_u *p;
23617 garray_T ga;
23618 int len;
23619 int save_did_emsg;
23620
23621 ga_init2(&ga, 1, 80);
23622
23623 if (eap->skip)
23624 ++emsg_skip;
23625 while (*arg != NUL && *arg != '|' && *arg != '\n')
23626 {
23627 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023628 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 {
23630 /*
23631 * Report the invalid expression unless the expression evaluation
23632 * has been cancelled due to an aborting error, an interrupt, or an
23633 * exception.
23634 */
23635 if (!aborting())
23636 EMSG2(_(e_invexpr2), p);
23637 ret = FAIL;
23638 break;
23639 }
23640
23641 if (!eap->skip)
23642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023643 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 len = (int)STRLEN(p);
23645 if (ga_grow(&ga, len + 2) == FAIL)
23646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023647 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 ret = FAIL;
23649 break;
23650 }
23651 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 ga.ga_len += len;
23655 }
23656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 arg = skipwhite(arg);
23659 }
23660
23661 if (ret != FAIL && ga.ga_data != NULL)
23662 {
23663 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023666 out_flush();
23667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668 else if (eap->cmdidx == CMD_echoerr)
23669 {
23670 /* We don't want to abort following commands, restore did_emsg. */
23671 save_did_emsg = did_emsg;
23672 EMSG((char_u *)ga.ga_data);
23673 if (!force_abort)
23674 did_emsg = save_did_emsg;
23675 }
23676 else if (eap->cmdidx == CMD_execute)
23677 do_cmdline((char_u *)ga.ga_data,
23678 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23679 }
23680
23681 ga_clear(&ga);
23682
23683 if (eap->skip)
23684 --emsg_skip;
23685
23686 eap->nextcmd = check_nextcmd(arg);
23687}
23688
23689/*
23690 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23691 * "arg" points to the "&" or '+' when called, to "option" when returning.
23692 * Returns NULL when no option name found. Otherwise pointer to the char
23693 * after the option name.
23694 */
23695 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023696find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697{
23698 char_u *p = *arg;
23699
23700 ++p;
23701 if (*p == 'g' && p[1] == ':')
23702 {
23703 *opt_flags = OPT_GLOBAL;
23704 p += 2;
23705 }
23706 else if (*p == 'l' && p[1] == ':')
23707 {
23708 *opt_flags = OPT_LOCAL;
23709 p += 2;
23710 }
23711 else
23712 *opt_flags = 0;
23713
23714 if (!ASCII_ISALPHA(*p))
23715 return NULL;
23716 *arg = p;
23717
23718 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23719 p += 4; /* termcap option */
23720 else
23721 while (ASCII_ISALPHA(*p))
23722 ++p;
23723 return p;
23724}
23725
23726/*
23727 * ":function"
23728 */
23729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023730ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731{
23732 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023733 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 int j;
23735 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023737 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 char_u *name = NULL;
23739 char_u *p;
23740 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023741 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 garray_T newargs;
23743 garray_T newlines;
23744 int varargs = FALSE;
23745 int mustend = FALSE;
23746 int flags = 0;
23747 ufunc_T *fp;
23748 int indent;
23749 int nesting;
23750 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023751 dictitem_T *v;
23752 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023753 static int func_nr = 0; /* number for nameless function */
23754 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023755 hashtab_T *ht;
23756 int todo;
23757 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023758 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759
23760 /*
23761 * ":function" without argument: list functions.
23762 */
23763 if (ends_excmd(*eap->arg))
23764 {
23765 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023767 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023768 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023769 {
23770 if (!HASHITEM_EMPTY(hi))
23771 {
23772 --todo;
23773 fp = HI2UF(hi);
23774 if (!isdigit(*fp->uf_name))
23775 list_func_head(fp, FALSE);
23776 }
23777 }
23778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 eap->nextcmd = check_nextcmd(eap->arg);
23780 return;
23781 }
23782
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023783 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023784 * ":function /pat": list functions matching pattern.
23785 */
23786 if (*eap->arg == '/')
23787 {
23788 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23789 if (!eap->skip)
23790 {
23791 regmatch_T regmatch;
23792
23793 c = *p;
23794 *p = NUL;
23795 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23796 *p = c;
23797 if (regmatch.regprog != NULL)
23798 {
23799 regmatch.rm_ic = p_ic;
23800
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023801 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023802 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23803 {
23804 if (!HASHITEM_EMPTY(hi))
23805 {
23806 --todo;
23807 fp = HI2UF(hi);
23808 if (!isdigit(*fp->uf_name)
23809 && vim_regexec(&regmatch, fp->uf_name, 0))
23810 list_func_head(fp, FALSE);
23811 }
23812 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023813 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023814 }
23815 }
23816 if (*p == '/')
23817 ++p;
23818 eap->nextcmd = check_nextcmd(p);
23819 return;
23820 }
23821
23822 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023823 * Get the function name. There are these situations:
23824 * func normal function name
23825 * "name" == func, "fudi.fd_dict" == NULL
23826 * dict.func new dictionary entry
23827 * "name" == NULL, "fudi.fd_dict" set,
23828 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23829 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023830 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23832 * dict.func existing dict entry that's not a Funcref
23833 * "name" == NULL, "fudi.fd_dict" set,
23834 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023835 * s:func script-local function name
23836 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 name = trans_function_name(&p, eap->skip, 0, &fudi);
23840 paren = (vim_strchr(p, '(') != NULL);
23841 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 {
23843 /*
23844 * Return on an invalid expression in braces, unless the expression
23845 * evaluation has been cancelled due to an aborting error, an
23846 * interrupt, or an exception.
23847 */
23848 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023849 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023850 if (!eap->skip && fudi.fd_newkey != NULL)
23851 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023852 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 else
23856 eap->skip = TRUE;
23857 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023858
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 /* An error in a function call during evaluation of an expression in magic
23860 * braces should not cause the function not to be defined. */
23861 saved_did_emsg = did_emsg;
23862 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863
23864 /*
23865 * ":function func" with only function name: list function.
23866 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023867 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 {
23869 if (!ends_excmd(*skipwhite(p)))
23870 {
23871 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023872 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 }
23874 eap->nextcmd = check_nextcmd(p);
23875 if (eap->nextcmd != NULL)
23876 *p = NUL;
23877 if (!eap->skip && !got_int)
23878 {
23879 fp = find_func(name);
23880 if (fp != NULL)
23881 {
23882 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023883 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023885 if (FUNCLINE(fp, j) == NULL)
23886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 msg_putchar('\n');
23888 msg_outnum((long)(j + 1));
23889 if (j < 9)
23890 msg_putchar(' ');
23891 if (j < 99)
23892 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023893 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 out_flush(); /* show a line at a time */
23895 ui_breakcheck();
23896 }
23897 if (!got_int)
23898 {
23899 msg_putchar('\n');
23900 msg_puts((char_u *)" endfunction");
23901 }
23902 }
23903 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023904 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 }
23908
23909 /*
23910 * ":function name(arg1, arg2)" Define function.
23911 */
23912 p = skipwhite(p);
23913 if (*p != '(')
23914 {
23915 if (!eap->skip)
23916 {
23917 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023918 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 }
23920 /* attempt to continue by skipping some text */
23921 if (vim_strchr(p, '(') != NULL)
23922 p = vim_strchr(p, '(');
23923 }
23924 p = skipwhite(p + 1);
23925
23926 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23927 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23928
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023929 if (!eap->skip)
23930 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023931 /* Check the name of the function. Unless it's a dictionary function
23932 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023933 if (name != NULL)
23934 arg = name;
23935 else
23936 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023937 if (arg != NULL && (fudi.fd_di == NULL
23938 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023939 {
23940 if (*arg == K_SPECIAL)
23941 j = 3;
23942 else
23943 j = 0;
23944 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23945 : eval_isnamec(arg[j])))
23946 ++j;
23947 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023948 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023949 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023950 /* Disallow using the g: dict. */
23951 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23952 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023953 }
23954
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 /*
23956 * Isolate the arguments: "arg1, arg2, ...)"
23957 */
23958 while (*p != ')')
23959 {
23960 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23961 {
23962 varargs = TRUE;
23963 p += 3;
23964 mustend = TRUE;
23965 }
23966 else
23967 {
23968 arg = p;
23969 while (ASCII_ISALNUM(*p) || *p == '_')
23970 ++p;
23971 if (arg == p || isdigit(*arg)
23972 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23973 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23974 {
23975 if (!eap->skip)
23976 EMSG2(_("E125: Illegal argument: %s"), arg);
23977 break;
23978 }
23979 if (ga_grow(&newargs, 1) == FAIL)
23980 goto erret;
23981 c = *p;
23982 *p = NUL;
23983 arg = vim_strsave(arg);
23984 if (arg == NULL)
23985 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023986
23987 /* Check for duplicate argument name. */
23988 for (i = 0; i < newargs.ga_len; ++i)
23989 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23990 {
23991 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023992 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023993 goto erret;
23994 }
23995
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23997 *p = c;
23998 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 if (*p == ',')
24000 ++p;
24001 else
24002 mustend = TRUE;
24003 }
24004 p = skipwhite(p);
24005 if (mustend && *p != ')')
24006 {
24007 if (!eap->skip)
24008 EMSG2(_(e_invarg2), eap->arg);
24009 break;
24010 }
24011 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024012 if (*p != ')')
24013 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 ++p; /* skip the ')' */
24015
Bram Moolenaare9a41262005-01-15 22:18:47 +000024016 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 for (;;)
24018 {
24019 p = skipwhite(p);
24020 if (STRNCMP(p, "range", 5) == 0)
24021 {
24022 flags |= FC_RANGE;
24023 p += 5;
24024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024025 else if (STRNCMP(p, "dict", 4) == 0)
24026 {
24027 flags |= FC_DICT;
24028 p += 4;
24029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 else if (STRNCMP(p, "abort", 5) == 0)
24031 {
24032 flags |= FC_ABORT;
24033 p += 5;
24034 }
24035 else
24036 break;
24037 }
24038
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024039 /* When there is a line break use what follows for the function body.
24040 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24041 if (*p == '\n')
24042 line_arg = p + 1;
24043 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 EMSG(_(e_trailing));
24045
24046 /*
24047 * Read the body of the function, until ":endfunction" is found.
24048 */
24049 if (KeyTyped)
24050 {
24051 /* Check if the function already exists, don't let the user type the
24052 * whole function before telling him it doesn't work! For a script we
24053 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024054 if (!eap->skip && !eap->forceit)
24055 {
24056 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24057 EMSG(_(e_funcdict));
24058 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024059 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024062 if (!eap->skip && did_emsg)
24063 goto erret;
24064
Bram Moolenaar071d4272004-06-13 20:20:40 +000024065 msg_putchar('\n'); /* don't overwrite the function name */
24066 cmdline_row = msg_row;
24067 }
24068
24069 indent = 2;
24070 nesting = 0;
24071 for (;;)
24072 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024073 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024074 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024075 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024076 saved_wait_return = FALSE;
24077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024079 sourcing_lnum_off = sourcing_lnum;
24080
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024081 if (line_arg != NULL)
24082 {
24083 /* Use eap->arg, split up in parts by line breaks. */
24084 theline = line_arg;
24085 p = vim_strchr(theline, '\n');
24086 if (p == NULL)
24087 line_arg += STRLEN(line_arg);
24088 else
24089 {
24090 *p = NUL;
24091 line_arg = p + 1;
24092 }
24093 }
24094 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 theline = getcmdline(':', 0L, indent);
24096 else
24097 theline = eap->getline(':', eap->cookie, indent);
24098 if (KeyTyped)
24099 lines_left = Rows - 1;
24100 if (theline == NULL)
24101 {
24102 EMSG(_("E126: Missing :endfunction"));
24103 goto erret;
24104 }
24105
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024106 /* Detect line continuation: sourcing_lnum increased more than one. */
24107 if (sourcing_lnum > sourcing_lnum_off + 1)
24108 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24109 else
24110 sourcing_lnum_off = 0;
24111
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 if (skip_until != NULL)
24113 {
24114 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24115 * don't check for ":endfunc". */
24116 if (STRCMP(theline, skip_until) == 0)
24117 {
24118 vim_free(skip_until);
24119 skip_until = NULL;
24120 }
24121 }
24122 else
24123 {
24124 /* skip ':' and blanks*/
24125 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24126 ;
24127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024128 /* Check for "endfunction". */
24129 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024131 if (line_arg == NULL)
24132 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 break;
24134 }
24135
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024136 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 * at "end". */
24138 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24139 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024140 else if (STRNCMP(p, "if", 2) == 0
24141 || STRNCMP(p, "wh", 2) == 0
24142 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 || STRNCMP(p, "try", 3) == 0)
24144 indent += 2;
24145
24146 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024147 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024149 if (*p == '!')
24150 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010024152 vim_free(trans_function_name(&p, TRUE, 0, NULL));
24153 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024155 ++nesting;
24156 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 }
24158 }
24159
24160 /* Check for ":append" or ":insert". */
24161 p = skip_range(p, NULL);
24162 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24163 || (p[0] == 'i'
24164 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24165 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24166 skip_until = vim_strsave((char_u *)".");
24167
24168 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24169 arg = skipwhite(skiptowhite(p));
24170 if (arg[0] == '<' && arg[1] =='<'
24171 && ((p[0] == 'p' && p[1] == 'y'
24172 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24173 || (p[0] == 'p' && p[1] == 'e'
24174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24175 || (p[0] == 't' && p[1] == 'c'
24176 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024177 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24178 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24180 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024181 || (p[0] == 'm' && p[1] == 'z'
24182 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 ))
24184 {
24185 /* ":python <<" continues until a dot, like ":append" */
24186 p = skipwhite(arg + 2);
24187 if (*p == NUL)
24188 skip_until = vim_strsave((char_u *)".");
24189 else
24190 skip_until = vim_strsave(p);
24191 }
24192 }
24193
24194 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024195 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024196 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024197 if (line_arg == NULL)
24198 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024200 }
24201
24202 /* Copy the line to newly allocated memory. get_one_sourceline()
24203 * allocates 250 bytes per line, this saves 80% on average. The cost
24204 * is an extra alloc/free. */
24205 p = vim_strsave(theline);
24206 if (p != NULL)
24207 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024208 if (line_arg == NULL)
24209 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024210 theline = p;
24211 }
24212
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024213 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24214
24215 /* Add NULL lines for continuation lines, so that the line count is
24216 * equal to the index in the growarray. */
24217 while (sourcing_lnum_off-- > 0)
24218 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024219
24220 /* Check for end of eap->arg. */
24221 if (line_arg != NULL && *line_arg == NUL)
24222 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 }
24224
24225 /* Don't define the function when skipping commands or when an error was
24226 * detected. */
24227 if (eap->skip || did_emsg)
24228 goto erret;
24229
24230 /*
24231 * If there are no errors, add the function
24232 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024233 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024234 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024235 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024236 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024238 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024239 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024240 goto erret;
24241 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024242
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 fp = find_func(name);
24244 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 if (!eap->forceit)
24247 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024248 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 goto erret;
24250 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024251 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024253 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024254 name);
24255 goto erret;
24256 }
24257 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024258 ga_clear_strings(&(fp->uf_args));
24259 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024260 vim_free(name);
24261 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 }
24264 else
24265 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266 char numbuf[20];
24267
24268 fp = NULL;
24269 if (fudi.fd_newkey == NULL && !eap->forceit)
24270 {
24271 EMSG(_(e_funcdict));
24272 goto erret;
24273 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024274 if (fudi.fd_di == NULL)
24275 {
24276 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024277 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024278 goto erret;
24279 }
24280 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024281 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024282 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024283
24284 /* Give the function a sequential number. Can only be used with a
24285 * Funcref! */
24286 vim_free(name);
24287 sprintf(numbuf, "%d", ++func_nr);
24288 name = vim_strsave((char_u *)numbuf);
24289 if (name == NULL)
24290 goto erret;
24291 }
24292
24293 if (fp == NULL)
24294 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024295 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 {
24297 int slen, plen;
24298 char_u *scriptname;
24299
24300 /* Check that the autoload name matches the script name. */
24301 j = FAIL;
24302 if (sourcing_name != NULL)
24303 {
24304 scriptname = autoload_name(name);
24305 if (scriptname != NULL)
24306 {
24307 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024308 plen = (int)STRLEN(p);
24309 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 if (slen > plen && fnamecmp(p,
24311 sourcing_name + slen - plen) == 0)
24312 j = OK;
24313 vim_free(scriptname);
24314 }
24315 }
24316 if (j == FAIL)
24317 {
24318 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24319 goto erret;
24320 }
24321 }
24322
24323 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 if (fp == NULL)
24325 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024326
24327 if (fudi.fd_dict != NULL)
24328 {
24329 if (fudi.fd_di == NULL)
24330 {
24331 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024332 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024333 if (fudi.fd_di == NULL)
24334 {
24335 vim_free(fp);
24336 goto erret;
24337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024338 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24339 {
24340 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024341 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024342 goto erret;
24343 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 }
24345 else
24346 /* overwrite existing dict entry */
24347 clear_tv(&fudi.fd_di->di_tv);
24348 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024349 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024352
24353 /* behave like "dict" was used */
24354 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024355 }
24356
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024358 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024359 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24360 {
24361 vim_free(fp);
24362 goto erret;
24363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 fp->uf_args = newargs;
24366 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367#ifdef FEAT_PROFILE
24368 fp->uf_tml_count = NULL;
24369 fp->uf_tml_total = NULL;
24370 fp->uf_tml_self = NULL;
24371 fp->uf_profiling = FALSE;
24372 if (prof_def_func())
24373 func_do_profile(fp);
24374#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024375 fp->uf_varargs = varargs;
24376 fp->uf_flags = flags;
24377 fp->uf_calls = 0;
24378 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380
24381erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382 ga_clear_strings(&newargs);
24383 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024384ret_free:
24385 vim_free(skip_until);
24386 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024389 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390}
24391
24392/*
24393 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024394 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024396 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024397 * TFN_INT: internal function name OK
24398 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024399 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 * Advances "pp" to just after the function name (if no error).
24401 */
24402 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403trans_function_name(
24404 char_u **pp,
24405 int skip, /* only find the end, don't evaluate */
24406 int flags,
24407 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024409 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 char_u *start;
24411 char_u *end;
24412 int lead;
24413 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024415 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024416
24417 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024418 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024420
24421 /* Check for hard coded <SNR>: already translated function ID (from a user
24422 * command). */
24423 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24424 && (*pp)[2] == (int)KE_SNR)
24425 {
24426 *pp += 3;
24427 len = get_id_len(pp) + 3;
24428 return vim_strnsave(start, len);
24429 }
24430
24431 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24432 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024434 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024436
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024437 /* Note that TFN_ flags use the same values as GLV_ flags. */
24438 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024439 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024440 if (end == start)
24441 {
24442 if (!skip)
24443 EMSG(_("E129: Function name required"));
24444 goto theend;
24445 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024446 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024447 {
24448 /*
24449 * Report an invalid expression in braces, unless the expression
24450 * evaluation has been cancelled due to an aborting error, an
24451 * interrupt, or an exception.
24452 */
24453 if (!aborting())
24454 {
24455 if (end != NULL)
24456 EMSG2(_(e_invarg2), start);
24457 }
24458 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024459 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024460 goto theend;
24461 }
24462
24463 if (lv.ll_tv != NULL)
24464 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024465 if (fdp != NULL)
24466 {
24467 fdp->fd_dict = lv.ll_dict;
24468 fdp->fd_newkey = lv.ll_newkey;
24469 lv.ll_newkey = NULL;
24470 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024472 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24473 {
24474 name = vim_strsave(lv.ll_tv->vval.v_string);
24475 *pp = end;
24476 }
24477 else
24478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024479 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24480 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024481 EMSG(_(e_funcref));
24482 else
24483 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024484 name = NULL;
24485 }
24486 goto theend;
24487 }
24488
24489 if (lv.ll_name == NULL)
24490 {
24491 /* Error found, but continue after the function name. */
24492 *pp = end;
24493 goto theend;
24494 }
24495
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024496 /* Check if the name is a Funcref. If so, use the value. */
24497 if (lv.ll_exp_name != NULL)
24498 {
24499 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024500 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024501 if (name == lv.ll_exp_name)
24502 name = NULL;
24503 }
24504 else
24505 {
24506 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024507 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024508 if (name == *pp)
24509 name = NULL;
24510 }
24511 if (name != NULL)
24512 {
24513 name = vim_strsave(name);
24514 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024515 if (STRNCMP(name, "<SNR>", 5) == 0)
24516 {
24517 /* Change "<SNR>" to the byte sequence. */
24518 name[0] = K_SPECIAL;
24519 name[1] = KS_EXTRA;
24520 name[2] = (int)KE_SNR;
24521 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24522 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024523 goto theend;
24524 }
24525
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024526 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024528 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024529 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24530 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24531 {
24532 /* When there was "s:" already or the name expanded to get a
24533 * leading "s:" then remove it. */
24534 lv.ll_name += 2;
24535 len -= 2;
24536 lead = 2;
24537 }
24538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024539 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024540 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024541 /* skip over "s:" and "g:" */
24542 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024543 lv.ll_name += 2;
24544 len = (int)(end - lv.ll_name);
24545 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024546
24547 /*
24548 * Copy the function name to allocated memory.
24549 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24550 * Accept <SNR>123_name() outside a script.
24551 */
24552 if (skip)
24553 lead = 0; /* do nothing */
24554 else if (lead > 0)
24555 {
24556 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024557 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24558 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024559 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024560 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024561 if (current_SID <= 0)
24562 {
24563 EMSG(_(e_usingsid));
24564 goto theend;
24565 }
24566 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24567 lead += (int)STRLEN(sid_buf);
24568 }
24569 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024570 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024571 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024572 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024573 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024574 goto theend;
24575 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024576 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024577 {
24578 char_u *cp = vim_strchr(lv.ll_name, ':');
24579
24580 if (cp != NULL && cp < end)
24581 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024582 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024583 goto theend;
24584 }
24585 }
24586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024587 name = alloc((unsigned)(len + lead + 1));
24588 if (name != NULL)
24589 {
24590 if (lead > 0)
24591 {
24592 name[0] = K_SPECIAL;
24593 name[1] = KS_EXTRA;
24594 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024595 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024596 STRCPY(name + 3, sid_buf);
24597 }
24598 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024599 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024600 }
24601 *pp = end;
24602
24603theend:
24604 clear_lval(&lv);
24605 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024606}
24607
24608/*
24609 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24610 * Return 2 if "p" starts with "s:".
24611 * Return 0 otherwise.
24612 */
24613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024614eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024616 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24617 * the standard library function. */
24618 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24619 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 return 5;
24621 if (p[0] == 's' && p[1] == ':')
24622 return 2;
24623 return 0;
24624}
24625
24626/*
24627 * Return TRUE if "p" starts with "<SID>" or "s:".
24628 * Only works if eval_fname_script() returned non-zero for "p"!
24629 */
24630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024631eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632{
24633 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24634}
24635
24636/*
24637 * List the head of the function: "name(arg1, arg2)".
24638 */
24639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024640list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641{
24642 int j;
24643
24644 msg_start();
24645 if (indent)
24646 MSG_PUTS(" ");
24647 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024648 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 {
24650 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024651 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 }
24653 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024654 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024656 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 {
24658 if (j)
24659 MSG_PUTS(", ");
24660 msg_puts(FUNCARG(fp, j));
24661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024662 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 {
24664 if (j)
24665 MSG_PUTS(", ");
24666 MSG_PUTS("...");
24667 }
24668 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024669 if (fp->uf_flags & FC_ABORT)
24670 MSG_PUTS(" abort");
24671 if (fp->uf_flags & FC_RANGE)
24672 MSG_PUTS(" range");
24673 if (fp->uf_flags & FC_DICT)
24674 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024675 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024676 if (p_verbose > 0)
24677 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678}
24679
24680/*
24681 * Find a function by name, return pointer to it in ufuncs.
24682 * Return NULL for unknown function.
24683 */
24684 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024685find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024687 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 hi = hash_find(&func_hashtab, name);
24690 if (!HASHITEM_EMPTY(hi))
24691 return HI2UF(hi);
24692 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693}
24694
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024695#if defined(EXITFREE) || defined(PROTO)
24696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024697free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024698{
24699 hashitem_T *hi;
24700
24701 /* Need to start all over every time, because func_free() may change the
24702 * hash table. */
24703 while (func_hashtab.ht_used > 0)
24704 for (hi = func_hashtab.ht_array; ; ++hi)
24705 if (!HASHITEM_EMPTY(hi))
24706 {
24707 func_free(HI2UF(hi));
24708 break;
24709 }
24710}
24711#endif
24712
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024714translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024715{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024716 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024717 return find_internal_func(name) >= 0;
24718 return find_func(name) != NULL;
24719}
24720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024721/*
24722 * Return TRUE if a function "name" exists.
24723 */
24724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024725function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024726{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024727 char_u *nm = name;
24728 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024729 int n = FALSE;
24730
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024731 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24732 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024733 nm = skipwhite(nm);
24734
24735 /* Only accept "funcname", "funcname ", "funcname (..." and
24736 * "funcname(...", not "funcname!...". */
24737 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024738 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024739 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024740 return n;
24741}
24742
Bram Moolenaara1544c02013-05-30 12:35:52 +020024743 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024745{
24746 char_u *nm = name;
24747 char_u *p;
24748
24749 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24750
24751 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024752 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024753 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024754
Bram Moolenaara1544c02013-05-30 12:35:52 +020024755 vim_free(p);
24756 return NULL;
24757}
24758
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024759/*
24760 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024761 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24762 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024763 */
24764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024765builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024766{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024767 char_u *p;
24768
24769 if (!ASCII_ISLOWER(name[0]))
24770 return FALSE;
24771 p = vim_strchr(name, AUTOLOAD_CHAR);
24772 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024773}
24774
Bram Moolenaar05159a02005-02-26 23:04:13 +000024775#if defined(FEAT_PROFILE) || defined(PROTO)
24776/*
24777 * Start profiling function "fp".
24778 */
24779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024782 int len = fp->uf_lines.ga_len;
24783
24784 if (len == 0)
24785 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786 fp->uf_tm_count = 0;
24787 profile_zero(&fp->uf_tm_self);
24788 profile_zero(&fp->uf_tm_total);
24789 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024790 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024791 if (fp->uf_tml_total == NULL)
24792 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024793 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024794 if (fp->uf_tml_self == NULL)
24795 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024796 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024797 fp->uf_tml_idx = -1;
24798 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24799 || fp->uf_tml_self == NULL)
24800 return; /* out of memory */
24801
24802 fp->uf_profiling = TRUE;
24803}
24804
24805/*
24806 * Dump the profiling results for all functions in file "fd".
24807 */
24808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024809func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024810{
24811 hashitem_T *hi;
24812 int todo;
24813 ufunc_T *fp;
24814 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024815 ufunc_T **sorttab;
24816 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024817
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024818 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024819 if (todo == 0)
24820 return; /* nothing to dump */
24821
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024822 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024823
Bram Moolenaar05159a02005-02-26 23:04:13 +000024824 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24825 {
24826 if (!HASHITEM_EMPTY(hi))
24827 {
24828 --todo;
24829 fp = HI2UF(hi);
24830 if (fp->uf_profiling)
24831 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024832 if (sorttab != NULL)
24833 sorttab[st_len++] = fp;
24834
Bram Moolenaar05159a02005-02-26 23:04:13 +000024835 if (fp->uf_name[0] == K_SPECIAL)
24836 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24837 else
24838 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24839 if (fp->uf_tm_count == 1)
24840 fprintf(fd, "Called 1 time\n");
24841 else
24842 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24843 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24844 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24845 fprintf(fd, "\n");
24846 fprintf(fd, "count total (s) self (s)\n");
24847
24848 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24849 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024850 if (FUNCLINE(fp, i) == NULL)
24851 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024852 prof_func_line(fd, fp->uf_tml_count[i],
24853 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024854 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24855 }
24856 fprintf(fd, "\n");
24857 }
24858 }
24859 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024860
24861 if (sorttab != NULL && st_len > 0)
24862 {
24863 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24864 prof_total_cmp);
24865 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24866 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24867 prof_self_cmp);
24868 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24869 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024870
24871 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024872}
Bram Moolenaar73830342005-02-28 22:48:19 +000024873
24874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024875prof_sort_list(
24876 FILE *fd,
24877 ufunc_T **sorttab,
24878 int st_len,
24879 char *title,
24880 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024881{
24882 int i;
24883 ufunc_T *fp;
24884
24885 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24886 fprintf(fd, "count total (s) self (s) function\n");
24887 for (i = 0; i < 20 && i < st_len; ++i)
24888 {
24889 fp = sorttab[i];
24890 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24891 prefer_self);
24892 if (fp->uf_name[0] == K_SPECIAL)
24893 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24894 else
24895 fprintf(fd, " %s()\n", fp->uf_name);
24896 }
24897 fprintf(fd, "\n");
24898}
24899
24900/*
24901 * Print the count and times for one function or function line.
24902 */
24903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024904prof_func_line(
24905 FILE *fd,
24906 int count,
24907 proftime_T *total,
24908 proftime_T *self,
24909 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024910{
24911 if (count > 0)
24912 {
24913 fprintf(fd, "%5d ", count);
24914 if (prefer_self && profile_equal(total, self))
24915 fprintf(fd, " ");
24916 else
24917 fprintf(fd, "%s ", profile_msg(total));
24918 if (!prefer_self && profile_equal(total, self))
24919 fprintf(fd, " ");
24920 else
24921 fprintf(fd, "%s ", profile_msg(self));
24922 }
24923 else
24924 fprintf(fd, " ");
24925}
24926
24927/*
24928 * Compare function for total time sorting.
24929 */
24930 static int
24931#ifdef __BORLANDC__
24932_RTLENTRYF
24933#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024934prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024935{
24936 ufunc_T *p1, *p2;
24937
24938 p1 = *(ufunc_T **)s1;
24939 p2 = *(ufunc_T **)s2;
24940 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24941}
24942
24943/*
24944 * Compare function for self time sorting.
24945 */
24946 static int
24947#ifdef __BORLANDC__
24948_RTLENTRYF
24949#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024950prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024951{
24952 ufunc_T *p1, *p2;
24953
24954 p1 = *(ufunc_T **)s1;
24955 p2 = *(ufunc_T **)s2;
24956 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24957}
24958
Bram Moolenaar05159a02005-02-26 23:04:13 +000024959#endif
24960
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024961/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024962 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024963 * Return TRUE if a package was loaded.
24964 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024966script_autoload(
24967 char_u *name,
24968 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024969{
24970 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024971 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024972 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024973 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024974
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024975 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024976 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024977 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024978 return FALSE;
24979
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024980 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024981
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024982 /* Find the name in the list of previously loaded package names. Skip
24983 * "autoload/", it's always the same. */
24984 for (i = 0; i < ga_loaded.ga_len; ++i)
24985 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24986 break;
24987 if (!reload && i < ga_loaded.ga_len)
24988 ret = FALSE; /* was loaded already */
24989 else
24990 {
24991 /* Remember the name if it wasn't loaded already. */
24992 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24993 {
24994 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24995 tofree = NULL;
24996 }
24997
24998 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024999 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025000 ret = TRUE;
25001 }
25002
25003 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025004 return ret;
25005}
25006
25007/*
25008 * Return the autoload script name for a function or variable name.
25009 * Returns NULL when out of memory.
25010 */
25011 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025012autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025013{
25014 char_u *p;
25015 char_u *scriptname;
25016
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025017 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025018 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25019 if (scriptname == NULL)
25020 return FALSE;
25021 STRCPY(scriptname, "autoload/");
25022 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025023 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025024 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025025 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025026 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025027 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025028}
25029
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25031
25032/*
25033 * Function given to ExpandGeneric() to obtain the list of user defined
25034 * function names.
25035 */
25036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025037get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025039 static long_u done;
25040 static hashitem_T *hi;
25041 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042
25043 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025045 done = 0;
25046 hi = func_hashtab.ht_array;
25047 }
25048 if (done < func_hashtab.ht_used)
25049 {
25050 if (done++ > 0)
25051 ++hi;
25052 while (HASHITEM_EMPTY(hi))
25053 ++hi;
25054 fp = HI2UF(hi);
25055
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025056 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025057 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025059 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25060 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061
25062 cat_func_name(IObuff, fp);
25063 if (xp->xp_context != EXPAND_USER_FUNC)
25064 {
25065 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025066 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067 STRCAT(IObuff, ")");
25068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 return IObuff;
25070 }
25071 return NULL;
25072}
25073
25074#endif /* FEAT_CMDL_COMPL */
25075
25076/*
25077 * Copy the function name of "fp" to buffer "buf".
25078 * "buf" must be able to hold the function name plus three bytes.
25079 * Takes care of script-local function names.
25080 */
25081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025082cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025084 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 {
25086 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025087 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 }
25089 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025090 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091}
25092
25093/*
25094 * ":delfunction {name}"
25095 */
25096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025097ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025099 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 char_u *p;
25101 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025102 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103
25104 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025105 name = trans_function_name(&p, eap->skip, 0, &fudi);
25106 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025108 {
25109 if (fudi.fd_dict != NULL && !eap->skip)
25110 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 if (!ends_excmd(*skipwhite(p)))
25114 {
25115 vim_free(name);
25116 EMSG(_(e_trailing));
25117 return;
25118 }
25119 eap->nextcmd = check_nextcmd(p);
25120 if (eap->nextcmd != NULL)
25121 *p = NUL;
25122
25123 if (!eap->skip)
25124 fp = find_func(name);
25125 vim_free(name);
25126
25127 if (!eap->skip)
25128 {
25129 if (fp == NULL)
25130 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025131 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 return;
25133 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025134 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135 {
25136 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25137 return;
25138 }
25139
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025140 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025142 /* Delete the dict item that refers to the function, it will
25143 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025144 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025146 else
25147 func_free(fp);
25148 }
25149}
25150
25151/*
25152 * Free a function and remove it from the list of functions.
25153 */
25154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025155func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025156{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025157 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025158
25159 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025160 ga_clear_strings(&(fp->uf_args));
25161 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025162#ifdef FEAT_PROFILE
25163 vim_free(fp->uf_tml_count);
25164 vim_free(fp->uf_tml_total);
25165 vim_free(fp->uf_tml_self);
25166#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025167
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025168 /* remove the function from the function hashtable */
25169 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25170 if (HASHITEM_EMPTY(hi))
25171 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025172 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025173 hash_remove(&func_hashtab, hi);
25174
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025175 vim_free(fp);
25176}
25177
25178/*
25179 * Unreference a Function: decrement the reference count and free it when it
25180 * becomes zero. Only for numbered functions.
25181 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025183func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025184{
25185 ufunc_T *fp;
25186
25187 if (name != NULL && isdigit(*name))
25188 {
25189 fp = find_func(name);
25190 if (fp == NULL)
25191 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025192 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025193 {
25194 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025195 * when "uf_calls" becomes zero. */
25196 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025197 func_free(fp);
25198 }
25199 }
25200}
25201
25202/*
25203 * Count a reference to a Function.
25204 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025206func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025207{
25208 ufunc_T *fp;
25209
25210 if (name != NULL && isdigit(*name))
25211 {
25212 fp = find_func(name);
25213 if (fp == NULL)
25214 EMSG2(_(e_intern2), "func_ref()");
25215 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025216 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217 }
25218}
25219
25220/*
25221 * Call a user function.
25222 */
25223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025224call_user_func(
25225 ufunc_T *fp, /* pointer to function */
25226 int argcount, /* nr of args */
25227 typval_T *argvars, /* arguments */
25228 typval_T *rettv, /* return value */
25229 linenr_T firstline, /* first line of range */
25230 linenr_T lastline, /* last line of range */
25231 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232{
Bram Moolenaar33570922005-01-25 22:26:29 +000025233 char_u *save_sourcing_name;
25234 linenr_T save_sourcing_lnum;
25235 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025236 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025237 int save_did_emsg;
25238 static int depth = 0;
25239 dictitem_T *v;
25240 int fixvar_idx = 0; /* index in fixvar[] */
25241 int i;
25242 int ai;
25243 char_u numbuf[NUMBUFLEN];
25244 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025245 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025246#ifdef FEAT_PROFILE
25247 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025248 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025250
25251 /* If depth of calling is getting too high, don't execute the function */
25252 if (depth >= p_mfd)
25253 {
25254 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025255 rettv->v_type = VAR_NUMBER;
25256 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 return;
25258 }
25259 ++depth;
25260
25261 line_breakcheck(); /* check for CTRL-C hit */
25262
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025263 fc = (funccall_T *)alloc(sizeof(funccall_T));
25264 fc->caller = current_funccal;
25265 current_funccal = fc;
25266 fc->func = fp;
25267 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025268 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025269 fc->linenr = 0;
25270 fc->returned = FALSE;
25271 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025273 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25274 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025277 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025278 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25279 * each argument variable and saves a lot of time.
25280 */
25281 /*
25282 * Init l: variables.
25283 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025284 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025285 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025286 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025287 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25288 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025289 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025290 name = v->di_key;
25291 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025293 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025295 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025296 v->di_tv.vval.v_dict = selfdict;
25297 ++selfdict->dv_refcount;
25298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025299
Bram Moolenaar33570922005-01-25 22:26:29 +000025300 /*
25301 * Init a: variables.
25302 * Set a:0 to "argcount".
25303 * Set a:000 to a list with room for the "..." arguments.
25304 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025305 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025306 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025307 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025308 /* Use "name" to avoid a warning from some compiler that checks the
25309 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025310 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025311 name = v->di_key;
25312 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025314 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025315 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025316 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025317 v->di_tv.vval.v_list = &fc->l_varlist;
25318 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25319 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25320 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025321
25322 /*
25323 * Set a:firstline to "firstline" and a:lastline to "lastline".
25324 * Set a:name to named arguments.
25325 * Set a:N to the "..." arguments.
25326 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025327 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025328 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025329 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025330 (varnumber_T)lastline);
25331 for (i = 0; i < argcount; ++i)
25332 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025333 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025334 if (ai < 0)
25335 /* named argument a:name */
25336 name = FUNCARG(fp, i);
25337 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025338 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025339 /* "..." argument a:1, a:2, etc. */
25340 sprintf((char *)numbuf, "%d", ai + 1);
25341 name = numbuf;
25342 }
25343 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25344 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025345 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25347 }
25348 else
25349 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025350 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25351 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025352 if (v == NULL)
25353 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025354 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025355 }
25356 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025357 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025358
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025359 /* Note: the values are copied directly to avoid alloc/free.
25360 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025361 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025362 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025363
25364 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25365 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025366 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25367 fc->l_listitems[ai].li_tv = argvars[i];
25368 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025369 }
25370 }
25371
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 /* Don't redraw while executing the function. */
25373 ++RedrawingDisabled;
25374 save_sourcing_name = sourcing_name;
25375 save_sourcing_lnum = sourcing_lnum;
25376 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025377 /* need space for function name + ("function " + 3) or "[number]" */
25378 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25379 + STRLEN(fp->uf_name) + 20;
25380 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 if (sourcing_name != NULL)
25382 {
25383 if (save_sourcing_name != NULL
25384 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025385 sprintf((char *)sourcing_name, "%s[%d]..",
25386 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 else
25388 STRCPY(sourcing_name, "function ");
25389 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25390
25391 if (p_verbose >= 12)
25392 {
25393 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025394 verbose_enter_scroll();
25395
Bram Moolenaar555b2802005-05-19 21:08:39 +000025396 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 if (p_verbose >= 14)
25398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025400 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025401 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025402 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403
25404 msg_puts((char_u *)"(");
25405 for (i = 0; i < argcount; ++i)
25406 {
25407 if (i > 0)
25408 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025409 if (argvars[i].v_type == VAR_NUMBER)
25410 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 else
25412 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025413 /* Do not want errors such as E724 here. */
25414 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025415 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025416 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025417 if (s != NULL)
25418 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025419 if (vim_strsize(s) > MSG_BUF_CLEN)
25420 {
25421 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25422 s = buf;
25423 }
25424 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025425 vim_free(tofree);
25426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 }
25428 }
25429 msg_puts((char_u *)")");
25430 }
25431 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025432
25433 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 --no_wait_return;
25435 }
25436 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025437#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025438 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025439 {
25440 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25441 func_do_profile(fp);
25442 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025443 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025444 {
25445 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025446 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025447 profile_zero(&fp->uf_tm_children);
25448 }
25449 script_prof_save(&wait_start);
25450 }
25451#endif
25452
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025454 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 save_did_emsg = did_emsg;
25456 did_emsg = FALSE;
25457
25458 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25461
25462 --RedrawingDisabled;
25463
25464 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025465 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025467 clear_tv(rettv);
25468 rettv->v_type = VAR_NUMBER;
25469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 }
25471
Bram Moolenaar05159a02005-02-26 23:04:13 +000025472#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025473 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025474 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025475 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025476 profile_end(&call_start);
25477 profile_sub_wait(&wait_start, &call_start);
25478 profile_add(&fp->uf_tm_total, &call_start);
25479 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025480 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025481 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025482 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25483 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025484 }
25485 }
25486#endif
25487
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 /* when being verbose, mention the return value */
25489 if (p_verbose >= 12)
25490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025492 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025495 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025496 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025497 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025498 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025501 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025502 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025503 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025504 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025505
Bram Moolenaar555b2802005-05-19 21:08:39 +000025506 /* The value may be very long. Skip the middle part, so that we
25507 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025508 * truncate it at the end. Don't want errors such as E724 here. */
25509 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025510 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025511 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025512 if (s != NULL)
25513 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025514 if (vim_strsize(s) > MSG_BUF_CLEN)
25515 {
25516 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25517 s = buf;
25518 }
25519 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025520 vim_free(tofree);
25521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 }
25523 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025524
25525 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 --no_wait_return;
25527 }
25528
25529 vim_free(sourcing_name);
25530 sourcing_name = save_sourcing_name;
25531 sourcing_lnum = save_sourcing_lnum;
25532 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025533#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025534 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025535 script_prof_restore(&wait_start);
25536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537
25538 if (p_verbose >= 12 && sourcing_name != NULL)
25539 {
25540 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025541 verbose_enter_scroll();
25542
Bram Moolenaar555b2802005-05-19 21:08:39 +000025543 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025545
25546 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 --no_wait_return;
25548 }
25549
25550 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025551 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025552 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025553
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025554 /* If the a:000 list and the l: and a: dicts are not referenced we can
25555 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025556 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25557 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25558 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25559 {
25560 free_funccal(fc, FALSE);
25561 }
25562 else
25563 {
25564 hashitem_T *hi;
25565 listitem_T *li;
25566 int todo;
25567
25568 /* "fc" is still in use. This can happen when returning "a:000" or
25569 * assigning "l:" to a global variable.
25570 * Link "fc" in the list for garbage collection later. */
25571 fc->caller = previous_funccal;
25572 previous_funccal = fc;
25573
25574 /* Make a copy of the a: variables, since we didn't do that above. */
25575 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25576 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25577 {
25578 if (!HASHITEM_EMPTY(hi))
25579 {
25580 --todo;
25581 v = HI2DI(hi);
25582 copy_tv(&v->di_tv, &v->di_tv);
25583 }
25584 }
25585
25586 /* Make a copy of the a:000 items, since we didn't do that above. */
25587 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25588 copy_tv(&li->li_tv, &li->li_tv);
25589 }
25590}
25591
25592/*
25593 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025594 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025595 */
25596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025598{
25599 return (fc->l_varlist.lv_copyID != copyID
25600 && fc->l_vars.dv_copyID != copyID
25601 && fc->l_avars.dv_copyID != copyID);
25602}
25603
25604/*
25605 * Free "fc" and what it contains.
25606 */
25607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025608free_funccal(
25609 funccall_T *fc,
25610 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025611{
25612 listitem_T *li;
25613
25614 /* The a: variables typevals may not have been allocated, only free the
25615 * allocated variables. */
25616 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25617
25618 /* free all l: variables */
25619 vars_clear(&fc->l_vars.dv_hashtab);
25620
25621 /* Free the a:000 variables if they were allocated. */
25622 if (free_val)
25623 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25624 clear_tv(&li->li_tv);
25625
25626 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627}
25628
25629/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025630 * Add a number variable "name" to dict "dp" with value "nr".
25631 */
25632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025633add_nr_var(
25634 dict_T *dp,
25635 dictitem_T *v,
25636 char *name,
25637 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025638{
25639 STRCPY(v->di_key, name);
25640 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25641 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25642 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025643 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025644 v->di_tv.vval.v_number = nr;
25645}
25646
25647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 * ":return [expr]"
25649 */
25650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025651ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652{
25653 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025654 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 int returning = FALSE;
25656
25657 if (current_funccal == NULL)
25658 {
25659 EMSG(_("E133: :return not inside a function"));
25660 return;
25661 }
25662
25663 if (eap->skip)
25664 ++emsg_skip;
25665
25666 eap->nextcmd = NULL;
25667 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025668 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 {
25670 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025671 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025673 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 }
25675 /* It's safer to return also on error. */
25676 else if (!eap->skip)
25677 {
25678 /*
25679 * Return unless the expression evaluation has been cancelled due to an
25680 * aborting error, an interrupt, or an exception.
25681 */
25682 if (!aborting())
25683 returning = do_return(eap, FALSE, TRUE, NULL);
25684 }
25685
25686 /* When skipping or the return gets pending, advance to the next command
25687 * in this line (!returning). Otherwise, ignore the rest of the line.
25688 * Following lines will be ignored by get_func_line(). */
25689 if (returning)
25690 eap->nextcmd = NULL;
25691 else if (eap->nextcmd == NULL) /* no argument */
25692 eap->nextcmd = check_nextcmd(arg);
25693
25694 if (eap->skip)
25695 --emsg_skip;
25696}
25697
25698/*
25699 * Return from a function. Possibly makes the return pending. Also called
25700 * for a pending return at the ":endtry" or after returning from an extra
25701 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025702 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025703 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 * FALSE when the return gets pending.
25705 */
25706 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025707do_return(
25708 exarg_T *eap,
25709 int reanimate,
25710 int is_cmd,
25711 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712{
25713 int idx;
25714 struct condstack *cstack = eap->cstack;
25715
25716 if (reanimate)
25717 /* Undo the return. */
25718 current_funccal->returned = FALSE;
25719
25720 /*
25721 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25722 * not in its finally clause (which then is to be executed next) is found.
25723 * In this case, make the ":return" pending for execution at the ":endtry".
25724 * Otherwise, return normally.
25725 */
25726 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25727 if (idx >= 0)
25728 {
25729 cstack->cs_pending[idx] = CSTP_RETURN;
25730
25731 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025732 /* A pending return again gets pending. "rettv" points to an
25733 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025735 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736 else
25737 {
25738 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025739 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025741 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025743 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744 {
25745 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025746 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025747 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748 else
25749 EMSG(_(e_outofmem));
25750 }
25751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025752 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753
25754 if (reanimate)
25755 {
25756 /* The pending return value could be overwritten by a ":return"
25757 * without argument in a finally clause; reset the default
25758 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025759 current_funccal->rettv->v_type = VAR_NUMBER;
25760 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 }
25762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025763 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 }
25765 else
25766 {
25767 current_funccal->returned = TRUE;
25768
25769 /* If the return is carried out now, store the return value. For
25770 * a return immediately after reanimation, the value is already
25771 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025772 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025774 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025775 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025777 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 }
25779 }
25780
25781 return idx < 0;
25782}
25783
25784/*
25785 * Free the variable with a pending return value.
25786 */
25787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025788discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789{
Bram Moolenaar33570922005-01-25 22:26:29 +000025790 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791}
25792
25793/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025794 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 * is an allocated string. Used by report_pending() for verbose messages.
25796 */
25797 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025798get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025800 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025801 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025802 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025803
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025804 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025805 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025806 if (s == NULL)
25807 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025808
25809 STRCPY(IObuff, ":return ");
25810 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25811 if (STRLEN(s) + 8 >= IOSIZE)
25812 STRCPY(IObuff + IOSIZE - 4, "...");
25813 vim_free(tofree);
25814 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815}
25816
25817/*
25818 * Get next function line.
25819 * Called by do_cmdline() to get the next line.
25820 * Returns allocated string, or NULL for end of function.
25821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025823get_func_line(
25824 int c UNUSED,
25825 void *cookie,
25826 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025827{
Bram Moolenaar33570922005-01-25 22:26:29 +000025828 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025829 ufunc_T *fp = fcp->func;
25830 char_u *retval;
25831 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832
25833 /* If breakpoints have been added/deleted need to check for it. */
25834 if (fcp->dbg_tick != debug_tick)
25835 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025836 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837 sourcing_lnum);
25838 fcp->dbg_tick = debug_tick;
25839 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025840#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025842 func_line_end(cookie);
25843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844
Bram Moolenaar05159a02005-02-26 23:04:13 +000025845 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025846 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25847 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 retval = NULL;
25849 else
25850 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025851 /* Skip NULL lines (continuation lines). */
25852 while (fcp->linenr < gap->ga_len
25853 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25854 ++fcp->linenr;
25855 if (fcp->linenr >= gap->ga_len)
25856 retval = NULL;
25857 else
25858 {
25859 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25860 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025861#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025862 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025863 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025864#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 }
25867
25868 /* Did we encounter a breakpoint? */
25869 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25870 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025871 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025873 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 sourcing_lnum);
25875 fcp->dbg_tick = debug_tick;
25876 }
25877
25878 return retval;
25879}
25880
Bram Moolenaar05159a02005-02-26 23:04:13 +000025881#if defined(FEAT_PROFILE) || defined(PROTO)
25882/*
25883 * Called when starting to read a function line.
25884 * "sourcing_lnum" must be correct!
25885 * When skipping lines it may not actually be executed, but we won't find out
25886 * until later and we need to store the time now.
25887 */
25888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025889func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025890{
25891 funccall_T *fcp = (funccall_T *)cookie;
25892 ufunc_T *fp = fcp->func;
25893
25894 if (fp->uf_profiling && sourcing_lnum >= 1
25895 && sourcing_lnum <= fp->uf_lines.ga_len)
25896 {
25897 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025898 /* Skip continuation lines. */
25899 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25900 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025901 fp->uf_tml_execed = FALSE;
25902 profile_start(&fp->uf_tml_start);
25903 profile_zero(&fp->uf_tml_children);
25904 profile_get_wait(&fp->uf_tml_wait);
25905 }
25906}
25907
25908/*
25909 * Called when actually executing a function line.
25910 */
25911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025912func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025913{
25914 funccall_T *fcp = (funccall_T *)cookie;
25915 ufunc_T *fp = fcp->func;
25916
25917 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25918 fp->uf_tml_execed = TRUE;
25919}
25920
25921/*
25922 * Called when done with a function line.
25923 */
25924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025925func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025926{
25927 funccall_T *fcp = (funccall_T *)cookie;
25928 ufunc_T *fp = fcp->func;
25929
25930 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25931 {
25932 if (fp->uf_tml_execed)
25933 {
25934 ++fp->uf_tml_count[fp->uf_tml_idx];
25935 profile_end(&fp->uf_tml_start);
25936 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025937 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025938 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25939 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025940 }
25941 fp->uf_tml_idx = -1;
25942 }
25943}
25944#endif
25945
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946/*
25947 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025948 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949 */
25950 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025951func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952{
Bram Moolenaar33570922005-01-25 22:26:29 +000025953 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954
25955 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25956 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025957 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958 || fcp->returned);
25959}
25960
25961/*
25962 * return TRUE if cookie indicates a function which "abort"s on errors.
25963 */
25964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025965func_has_abort(
25966 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025968 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969}
25970
25971#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25972typedef enum
25973{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025974 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25975 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25976 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977} var_flavour_T;
25978
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025979static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980
25981 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025982var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983{
25984 char_u *p = varname;
25985
25986 if (ASCII_ISUPPER(*p))
25987 {
25988 while (*(++p))
25989 if (ASCII_ISLOWER(*p))
25990 return VAR_FLAVOUR_SESSION;
25991 return VAR_FLAVOUR_VIMINFO;
25992 }
25993 else
25994 return VAR_FLAVOUR_DEFAULT;
25995}
25996#endif
25997
25998#if defined(FEAT_VIMINFO) || defined(PROTO)
25999/*
26000 * Restore global vars that start with a capital from the viminfo file
26001 */
26002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026003read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004{
26005 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026006 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026007 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026008 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009
26010 if (!writing && (find_viminfo_parameter('!') != NULL))
26011 {
26012 tab = vim_strchr(virp->vir_line + 1, '\t');
26013 if (tab != NULL)
26014 {
26015 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026016 switch (*tab)
26017 {
26018 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026019#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026020 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026021#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026022 case 'D': type = VAR_DICT; break;
26023 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026024 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026
26027 tab = vim_strchr(tab, '\t');
26028 if (tab != NULL)
26029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026030 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026031 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026032 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026034#ifdef FEAT_FLOAT
26035 else if (type == VAR_FLOAT)
26036 (void)string2float(tab + 1, &tv.vval.v_float);
26037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026039 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026040 if (type == VAR_DICT || type == VAR_LIST)
26041 {
26042 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26043
26044 if (etv == NULL)
26045 /* Failed to parse back the dict or list, use it as a
26046 * string. */
26047 tv.v_type = VAR_STRING;
26048 else
26049 {
26050 vim_free(tv.vval.v_string);
26051 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026052 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026053 }
26054 }
26055
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026056 /* when in a function use global variables */
26057 save_funccal = current_funccal;
26058 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026059 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026060 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026061
26062 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026063 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026064 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26065 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066 }
26067 }
26068 }
26069
26070 return viminfo_readline(virp);
26071}
26072
26073/*
26074 * Write global vars that start with a capital to the viminfo file
26075 */
26076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026077write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078{
Bram Moolenaar33570922005-01-25 22:26:29 +000026079 hashitem_T *hi;
26080 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026081 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026082 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026083 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026084 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026085 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086
26087 if (find_viminfo_parameter('!') == NULL)
26088 return;
26089
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026090 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026092 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026093 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026095 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026096 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026097 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026098 this_var = HI2DI(hi);
26099 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026100 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026101 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026102 {
26103 case VAR_STRING: s = "STR"; break;
26104 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026105 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026106 case VAR_DICT: s = "DIC"; break;
26107 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026108 case VAR_SPECIAL: s = "XPL"; break;
26109
26110 case VAR_UNKNOWN:
26111 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026112 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026113 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026114 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026115 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026116 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026117 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026118 if (p != NULL)
26119 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026120 vim_free(tofree);
26121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122 }
26123 }
26124}
26125#endif
26126
26127#if defined(FEAT_SESSION) || defined(PROTO)
26128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026129store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130{
Bram Moolenaar33570922005-01-25 22:26:29 +000026131 hashitem_T *hi;
26132 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026133 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 char_u *p, *t;
26135
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026136 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026137 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026139 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026141 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026142 this_var = HI2DI(hi);
26143 if ((this_var->di_tv.v_type == VAR_NUMBER
26144 || this_var->di_tv.v_type == VAR_STRING)
26145 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026146 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026147 /* Escape special characters with a backslash. Turn a LF and
26148 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026149 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026150 (char_u *)"\\\"\n\r");
26151 if (p == NULL) /* out of memory */
26152 break;
26153 for (t = p; *t != NUL; ++t)
26154 if (*t == '\n')
26155 *t = 'n';
26156 else if (*t == '\r')
26157 *t = 'r';
26158 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026159 this_var->di_key,
26160 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26161 : ' ',
26162 p,
26163 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26164 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026165 || put_eol(fd) == FAIL)
26166 {
26167 vim_free(p);
26168 return FAIL;
26169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 vim_free(p);
26171 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026172#ifdef FEAT_FLOAT
26173 else if (this_var->di_tv.v_type == VAR_FLOAT
26174 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26175 {
26176 float_T f = this_var->di_tv.vval.v_float;
26177 int sign = ' ';
26178
26179 if (f < 0)
26180 {
26181 f = -f;
26182 sign = '-';
26183 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026184 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026185 this_var->di_key, sign, f) < 0)
26186 || put_eol(fd) == FAIL)
26187 return FAIL;
26188 }
26189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 }
26191 }
26192 return OK;
26193}
26194#endif
26195
Bram Moolenaar661b1822005-07-28 22:36:45 +000026196/*
26197 * Display script name where an item was last set.
26198 * Should only be invoked when 'verbose' is non-zero.
26199 */
26200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026201last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026202{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026203 char_u *p;
26204
Bram Moolenaar661b1822005-07-28 22:36:45 +000026205 if (scriptID != 0)
26206 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026207 p = home_replace_save(NULL, get_scriptname(scriptID));
26208 if (p != NULL)
26209 {
26210 verbose_enter();
26211 MSG_PUTS(_("\n\tLast set from "));
26212 MSG_PUTS(p);
26213 vim_free(p);
26214 verbose_leave();
26215 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026216 }
26217}
26218
Bram Moolenaard812df62008-11-09 12:46:09 +000026219/*
26220 * List v:oldfiles in a nice way.
26221 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026223ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026224{
26225 list_T *l = vimvars[VV_OLDFILES].vv_list;
26226 listitem_T *li;
26227 int nr = 0;
26228
26229 if (l == NULL)
26230 msg((char_u *)_("No old files"));
26231 else
26232 {
26233 msg_start();
26234 msg_scroll = TRUE;
26235 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26236 {
26237 msg_outnum((long)++nr);
26238 MSG_PUTS(": ");
26239 msg_outtrans(get_tv_string(&li->li_tv));
26240 msg_putchar('\n');
26241 out_flush(); /* output one line at a time */
26242 ui_breakcheck();
26243 }
26244 /* Assume "got_int" was set to truncate the listing. */
26245 got_int = FALSE;
26246
26247#ifdef FEAT_BROWSE_CMD
26248 if (cmdmod.browse)
26249 {
26250 quit_more = FALSE;
26251 nr = prompt_for_number(FALSE);
26252 msg_starthere();
26253 if (nr > 0)
26254 {
26255 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26256 (long)nr);
26257
26258 if (p != NULL)
26259 {
26260 p = expand_env_save(p);
26261 eap->arg = p;
26262 eap->cmdidx = CMD_edit;
26263 cmdmod.browse = FALSE;
26264 do_exedit(eap, NULL);
26265 vim_free(p);
26266 }
26267 }
26268 }
26269#endif
26270 }
26271}
26272
Bram Moolenaar53744302015-07-17 17:38:22 +020026273/* reset v:option_new, v:option_old and v:option_type */
26274 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026275reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026276{
26277 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26278 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26279 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26280}
26281
26282
Bram Moolenaar071d4272004-06-13 20:20:40 +000026283#endif /* FEAT_EVAL */
26284
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026286#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287
26288#ifdef WIN3264
26289/*
26290 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26291 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026292static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26293static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26294static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295
26296/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026297 * Get the short path (8.3) for the filename in "fnamep".
26298 * Only works for a valid file name.
26299 * When the path gets longer "fnamep" is changed and the allocated buffer
26300 * is put in "bufp".
26301 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26302 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 */
26304 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026305get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026306{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026307 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308 char_u *newbuf;
26309
26310 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026311 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312 if (l > len - 1)
26313 {
26314 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026315 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026316 newbuf = vim_strnsave(*fnamep, l);
26317 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319
26320 vim_free(*bufp);
26321 *fnamep = *bufp = newbuf;
26322
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026323 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026324 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325 }
26326
26327 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026328 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026329}
26330
26331/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026332 * Get the short path (8.3) for the filename in "fname". The converted
26333 * path is returned in "bufp".
26334 *
26335 * Some of the directories specified in "fname" may not exist. This function
26336 * will shorten the existing directories at the beginning of the path and then
26337 * append the remaining non-existing path.
26338 *
26339 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026340 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026341 * bufp - Pointer to an allocated buffer for the filename.
26342 * fnamelen - Length of the filename pointed to by fname
26343 *
26344 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345 */
26346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026347shortpath_for_invalid_fname(
26348 char_u **fname,
26349 char_u **bufp,
26350 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026352 char_u *short_fname, *save_fname, *pbuf_unused;
26353 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026354 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026355 int old_len, len;
26356 int new_len, sfx_len;
26357 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358
26359 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026360 old_len = *fnamelen;
26361 save_fname = vim_strnsave(*fname, old_len);
26362 pbuf_unused = NULL;
26363 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026365 endp = save_fname + old_len - 1; /* Find the end of the copy */
26366 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026367
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026368 /*
26369 * Try shortening the supplied path till it succeeds by removing one
26370 * directory at a time from the tail of the path.
26371 */
26372 len = 0;
26373 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026374 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026375 /* go back one path-separator */
26376 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26377 --endp;
26378 if (endp <= save_fname)
26379 break; /* processed the complete path */
26380
26381 /*
26382 * Replace the path separator with a NUL and try to shorten the
26383 * resulting path.
26384 */
26385 ch = *endp;
26386 *endp = 0;
26387 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026388 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026389 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26390 {
26391 retval = FAIL;
26392 goto theend;
26393 }
26394 *endp = ch; /* preserve the string */
26395
26396 if (len > 0)
26397 break; /* successfully shortened the path */
26398
26399 /* failed to shorten the path. Skip the path separator */
26400 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401 }
26402
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026403 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026405 /*
26406 * Succeeded in shortening the path. Now concatenate the shortened
26407 * path with the remaining path at the tail.
26408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026410 /* Compute the length of the new path. */
26411 sfx_len = (int)(save_endp - endp) + 1;
26412 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026414 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026416 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026418 /* There is not enough space in the currently allocated string,
26419 * copy it to a buffer big enough. */
26420 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 {
26423 retval = FAIL;
26424 goto theend;
26425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 }
26427 else
26428 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026429 /* Transfer short_fname to the main buffer (it's big enough),
26430 * unless get_short_pathname() did its work in-place. */
26431 *fname = *bufp = save_fname;
26432 if (short_fname != save_fname)
26433 vim_strncpy(save_fname, short_fname, len);
26434 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026436
26437 /* concat the not-shortened part of the path */
26438 vim_strncpy(*fname + len, endp, sfx_len);
26439 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026441
26442theend:
26443 vim_free(pbuf_unused);
26444 vim_free(save_fname);
26445
26446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026447}
26448
26449/*
26450 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026451 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 */
26453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026454shortpath_for_partial(
26455 char_u **fnamep,
26456 char_u **bufp,
26457 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026458{
26459 int sepcount, len, tflen;
26460 char_u *p;
26461 char_u *pbuf, *tfname;
26462 int hasTilde;
26463
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026464 /* Count up the path separators from the RHS.. so we know which part
26465 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026467 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468 if (vim_ispathsep(*p))
26469 ++sepcount;
26470
26471 /* Need full path first (use expand_env() to remove a "~/") */
26472 hasTilde = (**fnamep == '~');
26473 if (hasTilde)
26474 pbuf = tfname = expand_env_save(*fnamep);
26475 else
26476 pbuf = tfname = FullName_save(*fnamep, FALSE);
26477
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026478 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026482
26483 if (len == 0)
26484 {
26485 /* Don't have a valid filename, so shorten the rest of the
26486 * path if we can. This CAN give us invalid 8.3 filenames, but
26487 * there's not a lot of point in guessing what it might be.
26488 */
26489 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026490 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026492 }
26493
26494 /* Count the paths backward to find the beginning of the desired string. */
26495 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026496 {
26497#ifdef FEAT_MBYTE
26498 if (has_mbyte)
26499 p -= mb_head_off(tfname, p);
26500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501 if (vim_ispathsep(*p))
26502 {
26503 if (sepcount == 0 || (hasTilde && sepcount == 1))
26504 break;
26505 else
26506 sepcount --;
26507 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509 if (hasTilde)
26510 {
26511 --p;
26512 if (p >= tfname)
26513 *p = '~';
26514 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026515 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516 }
26517 else
26518 ++p;
26519
26520 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26521 vim_free(*bufp);
26522 *fnamelen = (int)STRLEN(p);
26523 *bufp = pbuf;
26524 *fnamep = p;
26525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026526 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026527}
26528#endif /* WIN3264 */
26529
26530/*
26531 * Adjust a filename, according to a string of modifiers.
26532 * *fnamep must be NUL terminated when called. When returning, the length is
26533 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026534 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026535 * When there is an error, *fnamep is set to NULL.
26536 */
26537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026538modify_fname(
26539 char_u *src, /* string with modifiers */
26540 int *usedlen, /* characters after src that are used */
26541 char_u **fnamep, /* file name so far */
26542 char_u **bufp, /* buffer for allocated file name or NULL */
26543 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544{
26545 int valid = 0;
26546 char_u *tail;
26547 char_u *s, *p, *pbuf;
26548 char_u dirname[MAXPATHL];
26549 int c;
26550 int has_fullname = 0;
26551#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026552 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553 int has_shortname = 0;
26554#endif
26555
26556repeat:
26557 /* ":p" - full path/file_name */
26558 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26559 {
26560 has_fullname = 1;
26561
26562 valid |= VALID_PATH;
26563 *usedlen += 2;
26564
26565 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26566 if ((*fnamep)[0] == '~'
26567#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26568 && ((*fnamep)[1] == '/'
26569# ifdef BACKSLASH_IN_FILENAME
26570 || (*fnamep)[1] == '\\'
26571# endif
26572 || (*fnamep)[1] == NUL)
26573
26574#endif
26575 )
26576 {
26577 *fnamep = expand_env_save(*fnamep);
26578 vim_free(*bufp); /* free any allocated file name */
26579 *bufp = *fnamep;
26580 if (*fnamep == NULL)
26581 return -1;
26582 }
26583
26584 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026585 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 {
26587 if (vim_ispathsep(*p)
26588 && p[1] == '.'
26589 && (p[2] == NUL
26590 || vim_ispathsep(p[2])
26591 || (p[2] == '.'
26592 && (p[3] == NUL || vim_ispathsep(p[3])))))
26593 break;
26594 }
26595
26596 /* FullName_save() is slow, don't use it when not needed. */
26597 if (*p != NUL || !vim_isAbsName(*fnamep))
26598 {
26599 *fnamep = FullName_save(*fnamep, *p != NUL);
26600 vim_free(*bufp); /* free any allocated file name */
26601 *bufp = *fnamep;
26602 if (*fnamep == NULL)
26603 return -1;
26604 }
26605
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026606#ifdef WIN3264
26607# if _WIN32_WINNT >= 0x0500
26608 if (vim_strchr(*fnamep, '~') != NULL)
26609 {
26610 /* Expand 8.3 filename to full path. Needed to make sure the same
26611 * file does not have two different names.
26612 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26613 p = alloc(_MAX_PATH + 1);
26614 if (p != NULL)
26615 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026616 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026617 {
26618 vim_free(*bufp);
26619 *bufp = *fnamep = p;
26620 }
26621 else
26622 vim_free(p);
26623 }
26624 }
26625# endif
26626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026627 /* Append a path separator to a directory. */
26628 if (mch_isdir(*fnamep))
26629 {
26630 /* Make room for one or two extra characters. */
26631 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26632 vim_free(*bufp); /* free any allocated file name */
26633 *bufp = *fnamep;
26634 if (*fnamep == NULL)
26635 return -1;
26636 add_pathsep(*fnamep);
26637 }
26638 }
26639
26640 /* ":." - path relative to the current directory */
26641 /* ":~" - path relative to the home directory */
26642 /* ":8" - shortname path - postponed till after */
26643 while (src[*usedlen] == ':'
26644 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26645 {
26646 *usedlen += 2;
26647 if (c == '8')
26648 {
26649#ifdef WIN3264
26650 has_shortname = 1; /* Postpone this. */
26651#endif
26652 continue;
26653 }
26654 pbuf = NULL;
26655 /* Need full path first (use expand_env() to remove a "~/") */
26656 if (!has_fullname)
26657 {
26658 if (c == '.' && **fnamep == '~')
26659 p = pbuf = expand_env_save(*fnamep);
26660 else
26661 p = pbuf = FullName_save(*fnamep, FALSE);
26662 }
26663 else
26664 p = *fnamep;
26665
26666 has_fullname = 0;
26667
26668 if (p != NULL)
26669 {
26670 if (c == '.')
26671 {
26672 mch_dirname(dirname, MAXPATHL);
26673 s = shorten_fname(p, dirname);
26674 if (s != NULL)
26675 {
26676 *fnamep = s;
26677 if (pbuf != NULL)
26678 {
26679 vim_free(*bufp); /* free any allocated file name */
26680 *bufp = pbuf;
26681 pbuf = NULL;
26682 }
26683 }
26684 }
26685 else
26686 {
26687 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26688 /* Only replace it when it starts with '~' */
26689 if (*dirname == '~')
26690 {
26691 s = vim_strsave(dirname);
26692 if (s != NULL)
26693 {
26694 *fnamep = s;
26695 vim_free(*bufp);
26696 *bufp = s;
26697 }
26698 }
26699 }
26700 vim_free(pbuf);
26701 }
26702 }
26703
26704 tail = gettail(*fnamep);
26705 *fnamelen = (int)STRLEN(*fnamep);
26706
26707 /* ":h" - head, remove "/file_name", can be repeated */
26708 /* Don't remove the first "/" or "c:\" */
26709 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26710 {
26711 valid |= VALID_HEAD;
26712 *usedlen += 2;
26713 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026714 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026715 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026716 *fnamelen = (int)(tail - *fnamep);
26717#ifdef VMS
26718 if (*fnamelen > 0)
26719 *fnamelen += 1; /* the path separator is part of the path */
26720#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026721 if (*fnamelen == 0)
26722 {
26723 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26724 p = vim_strsave((char_u *)".");
26725 if (p == NULL)
26726 return -1;
26727 vim_free(*bufp);
26728 *bufp = *fnamep = tail = p;
26729 *fnamelen = 1;
26730 }
26731 else
26732 {
26733 while (tail > s && !after_pathsep(s, tail))
26734 mb_ptr_back(*fnamep, tail);
26735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026736 }
26737
26738 /* ":8" - shortname */
26739 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26740 {
26741 *usedlen += 2;
26742#ifdef WIN3264
26743 has_shortname = 1;
26744#endif
26745 }
26746
26747#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026748 /*
26749 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750 */
26751 if (has_shortname)
26752 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026753 /* Copy the string if it is shortened by :h and when it wasn't copied
26754 * yet, because we are going to change it in place. Avoids changing
26755 * the buffer name for "%:8". */
26756 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026757 {
26758 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026759 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026760 return -1;
26761 vim_free(*bufp);
26762 *bufp = *fnamep = p;
26763 }
26764
26765 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026766 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 if (!has_fullname && !vim_isAbsName(*fnamep))
26768 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026769 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026770 return -1;
26771 }
26772 else
26773 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026774 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775
Bram Moolenaardc935552011-08-17 15:23:23 +020026776 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026777 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026778 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026779 return -1;
26780
26781 if (l == 0)
26782 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026783 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026784 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026785 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026786 return -1;
26787 }
26788 *fnamelen = l;
26789 }
26790 }
26791#endif /* WIN3264 */
26792
26793 /* ":t" - tail, just the basename */
26794 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26795 {
26796 *usedlen += 2;
26797 *fnamelen -= (int)(tail - *fnamep);
26798 *fnamep = tail;
26799 }
26800
26801 /* ":e" - extension, can be repeated */
26802 /* ":r" - root, without extension, can be repeated */
26803 while (src[*usedlen] == ':'
26804 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26805 {
26806 /* find a '.' in the tail:
26807 * - for second :e: before the current fname
26808 * - otherwise: The last '.'
26809 */
26810 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26811 s = *fnamep - 2;
26812 else
26813 s = *fnamep + *fnamelen - 1;
26814 for ( ; s > tail; --s)
26815 if (s[0] == '.')
26816 break;
26817 if (src[*usedlen + 1] == 'e') /* :e */
26818 {
26819 if (s > tail)
26820 {
26821 *fnamelen += (int)(*fnamep - (s + 1));
26822 *fnamep = s + 1;
26823#ifdef VMS
26824 /* cut version from the extension */
26825 s = *fnamep + *fnamelen - 1;
26826 for ( ; s > *fnamep; --s)
26827 if (s[0] == ';')
26828 break;
26829 if (s > *fnamep)
26830 *fnamelen = s - *fnamep;
26831#endif
26832 }
26833 else if (*fnamep <= tail)
26834 *fnamelen = 0;
26835 }
26836 else /* :r */
26837 {
26838 if (s > tail) /* remove one extension */
26839 *fnamelen = (int)(s - *fnamep);
26840 }
26841 *usedlen += 2;
26842 }
26843
26844 /* ":s?pat?foo?" - substitute */
26845 /* ":gs?pat?foo?" - global substitute */
26846 if (src[*usedlen] == ':'
26847 && (src[*usedlen + 1] == 's'
26848 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26849 {
26850 char_u *str;
26851 char_u *pat;
26852 char_u *sub;
26853 int sep;
26854 char_u *flags;
26855 int didit = FALSE;
26856
26857 flags = (char_u *)"";
26858 s = src + *usedlen + 2;
26859 if (src[*usedlen + 1] == 'g')
26860 {
26861 flags = (char_u *)"g";
26862 ++s;
26863 }
26864
26865 sep = *s++;
26866 if (sep)
26867 {
26868 /* find end of pattern */
26869 p = vim_strchr(s, sep);
26870 if (p != NULL)
26871 {
26872 pat = vim_strnsave(s, (int)(p - s));
26873 if (pat != NULL)
26874 {
26875 s = p + 1;
26876 /* find end of substitution */
26877 p = vim_strchr(s, sep);
26878 if (p != NULL)
26879 {
26880 sub = vim_strnsave(s, (int)(p - s));
26881 str = vim_strnsave(*fnamep, *fnamelen);
26882 if (sub != NULL && str != NULL)
26883 {
26884 *usedlen = (int)(p + 1 - src);
26885 s = do_string_sub(str, pat, sub, flags);
26886 if (s != NULL)
26887 {
26888 *fnamep = s;
26889 *fnamelen = (int)STRLEN(s);
26890 vim_free(*bufp);
26891 *bufp = s;
26892 didit = TRUE;
26893 }
26894 }
26895 vim_free(sub);
26896 vim_free(str);
26897 }
26898 vim_free(pat);
26899 }
26900 }
26901 /* after using ":s", repeat all the modifiers */
26902 if (didit)
26903 goto repeat;
26904 }
26905 }
26906
Bram Moolenaar26df0922014-02-23 23:39:13 +010026907 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26908 {
26909 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26910 if (p == NULL)
26911 return -1;
26912 vim_free(*bufp);
26913 *bufp = *fnamep = p;
26914 *fnamelen = (int)STRLEN(p);
26915 *usedlen += 2;
26916 }
26917
Bram Moolenaar071d4272004-06-13 20:20:40 +000026918 return valid;
26919}
26920
26921/*
26922 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26923 * "flags" can be "g" to do a global substitute.
26924 * Returns an allocated string, NULL for error.
26925 */
26926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026927do_string_sub(
26928 char_u *str,
26929 char_u *pat,
26930 char_u *sub,
26931 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026932{
26933 int sublen;
26934 regmatch_T regmatch;
26935 int i;
26936 int do_all;
26937 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026938 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939 garray_T ga;
26940 char_u *ret;
26941 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026942 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026943
26944 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26945 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026946 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026947
26948 ga_init2(&ga, 1, 200);
26949
26950 do_all = (flags[0] == 'g');
26951
26952 regmatch.rm_ic = p_ic;
26953 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26954 if (regmatch.regprog != NULL)
26955 {
26956 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026957 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026958 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26959 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026960 /* Skip empty match except for first match. */
26961 if (regmatch.startp[0] == regmatch.endp[0])
26962 {
26963 if (zero_width == regmatch.startp[0])
26964 {
26965 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026966 i = MB_PTR2LEN(tail);
26967 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26968 (size_t)i);
26969 ga.ga_len += i;
26970 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026971 continue;
26972 }
26973 zero_width = regmatch.startp[0];
26974 }
26975
Bram Moolenaar071d4272004-06-13 20:20:40 +000026976 /*
26977 * Get some space for a temporary buffer to do the substitution
26978 * into. It will contain:
26979 * - The text up to where the match is.
26980 * - The substituted text.
26981 * - The text after the match.
26982 */
26983 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026984 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026985 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26986 {
26987 ga_clear(&ga);
26988 break;
26989 }
26990
26991 /* copy the text up to where the match is */
26992 i = (int)(regmatch.startp[0] - tail);
26993 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26994 /* add the substituted text */
26995 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26996 + ga.ga_len + i, TRUE, TRUE, FALSE);
26997 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026998 tail = regmatch.endp[0];
26999 if (*tail == NUL)
27000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001 if (!do_all)
27002 break;
27003 }
27004
27005 if (ga.ga_data != NULL)
27006 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27007
Bram Moolenaar473de612013-06-08 18:19:48 +020027008 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027009 }
27010
27011 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27012 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027013 if (p_cpo == empty_option)
27014 p_cpo = save_cpo;
27015 else
27016 /* Darn, evaluating {sub} expression changed the value. */
27017 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027018
27019 return ret;
27020}
27021
27022#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */