blob: f65c9a0b1bd03005d071a434f5a3d981778948b0 [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);
738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_tolower(typval_T *argvars, typval_T *rettv);
800static void f_toupper(typval_T *argvars, typval_T *rettv);
801static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_type(typval_T *argvars, typval_T *rettv);
806static void f_undofile(typval_T *argvars, typval_T *rettv);
807static void f_undotree(typval_T *argvars, typval_T *rettv);
808static void f_uniq(typval_T *argvars, typval_T *rettv);
809static void f_values(typval_T *argvars, typval_T *rettv);
810static void f_virtcol(typval_T *argvars, typval_T *rettv);
811static void f_visualmode(typval_T *argvars, typval_T *rettv);
812static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
813static void f_winbufnr(typval_T *argvars, typval_T *rettv);
814static void f_wincol(typval_T *argvars, typval_T *rettv);
815static void f_winheight(typval_T *argvars, typval_T *rettv);
816static void f_winline(typval_T *argvars, typval_T *rettv);
817static void f_winnr(typval_T *argvars, typval_T *rettv);
818static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
819static void f_winrestview(typval_T *argvars, typval_T *rettv);
820static void f_winsaveview(typval_T *argvars, typval_T *rettv);
821static void f_winwidth(typval_T *argvars, typval_T *rettv);
822static void f_writefile(typval_T *argvars, typval_T *rettv);
823static void f_wordcount(typval_T *argvars, typval_T *rettv);
824static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000825
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
827static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
828static int get_env_len(char_u **arg);
829static int get_id_len(char_u **arg);
830static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
831static 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 +0000832#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
833#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
834 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
836static int eval_isnamec(int c);
837static int eval_isnamec1(int c);
838static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
839static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static typval_T *alloc_string_tv(char_u *string);
841static void init_tv(typval_T *varp);
842static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100843#ifdef FEAT_FLOAT
844static float_T get_tv_float(typval_T *varp);
845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static linenr_T get_tv_lnum(typval_T *argvars);
847static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
848static char_u *get_tv_string(typval_T *varp);
849static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
850static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
851static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
852static hashtab_T *find_var_ht(char_u *name, char_u **varname);
853static funccall_T *get_funccal(void);
854static void vars_clear_ext(hashtab_T *ht, int free_val);
855static void delete_var(hashtab_T *ht, hashitem_T *hi);
856static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
857static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
858static void set_var(char_u *name, typval_T *varp, int copy);
859static int var_check_ro(int flags, char_u *name, int use_gettext);
860static int var_check_fixed(int flags, char_u *name, int use_gettext);
861static int var_check_func_name(char_u *name, int new_var);
862static int valid_varname(char_u *varname);
863static int tv_check_lock(int lock, char_u *name, int use_gettext);
864static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
865static char_u *find_option_end(char_u **arg, int *opt_flags);
866static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
867static int eval_fname_script(char_u *p);
868static int eval_fname_sid(char_u *p);
869static void list_func_head(ufunc_T *fp, int indent);
870static ufunc_T *find_func(char_u *name);
871static int function_exists(char_u *name);
872static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static void func_do_profile(ufunc_T *fp);
875static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
876static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int script_autoload(char_u *name, int reload);
889static char_u *autoload_name(char_u *name);
890static void cat_func_name(char_u *buf, ufunc_T *fp);
891static void func_free(ufunc_T *fp);
892static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
893static int can_free_funccal(funccall_T *fc, int copyID) ;
894static void free_funccal(funccall_T *fc, int free_val);
895static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
896static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
897static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
898static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
900static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
901static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int write_list(FILE *fd, list_T *list, int binary);
903static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000904
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int compare_func_name(const void *s1, const void *s2);
908static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909#endif
910
Bram Moolenaar33570922005-01-25 22:26:29 +0000911/*
912 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000913 */
914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000916{
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 int i;
918 struct vimvar *p;
919
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
921 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200922 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000923 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
926 for (i = 0; i < VV_LEN; ++i)
927 {
928 p = &vimvars[i];
929 STRCPY(p->vv_di.di_key, p->vv_name);
930 if (p->vv_flags & VV_RO)
931 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
932 else if (p->vv_flags & VV_RO_SBX)
933 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
934 else
935 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000936
937 /* add to v: scope dict, unless the value is not always available */
938 if (p->vv_type != VAR_UNKNOWN)
939 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 /* add to compat scope dict */
942 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100944 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
945
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100947 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200948 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100949 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100950
951 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
952 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
953 set_vim_var_nr(VV_NONE, VVAL_NONE);
954 set_vim_var_nr(VV_NULL, VVAL_NULL);
955
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200956 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200957
958#ifdef EBCDIC
959 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100960 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961 */
962 sortFunctions();
963#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000964}
965
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966#if defined(EXITFREE) || defined(PROTO)
967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100968eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969{
970 int i;
971 struct vimvar *p;
972
973 for (i = 0; i < VV_LEN; ++i)
974 {
975 p = &vimvars[i];
976 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000978 vim_free(p->vv_str);
979 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000980 }
981 else if (p->vv_di.di_tv.v_type == VAR_LIST)
982 {
983 list_unref(p->vv_list);
984 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 }
987 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000988 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 hash_clear(&compat_hashtab);
990
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200993 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100994# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995
996 /* global variables */
997 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000999 /* autoloaded script names */
1000 ga_clear_strings(&ga_loaded);
1001
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 /* Script-local variables. First clear all the variables and in a second
1003 * loop free the scriptvar_T, because a variable in one script might hold
1004 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 ga_clear(&ga_scripts);
1010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001011 /* unreferenced lists and dicts */
1012 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001013
1014 /* functions */
1015 free_all_functions();
1016 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017}
1018#endif
1019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Return the name of the executed function.
1022 */
1023 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the next breakpoint line for a funccall cookie.
1031 */
1032 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the debug tick for a funccall cookie.
1040 */
1041 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the nesting level for a funccall cookie.
1049 */
1050 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001057funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001059/* pointer to list of previously used funccal, still around because some
1060 * item in it is still being used. */
1061funccall_T *previous_funccal = NULL;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063/*
1064 * Return TRUE when a function was ended by a ":return" command.
1065 */
1066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 return current_funccal->returned;
1070}
1071
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Set an internal variable to a string value. Creates the variable if it does
1075 * not already exist.
1076 */
1077 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 val = vim_strsave(value);
1084 if (val != NULL)
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093}
1094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097static char_u *redir_endp = NULL;
1098static char_u *redir_varname = NULL;
1099
1100/*
1101 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 * Returns OK if successfully completed the setup. FAIL otherwise.
1104 */
1105 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
1108 int save_emsg;
1109 int err;
1110 typval_T tv;
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 {
1115 EMSG(_(e_invarg));
1116 return FAIL;
1117 }
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 redir_varname = vim_strsave(name);
1121 if (redir_varname == NULL)
1122 return FAIL;
1123
1124 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1125 if (redir_lval == NULL)
1126 {
1127 var_redir_stop();
1128 return FAIL;
1129 }
1130
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 /* The output is stored in growarray "redir_ga" until redirection ends. */
1132 ga_init2(&redir_ga, (int)sizeof(char), 500);
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001135 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1138 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001139 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp != NULL && *redir_endp != NUL)
1141 /* Trailing characters are present after the variable name */
1142 EMSG(_(e_trailing));
1143 else
1144 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 var_redir_stop();
1147 return FAIL;
1148 }
1149
1150 /* check if we can write to the variable: set it to or append an empty
1151 * string */
1152 save_emsg = did_emsg;
1153 did_emsg = FALSE;
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = (char_u *)"";
1156 if (append)
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1158 else
1159 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001162 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (err)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 * Append "value[value_len]" to the variable set by var_redir_start().
1175 * The actual appending is postponed until redirection ends, because the value
1176 * appended may in fact be the string we write to, changing it may cause freed
1177 * memory to be used:
1178 * :redir => foo
1179 * :let foo
1180 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186
1187 if (redir_lval == NULL)
1188 return;
1189
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 {
1197 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 }
1200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202}
1203
1204/*
1205 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 typval_T tv;
1212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 if (redir_lval != NULL)
1214 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 /* If there was no error: assign the text to the variable. */
1216 if (redir_endp != NULL)
1217 {
1218 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1219 tv.v_type = VAR_STRING;
1220 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 /* Call get_lval() again, if it's inside a Dict or List it may
1222 * have changed. */
1223 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001224 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1226 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1227 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* free the collected output */
1231 vim_free(redir_ga.ga_data);
1232 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 vim_free(redir_lval);
1235 redir_lval = NULL;
1236 }
1237 vim_free(redir_varname);
1238 redir_varname = NULL;
1239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# if defined(FEAT_MBYTE) || defined(PROTO)
1242 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001243eval_charconvert(
1244 char_u *enc_from,
1245 char_u *enc_to,
1246 char_u *fname_from,
1247 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1252 set_vim_var_string(VV_CC_TO, enc_to, -1);
1253 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1254 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1255 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_CC_FROM, NULL, -1);
1258 set_vim_var_string(VV_CC_TO, NULL, -1);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261
1262 if (err)
1263 return FAIL;
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_FNAME_IN, fname, -1);
1275 set_vim_var_string(VV_CMDARG, args, -1);
1276 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1277 err = TRUE;
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_CMDARG, NULL, -1);
1280
1281 if (err)
1282 {
1283 mch_remove(fname);
1284 return FAIL;
1285 }
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_DIFF) || defined(PROTO)
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_diff(
1293 char_u *origfile,
1294 char_u *newfile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307
1308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_patch(
1310 char_u *origfile,
1311 char_u *difffile,
1312 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int err;
1315
1316 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1318 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1319 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1320 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1322 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1323}
1324# endif
1325
1326/*
1327 * Top level evaluation function, returning a boolean.
1328 * Sets "error" to TRUE if there was an error.
1329 * Return TRUE or FALSE.
1330 */
1331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_to_bool(
1333 char_u *arg,
1334 int *error,
1335 char_u **nextcmd,
1336 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int retval = FALSE;
1340
1341 if (skip)
1342 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 {
1347 *error = FALSE;
1348 if (!skip)
1349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 }
1354 if (skip)
1355 --emsg_skip;
1356
1357 return retval;
1358}
1359
1360/*
1361 * Top level evaluation function, returning a string. If "skip" is TRUE,
1362 * only parsing to "nextcmd" is done, without reporting errors. Return
1363 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1364 */
1365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366eval_to_string_skip(
1367 char_u *arg,
1368 char_u **nextcmd,
1369 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *retval;
1373
1374 if (skip)
1375 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 retval = vim_strsave(get_tv_string(&tv));
1381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 if (skip)
1384 --emsg_skip;
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 * Skip over an expression at "*pp".
1391 * Return FAIL for an error, OK otherwise.
1392 */
1393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001394skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397
1398 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400}
1401
1402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 * When "convert" is TRUE convert a List into a sequence of lines and convert
1405 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Return pointer to allocated memory, or NULL for failure.
1407 */
1408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_to_string(
1410 char_u *arg,
1411 char_u **nextcmd,
1412 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 retval = NULL;
1423 else
1424 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001426 {
1427 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001430 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001431 if (tv.vval.v_list->lv_len > 0)
1432 ga_append(&ga, NL);
1433 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 ga_append(&ga, NUL);
1435 retval = (char_u *)ga.ga_data;
1436 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001437#ifdef FEAT_FLOAT
1438 else if (convert && tv.v_type == VAR_FLOAT)
1439 {
1440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1441 retval = vim_strsave(numbuf);
1442 }
1443#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444 else
1445 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 return retval;
1450}
1451
1452/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 * Call eval_to_string() without using current local variables and using
1454 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string_safe(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u *retval;
1463 void *save_funccalp;
1464
1465 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 ++sandbox;
1468 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 --sandbox;
1472 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 restore_funccal(save_funccalp);
1474 return retval;
1475}
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * Top level evaluation function, returning a number.
1479 * Evaluates "expr" silently.
1480 * Returns -1 for an error.
1481 */
1482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001487 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 ++emsg_off;
1490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 retval = -1;
1493 else
1494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 --emsg_off;
1499
1500 return retval;
1501}
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503/*
1504 * Prepare v: variable "idx" to be used.
1505 * Save the current typeval in "save_tv".
1506 * When not used yet add the variable to the v: hashtable.
1507 */
1508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510{
1511 *save_tv = vimvars[idx].vv_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1514}
1515
1516/*
1517 * Restore v: variable "idx" to typeval "save_tv".
1518 * When no longer defined, remove the variable from the v: hashtable.
1519 */
1520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522{
1523 hashitem_T *hi;
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525 vimvars[idx].vv_tv = *save_tv;
1526 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1527 {
1528 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1529 if (HASHITEM_EMPTY(hi))
1530 EMSG2(_(e_intern2), "restore_vimvar()");
1531 else
1532 hash_remove(&vimvarht, hi);
1533 }
1534}
1535
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001536#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537/*
1538 * Evaluate an expression to a list with suggestions.
1539 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001540 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 */
1542 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544{
1545 typval_T save_val;
1546 typval_T rettv;
1547 list_T *list = NULL;
1548 char_u *p = skipwhite(expr);
1549
1550 /* Set "v:val" to the bad word. */
1551 prepare_vimvar(VV_VAL, &save_val);
1552 vimvars[VV_VAL].vv_type = VAR_STRING;
1553 vimvars[VV_VAL].vv_str = badword;
1554 if (p_verbose == 0)
1555 ++emsg_off;
1556
1557 if (eval1(&p, &rettv, TRUE) == OK)
1558 {
1559 if (rettv.v_type != VAR_LIST)
1560 clear_tv(&rettv);
1561 else
1562 list = rettv.vval.v_list;
1563 }
1564
1565 if (p_verbose == 0)
1566 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 restore_vimvar(VV_VAL, &save_val);
1568
1569 return list;
1570}
1571
1572/*
1573 * "list" is supposed to contain two items: a word and a number. Return the
1574 * word in "pp" and the number as the return value.
1575 * Return -1 if anything isn't right.
1576 * Used to get the good word and score from the eval_spell_expr() result.
1577 */
1578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 listitem_T *li;
1582
1583 li = list->lv_first;
1584 if (li == NULL)
1585 return -1;
1586 *pp = get_tv_string(&li->li_tv);
1587
1588 li = li->li_next;
1589 if (li == NULL)
1590 return -1;
1591 return get_tv_number(&li->li_tv);
1592}
1593#endif
1594
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 * Top level evaluation function.
1597 * Returns an allocated typval_T with the result.
1598 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599 */
1600 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602{
1603 typval_T *tv;
1604
1605 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 {
1608 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 }
1611
1612 return tv;
1613}
1614
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 * Uses argv[argc] for the function arguments. Only Number and String
1619 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623call_vim_function(
1624 char_u *func,
1625 int argc,
1626 char_u **argv,
1627 int safe, /* use the sandbox */
1628 int str_arg_only, /* all arguments are strings */
1629 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 long n;
1633 int len;
1634 int i;
1635 int doesrange;
1636 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001639 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 /* Pass a NULL or empty argument as an empty string */
1646 if (argv[i] == NULL || *argv[i] == NUL)
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 continue;
1651 }
1652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 if (str_arg_only)
1654 len = 0;
1655 else
1656 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001657 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (len != 0 && len == (int)STRLEN(argv[i]))
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_NUMBER;
1661 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_STRING;
1666 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669
1670 if (safe)
1671 {
1672 save_funccalp = save_funccal();
1673 ++sandbox;
1674 }
1675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1677 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (safe)
1681 {
1682 --sandbox;
1683 restore_funccal(save_funccalp);
1684 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 vim_free(argvars);
1686
1687 if (ret == FAIL)
1688 clear_tv(rettv);
1689
1690 return ret;
1691}
1692
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001693/*
1694 * Call vimL function "func" and return the result as a number.
1695 * Returns -1 when calling the function fails.
1696 * Uses argv[argc] for the function arguments.
1697 */
1698 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699call_func_retnr(
1700 char_u *func,
1701 int argc,
1702 char_u **argv,
1703 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001704{
1705 typval_T rettv;
1706 long retval;
1707
1708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1710 return -1;
1711
1712 retval = get_tv_number_chk(&rettv, NULL);
1713 clear_tv(&rettv);
1714 return retval;
1715}
1716
1717#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1718 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722 * Call vimL function "func" and return the result as a string.
1723 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
1725 */
1726 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727call_func_retstr(
1728 char_u *func,
1729 int argc,
1730 char_u **argv,
1731 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732{
1733 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001734 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return retval;
1743}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001749 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752call_func_retlist(
1753 char_u *func,
1754 int argc,
1755 char_u **argv,
1756 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757{
1758 typval_T rettv;
1759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 if (rettv.v_type != VAR_LIST)
1765 {
1766 clear_tv(&rettv);
1767 return NULL;
1768 }
1769
1770 return rettv.vval.v_list;
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774/*
1775 * Save the current function call pointer, and set it to NULL.
1776 * Used when executing autocommands and for ":source".
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 current_funccal = NULL;
1784 return (void *)fc;
1785}
1786
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790 funccall_T *fc = (funccall_T *)vfc;
1791
1792 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795#if defined(FEAT_PROFILE) || defined(PROTO)
1796/*
1797 * Prepare profiling for entering a child or something else that is not
1798 * counted for the script/function itself.
1799 * Should always be called in pair with prof_child_exit().
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802prof_child_enter(
1803 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804{
1805 funccall_T *fc = current_funccal;
1806
1807 if (fc != NULL && fc->func->uf_profiling)
1808 profile_start(&fc->prof_child);
1809 script_prof_save(tm);
1810}
1811
1812/*
1813 * Take care of time spent in a child.
1814 * Should always be called after prof_child_enter().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_exit(
1818 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 {
1824 profile_end(&fc->prof_child);
1825 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1826 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1827 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1828 }
1829 script_prof_restore(tm);
1830}
1831#endif
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835/*
1836 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1837 * it in "*cp". Doesn't give error messages.
1838 */
1839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int retval;
1844 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001845 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1846 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 ++sandbox;
1851 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (tv.v_type == VAR_NUMBER)
1859 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001860 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a string, check if there is a non-digit before
1865 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!VIM_ISDIGIT(*s) && *s != '-')
1868 *cp = *s++;
1869 retval = atol((char *)s);
1870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001874 if (use_sandbox)
1875 --sandbox;
1876 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return retval;
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let" list all variable values
1884 * ":let var1 var2" list variable values
1885 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 * ":let var += expr" assignment command.
1887 * ":let var -= expr" assignment command.
1888 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 int var_count = 0;
1899 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 argend = skip_var_list(arg, &var_count, &semicolon);
1905 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001907 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1908 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001909 expr = skipwhite(argend);
1910 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1911 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001913 /*
1914 * ":let" without "=": list variables
1915 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 if (*arg == '[')
1917 EMSG(_(e_invarg));
1918 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_glob_vars(&first);
1925 list_buf_vars(&first);
1926 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001929#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_script_vars(&first);
1931 list_func_vars(&first);
1932 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 eap->nextcmd = check_nextcmd(arg);
1935 }
1936 else
1937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op[0] = '=';
1939 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1943 op[0] = *expr; /* +=, -= or .= */
1944 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 else
1947 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 {
1954 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 --emsg_skip;
1957 }
1958 else if (i != FAIL)
1959 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965}
1966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967/*
1968 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1969 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 * When "nextchars" is not NULL it points to a string with characters that
1971 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1972 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 * Returns OK or FAIL;
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976ex_let_vars(
1977 char_u *arg_start,
1978 typval_T *tv,
1979 int copy, /* copy values from "tv", don't move */
1980 int semicolon, /* from skip_var_list() */
1981 int var_count, /* from skip_var_list() */
1982 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983{
1984 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 listitem_T *item;
1988 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989
1990 if (*arg != '[')
1991 {
1992 /*
1993 * ":let var = expr" or ":for var in list"
1994 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 return OK;
1998 }
1999
2000 /*
2001 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2002 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002003 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 EMSG(_(e_listreq));
2006 return FAIL;
2007 }
2008
2009 i = list_len(l);
2010 if (semicolon == 0 && var_count < i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015 if (var_count - semicolon > i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020
2021 item = l->lv_first;
2022 while (*arg != ']')
2023 {
2024 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 item = item->li_next;
2027 if (arg == NULL)
2028 return FAIL;
2029
2030 arg = skipwhite(arg);
2031 if (*arg == ';')
2032 {
2033 /* Put the rest of the list (may be empty) in the var after ';'.
2034 * Create a new list for this. */
2035 l = list_alloc();
2036 if (l == NULL)
2037 return FAIL;
2038 while (item != NULL)
2039 {
2040 list_append_tv(l, &item->li_tv);
2041 item = item->li_next;
2042 }
2043
2044 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002045 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 ltv.vval.v_list = l;
2047 l->lv_refcount = 1;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2050 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 clear_tv(&ltv);
2052 if (arg == NULL)
2053 return FAIL;
2054 break;
2055 }
2056 else if (*arg != ',' && *arg != ']')
2057 {
2058 EMSG2(_(e_intern2), "ex_let_vars()");
2059 return FAIL;
2060 }
2061 }
2062
2063 return OK;
2064}
2065
2066/*
2067 * Skip over assignable variable "var" or list of variables "[var, var]".
2068 * Used for ":let varvar = expr" and ":for varvar in expr".
2069 * For "[var, var]" increment "*var_count" for each variable.
2070 * for "[var, var; var]" set "semicolon".
2071 * Return NULL for an error.
2072 */
2073 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074skip_var_list(
2075 char_u *arg,
2076 int *var_count,
2077 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078{
2079 char_u *p, *s;
2080
2081 if (*arg == '[')
2082 {
2083 /* "[var, var]": find the matching ']'. */
2084 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002085 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 {
2087 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2088 s = skip_var_one(p);
2089 if (s == p)
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 ++*var_count;
2095
2096 p = skipwhite(s);
2097 if (*p == ']')
2098 break;
2099 else if (*p == ';')
2100 {
2101 if (*semicolon == 1)
2102 {
2103 EMSG(_("Double ; in list of variables"));
2104 return NULL;
2105 }
2106 *semicolon = 1;
2107 }
2108 else if (*p != ',')
2109 {
2110 EMSG2(_(e_invarg2), p);
2111 return NULL;
2112 }
2113 }
2114 return p + 1;
2115 }
2116 else
2117 return skip_var_one(arg);
2118}
2119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002121 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 if (*arg == '@' && arg[1] != NUL)
2128 return arg + 2;
2129 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2130 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131}
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 * List variables for hashtab "ht" with prefix "prefix".
2135 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138list_hashtable_vars(
2139 hashtab_T *ht,
2140 char_u *prefix,
2141 int empty,
2142 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 hashitem_T *hi;
2145 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 int todo;
2147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2150 {
2151 if (!HASHITEM_EMPTY(hi))
2152 {
2153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 di = HI2DI(hi);
2155 if (empty || di->di_tv.v_type != VAR_STRING
2156 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 }
2159 }
2160}
2161
2162/*
2163 * List global variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List buffer variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002176{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 char_u numbuf[NUMBUFLEN];
2178
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181
2182 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2184 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List window variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002193 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197#ifdef FEAT_WINDOWS
2198/*
2199 * List tab page variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216}
2217
2218/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219 * List script-local variables, if there is a script.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2226 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
2230 * List function variables, if there is a function.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234{
2235 if (current_funccal != NULL)
2236 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238}
2239
2240/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 * List variables in "arg".
2242 */
2243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
2246 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 char_u *name_start;
2250 char_u *arg_subsc;
2251 char_u *tofree;
2252 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
2254 while (!ends_excmd(*arg) && !got_int)
2255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2260 {
2261 emsg_severe = TRUE;
2262 EMSG(_(e_trailing));
2263 break;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* get_name_len() takes care of expanding curly braces */
2269 name_start = name = arg;
2270 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2271 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* This is mainly to keep test 49 working: when expanding
2274 * curly braces fails overrule the exception error message. */
2275 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 emsg_severe = TRUE;
2278 EMSG2(_(e_invarg2), arg);
2279 break;
2280 }
2281 error = TRUE;
2282 }
2283 else
2284 {
2285 if (tofree != NULL)
2286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002287 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* handle d.key, l[idx], f(expr) */
2292 arg_subsc = arg;
2293 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'g': list_glob_vars(first); break;
2302 case 'b': list_buf_vars(first); break;
2303 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'v': list_vim_vars(first); break;
2308 case 's': list_script_vars(first); break;
2309 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 default:
2311 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
2314 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 {
2316 char_u numbuf[NUMBUFLEN];
2317 char_u *tf;
2318 int c;
2319 char_u *s;
2320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002321 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 c = *arg;
2323 *arg = NUL;
2324 list_one_var_a((char_u *)"",
2325 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002326 tv.v_type,
2327 s == NULL ? (char_u *)"" : s,
2328 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 *arg = c;
2330 vim_free(tf);
2331 }
2332 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 return arg;
2344}
2345
2346/*
2347 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2348 * Returns a pointer to the char just after the var name.
2349 * Returns NULL if there is an error.
2350 */
2351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352ex_let_one(
2353 char_u *arg, /* points to variable name */
2354 typval_T *tv, /* value to assign to variable */
2355 int copy, /* copy value from "tv" */
2356 char_u *endchars, /* valid chars after variable name or NULL */
2357 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358{
2359 int c1;
2360 char_u *name;
2361 char_u *p;
2362 char_u *arg_end = NULL;
2363 int len;
2364 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
2367 /*
2368 * ":let $VAR = expr": Set environment variable.
2369 */
2370 if (*arg == '$')
2371 {
2372 /* Find the end of the name. */
2373 ++arg;
2374 name = arg;
2375 len = get_env_len(&arg);
2376 if (len == 0)
2377 EMSG2(_(e_invarg2), name - 1);
2378 else
2379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 if (op != NULL && (*op == '+' || *op == '-'))
2381 EMSG2(_(e_letwrong), op);
2382 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002385 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
2387 c1 = name[len];
2388 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 p = get_tv_string_chk(tv);
2390 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 int mustfree = FALSE;
2393 char_u *s = vim_getenv(name, &mustfree);
2394
2395 if (s != NULL)
2396 {
2397 p = tofree = concat_str(s, p);
2398 if (mustfree)
2399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 if (STRICMP(name, "HOME") == 0)
2406 init_homedir();
2407 else if (didset_vim && STRICMP(name, "VIM") == 0)
2408 didset_vim = FALSE;
2409 else if (didset_vimruntime
2410 && STRICMP(name, "VIMRUNTIME") == 0)
2411 didset_vimruntime = FALSE;
2412 arg_end = arg;
2413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 }
2417 }
2418 }
2419
2420 /*
2421 * ":let &option = expr": Set option value.
2422 * ":let &l:option = expr": Set local option value.
2423 * ":let &g:option = expr": Set global option value.
2424 */
2425 else if (*arg == '&')
2426 {
2427 /* Find the end of the name. */
2428 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 if (p == NULL || (endchars != NULL
2430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 EMSG(_(e_letunexp));
2432 else
2433 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 long n;
2435 int opt_type;
2436 long numval;
2437 char_u *stringval = NULL;
2438 char_u *s;
2439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 c1 = *p;
2441 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442
2443 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 s = get_tv_string_chk(tv); /* != NULL if number or string */
2445 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
2447 opt_type = get_option_value(arg, &numval,
2448 &stringval, opt_flags);
2449 if ((opt_type == 1 && *op == '.')
2450 || (opt_type == 0 && *op != '.'))
2451 EMSG2(_(e_letwrong), op);
2452 else
2453 {
2454 if (opt_type == 1) /* number */
2455 {
2456 if (*op == '+')
2457 n = numval + n;
2458 else
2459 n = numval - n;
2460 }
2461 else if (opt_type == 0 && stringval != NULL) /* string */
2462 {
2463 s = concat_str(stringval, s);
2464 vim_free(stringval);
2465 stringval = s;
2466 }
2467 }
2468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (s != NULL)
2470 {
2471 set_option_value(arg, n, s, opt_flags);
2472 arg_end = p;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
2477 }
2478
2479 /*
2480 * ":let @r = expr": Set register contents.
2481 */
2482 else if (*arg == '@')
2483 {
2484 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (op != NULL && (*op == '+' || *op == '-'))
2486 EMSG2(_(e_letwrong), op);
2487 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *s;
2494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 p = get_tv_string_chk(tv);
2496 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002498 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 if (s != NULL)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 vim_free(s);
2503 }
2504 }
2505 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 arg_end = arg + 1;
2509 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002522 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 arg_end = p;
2531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535
2536 else
2537 EMSG2(_(e_invarg2), arg);
2538
2539 return arg_end;
2540}
2541
2542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2544 */
2545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547{
2548 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2549 {
2550 EMSG2(_(e_readonlyvar), arg);
2551 return TRUE;
2552 }
2553 return FALSE;
2554}
2555
2556/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Get an lval: variable, Dict item or List item that can be assigned a value
2558 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2559 * "name.key", "name.key[expr]" etc.
2560 * Indexing only works if "name" is an existing List or Dictionary.
2561 * "name" points to the start of the name.
2562 * If "rettv" is not NULL it points to the value to be assigned.
2563 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2564 * wrong; must end in space or cmd separator.
2565 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566 * flags:
2567 * GLV_QUIET: do not give error messages
2568 * GLV_NO_AUTOLOAD: do not use script autoloading
2569 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002571 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 */
2574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575get_lval(
2576 char_u *name,
2577 typval_T *rettv,
2578 lval_T *lp,
2579 int unlet,
2580 int skip,
2581 int flags, /* GLV_ values */
2582 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 char_u *expr_start, *expr_end;
2586 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 dictitem_T *v;
2588 typval_T var1;
2589 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002595 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599
2600 if (skip)
2601 {
2602 /* When skipping just find the end of the name. */
2603 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 }
2606
2607 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (expr_start != NULL)
2610 {
2611 /* Don't expand the name when we already know there is an error. */
2612 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2613 && *p != '[' && *p != '.')
2614 {
2615 EMSG(_(e_trailing));
2616 return NULL;
2617 }
2618
2619 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2620 if (lp->ll_exp_name == NULL)
2621 {
2622 /* Report an invalid expression in braces, unless the
2623 * expression evaluation has been cancelled due to an
2624 * aborting error, an interrupt, or an exception. */
2625 if (!aborting() && !quiet)
2626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002627 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 EMSG2(_(e_invarg2), name);
2629 return NULL;
2630 }
2631 }
2632 lp->ll_name = lp->ll_exp_name;
2633 }
2634 else
2635 lp->ll_name = name;
2636
2637 /* Without [idx] or .key we are done. */
2638 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2639 return p;
2640
2641 cc = *p;
2642 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (v == NULL && !quiet)
2645 EMSG2(_(e_undefvar), lp->ll_name);
2646 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 if (v == NULL)
2648 return NULL;
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /*
2651 * Loop until no more [idx] or .key is following.
2652 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2657 && !(lp->ll_tv->v_type == VAR_DICT
2658 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E689: Can only index a List or Dictionary"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E708: [:] must come last"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 len = -1;
2672 if (*p == '.')
2673 {
2674 key = p + 1;
2675 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2676 ;
2677 if (len == 0)
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_emptykey));
2681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = key + len;
2684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p == ':')
2690 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 empty1 = FALSE;
2694 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 if (get_tv_string_chk(&var1) == NULL)
2697 {
2698 /* not a number or string */
2699 clear_tv(&var1);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Optionally get the second index [ :expr]. */
2705 if (*p == ':')
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (rettv != NULL && (rettv->v_type != VAR_LIST
2716 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 p = skipwhite(p + 1);
2725 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var2) == NULL)
2737 {
2738 /* not a number or string */
2739 if (!empty1)
2740 clear_tv(&var1);
2741 clear_tv(&var2);
2742 return NULL;
2743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
2753 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760
2761 /* Skip to past ']'. */
2762 ++p;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
2767 if (len == -1)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (*key == NUL)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
2774 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 lp->ll_list = NULL;
2780 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 /* When assigning to a scope dictionary check that a function and
2784 * variable name is valid (only variable name unless it is l: or
2785 * g: dictionary). Disallow overwriting a builtin function. */
2786 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 int prevval;
2789 int wrong;
2790
2791 if (len != -1)
2792 {
2793 prevval = key[len];
2794 key[len] = NUL;
2795 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002796 else
2797 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2799 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 || !valid_varname(key);
2802 if (len != -1)
2803 key[len] = prevval;
2804 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 return NULL;
2806 }
2807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* Can't add "v:" variable. */
2811 if (lp->ll_dict == &vimvardict)
2812 {
2813 EMSG2(_(e_illvar), name);
2814 return NULL;
2815 }
2816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 p = NULL;
2834 break;
2835 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002837 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 else
2845 {
2846 /*
2847 * Get the number and item for the only or first index of the List.
2848 */
2849 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 else
2852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 clear_tv(&var1);
2855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_list = lp->ll_tv->vval.v_list;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 if (lp->ll_n1 < 0)
2862 {
2863 lp->ll_n1 = 0;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 }
2866 }
2867 if (lp->ll_li == NULL)
2868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
2876 /*
2877 * May need to find the item or absolute index for the second
2878 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * When no index given: "lp->ll_empty2" is TRUE.
2880 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2899 if (lp->ll_n1 < 0)
2900 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2901 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 {
2903 if (!quiet)
2904 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return p;
2914}
2915
2916/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 vim_free(lp->ll_exp_name);
2923 vim_free(lp->ll_newkey);
2924}
2925
2926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 */
2931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932set_var_lval(
2933 lval_T *lp,
2934 char_u *endp,
2935 typval_T *rettv,
2936 int copy,
2937 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938{
2939 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 listitem_T *ri;
2941 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942
2943 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 cc = *endp;
2948 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 if (op != NULL && *op != '=')
2950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 if ((di == NULL
2959 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2960 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2961 FALSE)))
2962 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 set_var(lp->ll_name, &tv, FALSE);
2964 clear_tv(&tv);
2965 }
2966 }
2967 else
2968 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (tv_check_lock(lp->ll_newkey == NULL
2973 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 else if (lp->ll_range)
2977 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 listitem_T *ll_li = lp->ll_li;
2979 int ll_n1 = lp->ll_n1;
2980
2981 /*
2982 * Check whether any of the list items is locked
2983 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002984 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002986 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002987 return;
2988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2990 break;
2991 ll_li = ll_li->li_next;
2992 ++ll_n1;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /*
2996 * Assign the List values to the list items.
2997 */
2998 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3002 else
3003 {
3004 clear_tv(&lp->ll_li->li_tv);
3005 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = ri->li_next;
3008 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3009 break;
3010 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003013 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = NULL;
3016 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 lp->ll_li = lp->ll_li->li_next;
3020 ++lp->ll_n1;
3021 }
3022 if (ri != NULL)
3023 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 else if (lp->ll_empty2
3025 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 : lp->ll_n1 != lp->ll_n2)
3027 EMSG(_("E711: List value has not enough items"));
3028 }
3029 else
3030 {
3031 /*
3032 * Assign to a List or Dictionary item.
3033 */
3034 if (lp->ll_newkey != NULL)
3035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 {
3038 EMSG2(_(e_letwrong), op);
3039 return;
3040 }
3041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 if (di == NULL)
3045 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3047 {
3048 vim_free(di);
3049 return;
3050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else if (op != NULL && *op != '=')
3054 {
3055 tv_op(lp->ll_tv, rettv, op);
3056 return;
3057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 /*
3062 * Assign the value to the variable or list item.
3063 */
3064 if (copy)
3065 copy_tv(rettv, lp->ll_tv);
3066 else
3067 {
3068 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003069 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 }
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073}
3074
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3077 * Returns OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081{
3082 long n;
3083 char_u numbuf[NUMBUFLEN];
3084 char_u *s;
3085
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3087 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3088 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 {
3090 switch (tv1->v_type)
3091 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 case VAR_DICT:
3094 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003095 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 break;
3099
3100 case VAR_LIST:
3101 if (*op != '+' || tv2->v_type != VAR_LIST)
3102 break;
3103 /* List += List */
3104 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3105 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3106 return OK;
3107
3108 case VAR_NUMBER:
3109 case VAR_STRING:
3110 if (tv2->v_type == VAR_LIST)
3111 break;
3112 if (*op == '+' || *op == '-')
3113 {
3114 /* nr += nr or nr -= nr*/
3115 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (tv2->v_type == VAR_FLOAT)
3118 {
3119 float_T f = n;
3120
3121 if (*op == '+')
3122 f += tv2->vval.v_float;
3123 else
3124 f -= tv2->vval.v_float;
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_FLOAT;
3127 tv1->vval.v_float = f;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
3131 {
3132 if (*op == '+')
3133 n += get_tv_number(tv2);
3134 else
3135 n -= get_tv_number(tv2);
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_NUMBER;
3138 tv1->vval.v_number = n;
3139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 }
3141 else
3142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (tv2->v_type == VAR_FLOAT)
3144 break;
3145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 /* str .= str */
3147 s = get_tv_string(tv1);
3148 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3149 clear_tv(tv1);
3150 tv1->v_type = VAR_STRING;
3151 tv1->vval.v_string = s;
3152 }
3153 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003156#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 {
3158 float_T f;
3159
3160 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3161 && tv2->v_type != VAR_NUMBER
3162 && tv2->v_type != VAR_STRING))
3163 break;
3164 if (tv2->v_type == VAR_FLOAT)
3165 f = tv2->vval.v_float;
3166 else
3167 f = get_tv_number(tv2);
3168 if (*op == '+')
3169 tv1->vval.v_float += f;
3170 else
3171 tv1->vval.v_float -= f;
3172 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003174 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 }
3177
3178 EMSG2(_(e_letwrong), op);
3179 return FAIL;
3180}
3181
3182/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * Add a watcher to a list.
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
3188 lw->lw_next = l->lv_watch;
3189 l->lv_watch = lw;
3190}
3191
3192/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003193 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 * No warning when it isn't found...
3195 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 lwp = &l->lv_watch;
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 {
3204 if (lw == lwrem)
3205 {
3206 *lwp = lw->lw_next;
3207 break;
3208 }
3209 lwp = &lw->lw_next;
3210 }
3211}
3212
3213/*
3214 * Just before removing an item from a list: advance watchers to the next
3215 * item.
3216 */
3217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3223 if (lw->lw_item == item)
3224 lw->lw_item = item->li_next;
3225}
3226
3227/*
3228 * Evaluate the expression used in a ":for var in expr" command.
3229 * "arg" points to "var".
3230 * Set "*errp" to TRUE for an error, FALSE otherwise;
3231 * Return a pointer that holds the info. Null when there is an error.
3232 */
3233 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234eval_for_line(
3235 char_u *arg,
3236 int *errp,
3237 char_u **nextcmdp,
3238 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 typval_T tv;
3243 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 *errp = TRUE; /* default: there is an error */
3246
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 if (fi == NULL)
3249 return NULL;
3250
3251 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3252 if (expr == NULL)
3253 return fi;
3254
3255 expr = skipwhite(expr);
3256 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 return fi;
3260 }
3261
3262 if (skip)
3263 ++emsg_skip;
3264 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3265 {
3266 *errp = FALSE;
3267 if (!skip)
3268 {
3269 l = tv.vval.v_list;
3270 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 clear_tv(&tv);
3274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 else
3276 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003277 /* No need to increment the refcount, it's already set for the
3278 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 fi->fi_list = l;
3280 list_add_watch(l, &fi->fi_lw);
3281 fi->fi_lw.lw_item = l->lv_first;
3282 }
3283 }
3284 }
3285 if (skip)
3286 --emsg_skip;
3287
3288 return fi;
3289}
3290
3291/*
3292 * Use the first item in a ":for" list. Advance to the next.
3293 * Assign the values to the variable (list). "arg" points to the first one.
3294 * Return TRUE when a valid item was found, FALSE when at end of list or
3295 * something wrong.
3296 */
3297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003324 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 list_unref(fi->fi_list);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 vim_free(fi);
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3333
3334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335set_context_for_expression(
3336 expand_T *xp,
3337 char_u *arg,
3338 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int got_eq = FALSE;
3341 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (cmdidx == CMD_let)
3345 {
3346 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003350 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 {
3352 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 if (vim_iswhite(*p))
3355 break;
3356 }
3357 return;
3358 }
3359 }
3360 else
3361 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3362 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((xp->xp_pattern = vim_strpbrk(arg,
3364 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3365 {
3366 c = *xp->xp_pattern;
3367 if (c == '&')
3368 {
3369 c = xp->xp_pattern[1];
3370 if (c == '&')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = cmdidx != CMD_let || got_eq
3374 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3375 }
3376 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003379 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3380 xp->xp_pattern += 2;
3381
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (c == '$')
3385 {
3386 /* environment variable */
3387 xp->xp_context = EXPAND_ENV_VARS;
3388 }
3389 else if (c == '=')
3390 {
3391 got_eq = TRUE;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && xp->xp_context == EXPAND_FUNCTIONS
3396 && vim_strchr(xp->xp_pattern, '(') == NULL)
3397 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 break;
3400 }
3401 else if (cmdidx != CMD_let || got_eq)
3402 {
3403 if (c == '"') /* string */
3404 {
3405 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3406 if (c == '\\' && xp->xp_pattern[1] != NUL)
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '\'') /* literal string */
3411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3414 /* skip */ ;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '|')
3418 {
3419 if (xp->xp_pattern[1] == '|')
3420 {
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
3425 xp->xp_context = EXPAND_COMMANDS;
3426 }
3427 else
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003431 /* Doesn't look like something valid, expand as an expression
3432 * anyway. */
3433 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = xp->xp_pattern;
3435 if (*arg != NUL)
3436 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3437 /* skip */ ;
3438 }
3439 xp->xp_pattern = arg;
3440}
3441
3442#endif /* FEAT_CMDL_COMPL */
3443
3444/*
3445 * ":1,25call func(arg1, arg2)" function call.
3446 */
3447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 if (eap->skip)
3462 {
3463 /* trans_function_name() doesn't work well when skipping, use eval0()
3464 * instead to skip to any following command, e.g. for:
3465 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003466 ++emsg_skip;
3467 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3468 clear_tv(&rettv);
3469 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 return;
3471 }
3472
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003474 if (fudi.fd_newkey != NULL)
3475 {
3476 /* Still need to give an error message for missing key. */
3477 EMSG2(_(e_dictkey), fudi.fd_newkey);
3478 vim_free(fudi.fd_newkey);
3479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 if (tofree == NULL)
3481 return;
3482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 /* Increase refcount on dictionary, it could get deleted when evaluating
3484 * the arguments. */
3485 if (fudi.fd_dict != NULL)
3486 ++fudi.fd_dict->dv_refcount;
3487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003490 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar532c7802005-01-27 14:44:31 +00003492 /* Skip white space to allow ":call func ()". Not good, but required for
3493 * backward compatibility. */
3494 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (*startarg != '(')
3498 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003499 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 goto end;
3501 }
3502
3503 /*
3504 * When skipping, evaluate the function once, to find the end of the
3505 * arguments.
3506 * When the function takes a range, this is discovered after the first
3507 * call, and the loop is broken.
3508 */
3509 if (eap->skip)
3510 {
3511 ++emsg_skip;
3512 lnum = eap->line2; /* do it once, also with an invalid range */
3513 }
3514 else
3515 lnum = eap->line1;
3516 for ( ; lnum <= eap->line2; ++lnum)
3517 {
3518 if (!eap->skip && eap->addr_count > 0)
3519 {
3520 curwin->w_cursor.lnum = lnum;
3521 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003522#ifdef FEAT_VIRTUALEDIT
3523 curwin->w_cursor.coladd = 0;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 eap->line1, eap->line2, &doesrange,
3529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
3535 /* Handle a function returning a Funcref, Dictionary or List. */
3536 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3537 {
3538 failed = TRUE;
3539 break;
3540 }
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (doesrange || eap->skip)
3544 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (aborting())
3551 break;
3552 }
3553 if (eap->skip)
3554 --emsg_skip;
3555
3556 if (!failed)
3557 {
3558 /* Check for trailing illegal characters and a following command. */
3559 if (!ends_excmd(*arg))
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 else
3565 eap->nextcmd = check_nextcmd(arg);
3566 }
3567
3568end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * ":unlet[!] var1 ... " command.
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int deep = 2;
3590
3591 if (eap->forceit)
3592 deep = -1;
3593 else if (vim_isdigit(*arg))
3594 {
3595 deep = getdigits(&arg);
3596 arg = skipwhite(arg);
3597 }
3598
3599 ex_unletlock(eap, arg, deep);
3600}
3601
3602/*
3603 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ex_unletlock(
3607 exarg_T *eap,
3608 char_u *argstart,
3609 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610{
3611 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 do
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003619 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003620 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (lv.ll_name == NULL)
3622 error = TRUE; /* error but continue parsing */
3623 if (name_end == NULL || (!vim_iswhite(*name_end)
3624 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (name_end != NULL)
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 if (!(eap->skip || error))
3632 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634 }
3635
3636 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 {
3638 if (eap->cmdidx == CMD_unlet)
3639 {
3640 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3641 error = TRUE;
3642 }
3643 else
3644 {
3645 if (do_lock_var(&lv, name_end, deep,
3646 eap->cmdidx == CMD_lockvar) == FAIL)
3647 error = TRUE;
3648 }
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (!eap->skip)
3652 clear_lval(&lv);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 arg = skipwhite(name_end);
3655 } while (!ends_excmd(*arg));
3656
3657 eap->nextcmd = check_nextcmd(arg);
3658}
3659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661do_unlet_var(
3662 lval_T *lp,
3663 char_u *name_end,
3664 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 int ret = OK;
3667 int cc;
3668
3669 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 cc = *name_end;
3672 *name_end = NUL;
3673
3674 /* Normal name or expanded name. */
3675 if (check_changedtick(lp->ll_name))
3676 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else if (lp->ll_range)
3687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003690 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691
3692 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3693 {
3694 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003696 return FAIL;
3697 ll_li = li;
3698 ++ll_n1;
3699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700
3701 /* Delete a range of List items. */
3702 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3703 {
3704 li = lp->ll_li->li_next;
3705 listitem_remove(lp->ll_list, lp->ll_li);
3706 lp->ll_li = li;
3707 ++lp->ll_n1;
3708 }
3709 }
3710 else
3711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a List item. */
3714 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003717 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 }
3719
3720 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else if (ht == &compat_hashtab)
3745 d = &vimvardict;
3746 else
3747 {
3748 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3749 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3750 }
3751 if (d == NULL)
3752 {
3753 EMSG2(_(e_intern2), "do_unlet()");
3754 return FAIL;
3755 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hi = hash_find(ht, varname);
3757 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003759 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003760 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003761 || var_check_ro(di->di_flags, name, FALSE)
3762 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003763 return FAIL;
3764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 delete_var(ht, hi);
3766 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769 if (forceit)
3770 return OK;
3771 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773}
3774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775/*
3776 * Lock or unlock variable indicated by "lp".
3777 * "deep" is the levels to go (-1 for unlimited);
3778 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3779 */
3780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781do_lock_var(
3782 lval_T *lp,
3783 char_u *name_end,
3784 int deep,
3785 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786{
3787 int ret = OK;
3788 int cc;
3789 dictitem_T *di;
3790
3791 if (deep == 0) /* nothing to do */
3792 return OK;
3793
3794 if (lp->ll_tv == NULL)
3795 {
3796 cc = *name_end;
3797 *name_end = NUL;
3798
3799 /* Normal name or expanded name. */
3800 if (check_changedtick(lp->ll_name))
3801 ret = FAIL;
3802 else
3803 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003804 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 if (di == NULL)
3806 ret = FAIL;
3807 else
3808 {
3809 if (lock)
3810 di->di_flags |= DI_FLAGS_LOCK;
3811 else
3812 di->di_flags &= ~DI_FLAGS_LOCK;
3813 item_lock(&di->di_tv, deep, lock);
3814 }
3815 }
3816 *name_end = cc;
3817 }
3818 else if (lp->ll_range)
3819 {
3820 listitem_T *li = lp->ll_li;
3821
3822 /* (un)lock a range of List items. */
3823 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3824 {
3825 item_lock(&li->li_tv, deep, lock);
3826 li = li->li_next;
3827 ++lp->ll_n1;
3828 }
3829 }
3830 else if (lp->ll_list != NULL)
3831 /* (un)lock a List item. */
3832 item_lock(&lp->ll_li->li_tv, deep, lock);
3833 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003834 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835 item_lock(&lp->ll_di->di_tv, deep, lock);
3836
3837 return ret;
3838}
3839
3840/*
3841 * Lock or unlock an item. "deep" is nr of levels to go.
3842 */
3843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845{
3846 static int recurse = 0;
3847 list_T *l;
3848 listitem_T *li;
3849 dict_T *d;
3850 hashitem_T *hi;
3851 int todo;
3852
3853 if (recurse >= DICT_MAXNEST)
3854 {
3855 EMSG(_("E743: variable nested too deep for (un)lock"));
3856 return;
3857 }
3858 if (deep == 0)
3859 return;
3860 ++recurse;
3861
3862 /* lock/unlock the item itself */
3863 if (lock)
3864 tv->v_lock |= VAR_LOCKED;
3865 else
3866 tv->v_lock &= ~VAR_LOCKED;
3867
3868 switch (tv->v_type)
3869 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 case VAR_UNKNOWN:
3871 case VAR_NUMBER:
3872 case VAR_STRING:
3873 case VAR_FUNC:
3874 case VAR_FLOAT:
3875 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003876 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003877 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 break;
3879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880 case VAR_LIST:
3881 if ((l = tv->vval.v_list) != NULL)
3882 {
3883 if (lock)
3884 l->lv_lock |= VAR_LOCKED;
3885 else
3886 l->lv_lock &= ~VAR_LOCKED;
3887 if (deep < 0 || deep > 1)
3888 /* recursive: lock/unlock the items the List contains */
3889 for (li = l->lv_first; li != NULL; li = li->li_next)
3890 item_lock(&li->li_tv, deep - 1, lock);
3891 }
3892 break;
3893 case VAR_DICT:
3894 if ((d = tv->vval.v_dict) != NULL)
3895 {
3896 if (lock)
3897 d->dv_lock |= VAR_LOCKED;
3898 else
3899 d->dv_lock &= ~VAR_LOCKED;
3900 if (deep < 0 || deep > 1)
3901 {
3902 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003904 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3905 {
3906 if (!HASHITEM_EMPTY(hi))
3907 {
3908 --todo;
3909 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3910 }
3911 }
3912 }
3913 }
3914 }
3915 --recurse;
3916}
3917
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003919 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3920 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924{
3925 return (tv->v_lock & VAR_LOCKED)
3926 || (tv->v_type == VAR_LIST
3927 && tv->vval.v_list != NULL
3928 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3929 || (tv->v_type == VAR_DICT
3930 && tv->vval.v_dict != NULL
3931 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3935/*
3936 * Delete all "menutrans_" variables.
3937 */
3938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 {
3948 if (!HASHITEM_EMPTY(hi))
3949 {
3950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3952 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 }
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3960
3961/*
3962 * Local string buffer for the next two functions to store a variable name
3963 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3964 * get_user_var_name().
3965 */
3966
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003967static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969static char_u *varnamebuf = NULL;
3970static int varnamebuflen = 0;
3971
3972/*
3973 * Function to concatenate a prefix and a variable name.
3974 */
3975 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int len;
3979
3980 len = (int)STRLEN(name) + 3;
3981 if (len > varnamebuflen)
3982 {
3983 vim_free(varnamebuf);
3984 len += 10; /* some additional space */
3985 varnamebuf = alloc(len);
3986 if (varnamebuf == NULL)
3987 {
3988 varnamebuflen = 0;
3989 return NULL;
3990 }
3991 varnamebuflen = len;
3992 }
3993 *varnamebuf = prefix;
3994 varnamebuf[1] = ':';
3995 STRCPY(varnamebuf + 2, name);
3996 return varnamebuf;
3997}
3998
3999/*
4000 * Function given to ExpandGeneric() to obtain the list of user defined
4001 * (global/buffer/window/built-in) variable names.
4002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static long_u gdone;
4007 static long_u bdone;
4008 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 static long_u tdone;
4011#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static int vidx;
4013 static hashitem_T *hi;
4014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 tdone = 0;
4021#endif
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* Global variables */
4025 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4034 return cat_prefix_varname('g', hi->hi_key);
4035 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
4038 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return (char_u *)"b:changedtick";
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004057 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004060 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (tdone < ht->ht_used)
4073 {
4074 if (tdone++ == 0)
4075 hi = ht->ht_array;
4076 else
4077 ++hi;
4078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('t', hi->hi_key);
4081 }
4082#endif
4083
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 /* v: variables */
4085 if (vidx < VV_LEN)
4086 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 vim_free(varnamebuf);
4089 varnamebuf = NULL;
4090 varnamebuflen = 0;
4091 return NULL;
4092}
4093
4094#endif /* FEAT_CMDL_COMPL */
4095
4096/*
4097 * types for expressions.
4098 */
4099typedef enum
4100{
4101 TYPE_UNKNOWN = 0
4102 , TYPE_EQUAL /* == */
4103 , TYPE_NEQUAL /* != */
4104 , TYPE_GREATER /* > */
4105 , TYPE_GEQUAL /* >= */
4106 , TYPE_SMALLER /* < */
4107 , TYPE_SEQUAL /* <= */
4108 , TYPE_MATCH /* =~ */
4109 , TYPE_NOMATCH /* !~ */
4110} exptype_T;
4111
4112/*
4113 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4116 */
4117
4118/*
4119 * Handle zero level expression.
4120 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004122 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval0(
4127 char_u *arg,
4128 typval_T *rettv,
4129 char_u **nextcmd,
4130 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 int ret;
4133 char_u *p;
4134
4135 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (ret == FAIL || !ends_excmd(*p))
4138 {
4139 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Report the invalid expression unless the expression evaluation has
4143 * been cancelled due to an aborting error, an interrupt, or an
4144 * exception.
4145 */
4146 if (!aborting())
4147 EMSG2(_(e_invexpr2), arg);
4148 ret = FAIL;
4149 }
4150 if (nextcmd != NULL)
4151 *nextcmd = check_nextcmd(p);
4152
4153 return ret;
4154}
4155
4156/*
4157 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004158 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004163 * Note: "rettv.v_lock" is not set.
4164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004168eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 if ((*arg)[0] == '?')
4180 {
4181 result = FALSE;
4182 if (evaluate)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
4185
4186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199
4200 /*
4201 * Check for the ":".
4202 */
4203 if ((*arg)[0] != ':')
4204 {
4205 EMSG(_("E109: Missing ':' after '?'"));
4206 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 /*
4212 * Get the third variable.
4213 */
4214 *arg = skipwhite(*arg + 1);
4215 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4216 {
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
4225 return OK;
4226}
4227
4228/*
4229 * Handle first level expression:
4230 * expr2 || expr2 || expr2 logical OR
4231 *
4232 * "arg" must point to the first non-white of the expression.
4233 * "arg" is advanced to the next non-white after the recognized expression.
4234 *
4235 * Return OK or FAIL.
4236 */
4237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004306eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 long result;
4310 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * Get the first variable.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
4318
4319 /*
4320 * Repeat until there is no following "&&".
4321 */
4322 first = TRUE;
4323 result = TRUE;
4324 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4325 {
4326 if (evaluate && first)
4327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 first = FALSE;
4334 }
4335
4336 /*
4337 * Get the second variable.
4338 */
4339 *arg = skipwhite(*arg + 2);
4340 if (eval4(arg, &var2, evaluate && result) == FAIL)
4341 return FAIL;
4342
4343 /*
4344 * Compute the result.
4345 */
4346 if (evaluate && result)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (evaluate)
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle third level expression:
4366 * var1 == var2
4367 * var1 =~ var2
4368 * var1 != var2
4369 * var1 !~ var2
4370 * var1 > var2
4371 * var1 >= var2
4372 * var1 < var2
4373 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 * var1 is var2
4375 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *p;
4387 int i;
4388 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int len = 2;
4391 long n1, n2;
4392 char_u *s1, *s2;
4393 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4394 regmatch_T regmatch;
4395 int ic;
4396 char_u *save_cpo;
4397
4398 /*
4399 * Get the first variable.
4400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 return FAIL;
4403
4404 p = *arg;
4405 switch (p[0])
4406 {
4407 case '=': if (p[1] == '=')
4408 type = TYPE_EQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_MATCH;
4411 break;
4412 case '!': if (p[1] == '=')
4413 type = TYPE_NEQUAL;
4414 else if (p[1] == '~')
4415 type = TYPE_NOMATCH;
4416 break;
4417 case '>': if (p[1] != '=')
4418 {
4419 type = TYPE_GREATER;
4420 len = 1;
4421 }
4422 else
4423 type = TYPE_GEQUAL;
4424 break;
4425 case '<': if (p[1] != '=')
4426 {
4427 type = TYPE_SMALLER;
4428 len = 1;
4429 }
4430 else
4431 type = TYPE_SEQUAL;
4432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 case 'i': if (p[1] == 's')
4434 {
4435 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4436 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004437 i = p[len];
4438 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 {
4440 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4441 type_is = TRUE;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446
4447 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 if (type != TYPE_UNKNOWN)
4451 {
4452 /* extra question mark appended: ignore case */
4453 if (p[len] == '?')
4454 {
4455 ic = TRUE;
4456 ++len;
4457 }
4458 /* extra '#' appended: match case */
4459 else if (p[len] == '#')
4460 {
4461 ic = FALSE;
4462 ++len;
4463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
4466 ic = p_ic;
4467
4468 /*
4469 * Get the second variable.
4470 */
4471 *arg = skipwhite(p + len);
4472 if (eval5(arg, &var2, evaluate) == FAIL)
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476 }
4477
4478 if (evaluate)
4479 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 if (type_is && rettv->v_type != var2.v_type)
4481 {
4482 /* For "is" a different type always means FALSE, for "notis"
4483 * it means TRUE. */
4484 n1 = (type == TYPE_NEQUAL);
4485 }
4486 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4487 {
4488 if (type_is)
4489 {
4490 n1 = (rettv->v_type == var2.v_type
4491 && rettv->vval.v_list == var2.vval.v_list);
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 else if (rettv->v_type != var2.v_type
4496 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4497 {
4498 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004499 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004501 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 clear_tv(rettv);
4503 clear_tv(&var2);
4504 return FAIL;
4505 }
4506 else
4507 {
4508 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004509 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4510 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 if (type == TYPE_NEQUAL)
4512 n1 = !n1;
4513 }
4514 }
4515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004516 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4517 {
4518 if (type_is)
4519 {
4520 n1 = (rettv->v_type == var2.v_type
4521 && rettv->vval.v_dict == var2.vval.v_dict);
4522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 else if (rettv->v_type != var2.v_type
4526 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4527 {
4528 if (rettv->v_type != var2.v_type)
4529 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4530 else
4531 EMSG(_("E736: Invalid operation for Dictionary"));
4532 clear_tv(rettv);
4533 clear_tv(&var2);
4534 return FAIL;
4535 }
4536 else
4537 {
4538 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004539 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4540 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004541 if (type == TYPE_NEQUAL)
4542 n1 = !n1;
4543 }
4544 }
4545
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4547 {
4548 if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Funcrefs for being equal or unequal. */
4562 if (rettv->vval.v_string == NULL
4563 || var2.vval.v_string == NULL)
4564 n1 = FALSE;
4565 else
4566 n1 = STRCMP(rettv->vval.v_string,
4567 var2.vval.v_string) == 0;
4568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346eval_index(
5347 char_u **arg,
5348 typval_T *rettv,
5349 int evaluate,
5350 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 case VAR_FUNC:
5363 if (verbose)
5364 EMSG(_("E695: Cannot index a Funcref"));
5365 return FAIL;
5366 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005367#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005374 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E909: Cannot index a special variable"));
5377 return FAIL;
5378 case VAR_UNKNOWN:
5379 if (evaluate)
5380 return FAIL;
5381 /* FALLTHROUGH */
5382
5383 case VAR_STRING:
5384 case VAR_NUMBER:
5385 case VAR_LIST:
5386 case VAR_DICT:
5387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 case VAR_FUNC:
5484 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_SPECIAL:
5486 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005487 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 break; /* not evaluating, skipping over subscript */
5489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 case VAR_NUMBER:
5491 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 len = (long)STRLEN(s);
5494 if (range)
5495 {
5496 /* The resulting variable is a substring. If the indexes
5497 * are out of range the result is empty. */
5498 if (n1 < 0)
5499 {
5500 n1 = len + n1;
5501 if (n1 < 0)
5502 n1 = 0;
5503 }
5504 if (n2 < 0)
5505 n2 = len + n2;
5506 else if (n2 >= len)
5507 n2 = len;
5508 if (n1 >= len || n2 < 0 || n1 > n2)
5509 s = NULL;
5510 else
5511 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5512 }
5513 else
5514 {
5515 /* The resulting variable is a string of a single
5516 * character. If the index is too big or negative the
5517 * result is empty. */
5518 if (n1 >= len || n1 < 0)
5519 s = NULL;
5520 else
5521 s = vim_strnsave(s + n1, 1);
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 clear_tv(rettv);
5524 rettv->v_type = VAR_STRING;
5525 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 break;
5527
5528 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 if (n1 < 0)
5531 n1 = len + n1;
5532 if (!empty1 && (n1 < 0 || n1 >= len))
5533 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005534 /* For a range we allow invalid values and return an empty
5535 * list. A list index out of range is an error. */
5536 if (!range)
5537 {
5538 if (verbose)
5539 EMSGN(_(e_listidx), n1);
5540 return FAIL;
5541 }
5542 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 if (range)
5545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 list_T *l;
5547 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548
5549 if (n2 < 0)
5550 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005551 else if (n2 >= len)
5552 n2 = len - 1;
5553 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 l = list_alloc();
5556 if (l == NULL)
5557 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 n1 <= n2; ++n1)
5560 {
5561 if (list_append_tv(l, &item->li_tv) == FAIL)
5562 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005563 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 return FAIL;
5565 }
5566 item = item->li_next;
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_LIST;
5570 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005571 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 else
5574 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005575 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 clear_tv(rettv);
5577 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
5581 case VAR_DICT:
5582 if (range)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 return FAIL;
5589 }
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
5593 if (len == -1)
5594 {
5595 key = get_tv_string(&var1);
5596 if (*key == NUL)
5597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (verbose)
5599 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 clear_tv(&var1);
5601 return FAIL;
5602 }
5603 }
5604
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005605 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005608 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (len == -1)
5610 clear_tv(&var1);
5611 if (item == NULL)
5612 return FAIL;
5613
5614 copy_tv(&item->di_tv, &var1);
5615 clear_tv(rettv);
5616 *rettv = var1;
5617 }
5618 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 }
5620 }
5621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Get an option value.
5627 * "arg" points to the '&' or '+' before the option name.
5628 * "arg" is advanced to character after the option name.
5629 * Return OK or FAIL.
5630 */
5631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632get_option_tv(
5633 char_u **arg,
5634 typval_T *rettv, /* when NULL, only check if option exists */
5635 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 char_u *option_end;
5638 long numval;
5639 char_u *stringval;
5640 int opt_type;
5641 int c;
5642 int working = (**arg == '+'); /* has("+option") */
5643 int ret = OK;
5644 int opt_flags;
5645
5646 /*
5647 * Isolate the option name and find its value.
5648 */
5649 option_end = find_option_end(arg, &opt_flags);
5650 if (option_end == NULL)
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E112: Option name missing: %s"), *arg);
5654 return FAIL;
5655 }
5656
5657 if (!evaluate)
5658 {
5659 *arg = option_end;
5660 return OK;
5661 }
5662
5663 c = *option_end;
5664 *option_end = NUL;
5665 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (opt_type == -3) /* invalid name */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E113: Unknown option: %s"), *arg);
5672 ret = FAIL;
5673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 if (opt_type == -2) /* hidden string option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_STRING;
5679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == -1) /* hidden number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else if (opt_type == 1) /* number option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_NUMBER;
5689 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else /* string option */
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 }
5697 else if (working && (opt_type == -2 || opt_type == -1))
5698 ret = FAIL;
5699
5700 *option_end = c; /* put back for error messages */
5701 *arg = option_end;
5702
5703 return ret;
5704}
5705
5706/*
5707 * Allocate a variable for a string constant.
5708 * Return OK or FAIL.
5709 */
5710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *p;
5714 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int extra = 0;
5716
5717 /*
5718 * Find the end of the string, skipping backslashed characters.
5719 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (*p == '\\' && p[1] != NUL)
5723 {
5724 ++p;
5725 /* A "\<x>" form occupies at least 4 characters, and produces up
5726 * to 6 characters: reserve space for 2 extra */
5727 if (*p == '<')
5728 extra += 2;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731
5732 if (*p != '"')
5733 {
5734 EMSG2(_("E114: Missing quote: %s"), *arg);
5735 return FAIL;
5736 }
5737
5738 /* If only parsing, set *arg and return here */
5739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
5746 * Copy the string into allocated memory, handling backslashed
5747 * characters.
5748 */
5749 name = alloc((unsigned)(p - *arg + extra));
5750 if (name == NULL)
5751 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (*p == '\\')
5758 {
5759 switch (*++p)
5760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case 'b': *name++ = BS; ++p; break;
5762 case 'e': *name++ = ESC; ++p; break;
5763 case 'f': *name++ = FF; ++p; break;
5764 case 'n': *name++ = NL; ++p; break;
5765 case 'r': *name++ = CAR; ++p; break;
5766 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 case 'X': /* hex: "\x1", "\x12" */
5769 case 'x':
5770 case 'u': /* Unicode: "\u0023" */
5771 case 'U':
5772 if (vim_isxdigit(p[1]))
5773 {
5774 int n, nr;
5775 int c = toupper(*p);
5776
5777 if (c == 'X')
5778 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005781 else
5782 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 nr = 0;
5784 while (--n >= 0 && vim_isxdigit(p[1]))
5785 {
5786 ++p;
5787 nr = (nr << 4) + hex2nr(*p);
5788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_MBYTE
5791 /* For "\u" store the number according to
5792 * 'encoding'. */
5793 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* octal: "\1", "\12", "\123" */
5802 case '0':
5803 case '1':
5804 case '2':
5805 case '3':
5806 case '4':
5807 case '5':
5808 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '7': *name = *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 *name = (*name << 3) + *p++ - '0';
5813 if (*p >= '0' && *p <= '7')
5814 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818
5819 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (extra != 0)
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 /* FALLTHROUGH */
5827
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 break;
5830 }
5831 }
5832 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 *arg = p + 1;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return OK;
5840}
5841
5842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 * Return OK or FAIL.
5845 */
5846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 int reduce = 0;
5852
5853 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 */
5856 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 break;
5862 ++reduce;
5863 ++p;
5864 }
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 return FAIL;
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 if (!evaluate)
5875 {
5876 *arg = p + 1;
5877 return OK;
5878 }
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 str = alloc((unsigned)((p - *arg) - reduce));
5884 if (str == NULL)
5885 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 break;
5895 ++p;
5896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 *arg = p + 1;
5901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Allocate a variable for a List and fill it from "*arg".
5907 * Return OK or FAIL.
5908 */
5909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l = NULL;
5913 typval_T tv;
5914 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915
5916 if (evaluate)
5917 {
5918 l = list_alloc();
5919 if (l == NULL)
5920 return FAIL;
5921 }
5922
5923 *arg = skipwhite(*arg + 1);
5924 while (**arg != ']' && **arg != NUL)
5925 {
5926 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5927 goto failret;
5928 if (evaluate)
5929 {
5930 item = listitem_alloc();
5931 if (item != NULL)
5932 {
5933 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005934 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 list_append(l, item);
5936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 else
5938 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940
5941 if (**arg == ']')
5942 break;
5943 if (**arg != ',')
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 goto failret;
5947 }
5948 *arg = skipwhite(*arg + 1);
5949 }
5950
5951 if (**arg != ']')
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954failret:
5955 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005956 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 return FAIL;
5958 }
5959
5960 *arg = skipwhite(*arg + 1);
5961 if (evaluate)
5962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 rettv->v_type = VAR_LIST;
5964 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 ++l->lv_refcount;
5966 }
5967
5968 return OK;
5969}
5970
5971/*
5972 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005973 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005975 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 list_T *l;
5979
5980 l = (list_T *)alloc_clear(sizeof(list_T));
5981 if (l != NULL)
5982 {
5983 /* Prepend the list to the list of lists for garbage collection. */
5984 if (first_list != NULL)
5985 first_list->lv_used_prev = l;
5986 l->lv_used_prev = NULL;
5987 l->lv_used_next = first_list;
5988 first_list = l;
5989 }
5990 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991}
5992
5993/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994 * Allocate an empty list for a return value.
5995 * Returns OK or FAIL.
5996 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005999{
6000 list_T *l = list_alloc();
6001
6002 if (l == NULL)
6003 return FAIL;
6004
6005 rettv->vval.v_list = l;
6006 rettv->v_type = VAR_LIST;
6007 ++l->lv_refcount;
6008 return OK;
6009}
6010
6011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Unreference a list: decrement the reference count and free it when it
6013 * becomes zero.
6014 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006016list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006018 if (l != NULL && --l->lv_refcount <= 0)
6019 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006023 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Ignores the reference count.
6025 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027list_free(
6028 list_T *l,
6029 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 /* Remove the list from the list of lists for garbage collection. */
6034 if (l->lv_used_prev == NULL)
6035 first_list = l->lv_used_next;
6036 else
6037 l->lv_used_prev->lv_used_next = l->lv_used_next;
6038 if (l->lv_used_next != NULL)
6039 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6040
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 /* Remove the item before deleting it. */
6044 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (recurse || (item->li_tv.v_type != VAR_LIST
6046 && item->li_tv.v_type != VAR_DICT))
6047 clear_tv(&item->li_tv);
6048 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 vim_free(l);
6051}
6052
6053/*
6054 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006055 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006058listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059{
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061}
6062
6063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006064 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 vim_free(item);
6071}
6072
6073/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074 * Remove a list item from a List and free it. Also clears the value.
6075 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 if (l == NULL)
6090 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006091 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092}
6093
6094/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 * Return TRUE when two lists have exactly the same values.
6096 */
6097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098list_equal(
6099 list_T *l1,
6100 list_T *l2,
6101 int ic, /* ignore case for strings */
6102 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (l1 == NULL || l2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (l1 == l2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (list_len(l1) != list_len(l2))
6111 return FALSE;
6112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 for (item1 = l1->lv_first, item2 = l2->lv_first;
6114 item1 != NULL && item2 != NULL;
6115 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 return FALSE;
6118 return item1 == NULL && item2 == NULL;
6119}
6120
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121/*
6122 * Return the dictitem that an entry in a hashtable points to.
6123 */
6124 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126{
6127 return HI2DI(hi);
6128}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 * Return TRUE when two dictionaries have exactly the same key/values.
6132 */
6133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134dict_equal(
6135 dict_T *d1,
6136 dict_T *d2,
6137 int ic, /* ignore case for strings */
6138 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 hashitem_T *hi;
6141 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006144 if (d1 == NULL || d2 == NULL)
6145 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 if (d1 == d2)
6147 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 if (dict_len(d1) != dict_len(d2))
6149 return FALSE;
6150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 if (!HASHITEM_EMPTY(hi))
6155 {
6156 item2 = dict_find(d2, hi->hi_key, -1);
6157 if (item2 == NULL)
6158 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 return FALSE;
6161 --todo;
6162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 }
6164 return TRUE;
6165}
6166
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167static int tv_equal_recurse_limit;
6168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006172 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 */
6174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175tv_equal(
6176 typval_T *tv1,
6177 typval_T *tv2,
6178 int ic, /* ignore case */
6179 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
6181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006184 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006189 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 * recursiveness to a limit. We guess they are equal then.
6191 * A fixed limit has the problem of still taking an awful long time.
6192 * Reduce the limit every time running into it. That should work fine for
6193 * deeply linked structures that are not recursively linked and catch
6194 * recursiveness quickly. */
6195 if (!recursive)
6196 tv_equal_recurse_limit = 1000;
6197 if (recursive_cnt >= tv_equal_recurse_limit)
6198 {
6199 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_FUNC:
6218 return (tv1->vval.v_string != NULL
6219 && tv2->vval.v_string != NULL
6220 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6221
6222 case VAR_NUMBER:
6223 return tv1->vval.v_number == tv2->vval.v_number;
6224
6225 case VAR_STRING:
6226 s1 = get_tv_string_buf(tv1, buf1);
6227 s2 = get_tv_string_buf(tv2, buf2);
6228 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006229
6230 case VAR_SPECIAL:
6231 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006232
6233 case VAR_FLOAT:
6234#ifdef FEAT_FLOAT
6235 return tv1->vval.v_float == tv2->vval.v_float;
6236#endif
6237 case VAR_JOB:
6238#ifdef FEAT_JOB
6239 return tv1->vval.v_job == tv2->vval.v_job;
6240#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006241 case VAR_CHANNEL:
6242#ifdef FEAT_CHANNEL
6243 return tv1->vval.v_channel == tv2->vval.v_channel;
6244#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006245 case VAR_UNKNOWN:
6246 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
Bram Moolenaara03f2332016-02-06 18:09:59 +01006249 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6250 * does not equal anything, not even itself. */
6251 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252}
6253
6254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 * Locate item with index "n" in list "l" and return it.
6256 * A negative index is counted from the end; -1 is the last item.
6257 * Returns NULL when "n" is out of range.
6258 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338list_find_nr(
6339 list_T *l,
6340 long idx,
6341 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006360{
6361 listitem_T *li;
6362
6363 li = list_find(l, idx - 1);
6364 if (li == NULL)
6365 {
6366 EMSGN(_(e_listidx), idx);
6367 return NULL;
6368 }
6369 return get_tv_string(&li->li_tv);
6370}
6371
6372/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373 * Locate "item" list "l" and return its index.
6374 * Returns -1 when "item" is not in the list.
6375 */
6376 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378{
6379 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381
6382 if (l == NULL)
6383 return -1;
6384 idx = 0;
6385 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6386 ++idx;
6387 if (li == NULL)
6388 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006389 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390}
6391
6392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * Append item "item" to the end of list "l".
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
6398 if (l->lv_last == NULL)
6399 {
6400 /* empty list */
6401 l->lv_first = item;
6402 l->lv_last = item;
6403 item->li_prev = NULL;
6404 }
6405 else
6406 {
6407 l->lv_last->li_next = item;
6408 item->li_prev = l->lv_last;
6409 l->lv_last = item;
6410 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 item->li_next = NULL;
6413}
6414
6415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 copy_tv(tv, &li->li_tv);
6427 list_append(l, li);
6428 return OK;
6429}
6430
6431/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006432 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 * Return FAIL when out of memory.
6434 */
6435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437{
6438 listitem_T *li = listitem_alloc();
6439
6440 if (li == NULL)
6441 return FAIL;
6442 li->li_tv.v_type = VAR_DICT;
6443 li->li_tv.v_lock = 0;
6444 li->li_tv.vval.v_dict = dict;
6445 list_append(list, li);
6446 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return OK;
6448}
6449
6450/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 * Returns FAIL when out of memory.
6454 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457{
6458 listitem_T *li = listitem_alloc();
6459
6460 if (li == NULL)
6461 return FAIL;
6462 list_append(l, li);
6463 li->li_tv.v_type = VAR_STRING;
6464 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465 if (str == NULL)
6466 li->li_tv.vval.v_string = NULL;
6467 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006468 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469 return FAIL;
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474 * Append "n" to list "l".
6475 * Returns FAIL when out of memory.
6476 */
6477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006479{
6480 listitem_T *li;
6481
6482 li = listitem_alloc();
6483 if (li == NULL)
6484 return FAIL;
6485 li->li_tv.v_type = VAR_NUMBER;
6486 li->li_tv.v_lock = 0;
6487 li->li_tv.vval.v_number = n;
6488 list_append(l, li);
6489 return OK;
6490}
6491
6492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 * If "item" is NULL append at the end.
6495 * Return FAIL when out of memory.
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501
6502 if (ni == NULL)
6503 return FAIL;
6504 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006505 list_insert(l, ni, item);
6506 return OK;
6507}
6508
6509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006546 /* We also quit the loop when we have inserted the original item count of
6547 * the list, avoid a hang when we extend a list with itself. */
6548 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6550 return FAIL;
6551 return OK;
6552}
6553
6554/*
6555 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6556 * Return FAIL when out of memory.
6557 */
6558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006563 if (l1 == NULL || l2 == NULL)
6564 return FAIL;
6565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 if (l == NULL)
6569 return FAIL;
6570 tv->v_type = VAR_LIST;
6571 tv->vval.v_list = l;
6572
6573 /* append all items from the second list */
6574 return list_extend(l, l2, NULL);
6575}
6576
6577/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Returns NULL when out of memory.
6582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *copy;
6587 listitem_T *item;
6588 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (orig == NULL)
6591 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592
6593 copy = list_alloc();
6594 if (copy != NULL)
6595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (copyID != 0)
6597 {
6598 /* Do this before adding the items, because one of the items may
6599 * refer back to this list. */
6600 orig->lv_copyID = copyID;
6601 orig->lv_copylist = copy;
6602 }
6603 for (item = orig->lv_first; item != NULL && !got_int;
6604 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 {
6606 ni = listitem_alloc();
6607 if (ni == NULL)
6608 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6612 {
6613 vim_free(ni);
6614 break;
6615 }
6616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 list_append(copy, ni);
6620 }
6621 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 if (item != NULL)
6623 {
6624 list_unref(copy);
6625 copy = NULL;
6626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 }
6628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 return copy;
6630}
6631
6632/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006634 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635 * This used to be called list_remove, but that conflicts with a Sun header
6636 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640{
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643 /* notify watchers */
6644 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006646 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 list_fix_watch(l, ip);
6648 if (ip == item2)
6649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651
6652 if (item2->li_next == NULL)
6653 l->lv_last = item->li_prev;
6654 else
6655 item2->li_next->li_prev = item->li_prev;
6656 if (item->li_prev == NULL)
6657 l->lv_first = item2->li_next;
6658 else
6659 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661}
6662
6663/*
6664 * Return an allocated string with the string representation of a list.
6665 * May return NULL.
6666 */
6667 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669{
6670 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 if (tv->vval.v_list == NULL)
6673 return NULL;
6674 ga_init2(&ga, (int)sizeof(char), 80);
6675 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 {
6678 vim_free(ga.ga_data);
6679 return NULL;
6680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 ga_append(&ga, ']');
6682 ga_append(&ga, NUL);
6683 return (char_u *)ga.ga_data;
6684}
6685
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686typedef struct join_S {
6687 char_u *s;
6688 char_u *tofree;
6689} join_T;
6690
6691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692list_join_inner(
6693 garray_T *gap, /* to store the result in */
6694 list_T *l,
6695 char_u *sep,
6696 int echo_style,
6697 int copyID,
6698 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699{
6700 int i;
6701 join_T *p;
6702 int len;
6703 int sumlen = 0;
6704 int first = TRUE;
6705 char_u *tofree;
6706 char_u numbuf[NUMBUFLEN];
6707 listitem_T *item;
6708 char_u *s;
6709
6710 /* Stringify each item in the list. */
6711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6712 {
6713 if (echo_style)
6714 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6715 else
6716 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6717 if (s == NULL)
6718 return FAIL;
6719
6720 len = (int)STRLEN(s);
6721 sumlen += len;
6722
Bram Moolenaarcde88542015-08-11 19:14:00 +02006723 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6725 if (tofree != NULL || s != numbuf)
6726 {
6727 p->s = s;
6728 p->tofree = tofree;
6729 }
6730 else
6731 {
6732 p->s = vim_strnsave(s, len);
6733 p->tofree = p->s;
6734 }
6735
6736 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006737 if (did_echo_string_emsg) /* recursion error, bail out */
6738 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 }
6740
6741 /* Allocate result buffer with its total size, avoid re-allocation and
6742 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6743 if (join_gap->ga_len >= 2)
6744 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6745 if (ga_grow(gap, sumlen + 2) == FAIL)
6746 return FAIL;
6747
6748 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6749 {
6750 if (first)
6751 first = FALSE;
6752 else
6753 ga_concat(gap, sep);
6754 p = ((join_T *)join_gap->ga_data) + i;
6755
6756 if (p->s != NULL)
6757 ga_concat(gap, p->s);
6758 line_breakcheck();
6759 }
6760
6761 return OK;
6762}
6763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006766 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006770list_join(
6771 garray_T *gap,
6772 list_T *l,
6773 char_u *sep,
6774 int echo_style,
6775 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 garray_T join_ga;
6778 int retval;
6779 join_T *p;
6780 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781
Bram Moolenaard39a7512015-04-16 22:51:22 +02006782 if (l->lv_len < 1)
6783 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6785 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6786
6787 /* Dispose each item in join_ga. */
6788 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 p = (join_T *)join_ga.ga_data;
6791 for (i = 0; i < join_ga.ga_len; ++i)
6792 {
6793 vim_free(p->tofree);
6794 ++p;
6795 }
6796 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798
6799 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800}
6801
6802/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803 * Return the next (unique) copy ID.
6804 * Used for serializing nested structures.
6805 */
6806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006808{
6809 current_copyID += COPYID_INC;
6810 return current_copyID;
6811}
6812
6813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 * Garbage collection for lists and dictionaries.
6815 *
6816 * We use reference counts to be able to free most items right away when they
6817 * are no longer used. But for composite items it's possible that it becomes
6818 * unused while the reference count is > 0: When there is a recursive
6819 * reference. Example:
6820 * :let l = [1, 2, 3]
6821 * :let d = {9: l}
6822 * :let l[1] = d
6823 *
6824 * Since this is quite unusual we handle this with garbage collection: every
6825 * once in a while find out which lists and dicts are not referenced from any
6826 * variable.
6827 *
6828 * Here is a good reference text about garbage collection (refers to Python
6829 * but it applies to all reference-counting mechanisms):
6830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Do garbage collection for lists and dicts.
6835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 buf_T *buf;
6843 win_T *wp;
6844 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006845 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006846 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_WINDOWS
6849 tabpage_T *tp;
6850#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006852 /* Only do this once. */
6853 want_garbage_collect = FALSE;
6854 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006855 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006856
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 /* We advance by two because we add one for items referenced through
6858 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006859 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /*
6862 * 1. Go through all accessible variables and mark all lists and dicts
6863 * with copyID.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865
6866 /* Don't free variables in the previous_funccal list unless they are only
6867 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006868 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6870 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6872 NULL);
6873 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6874 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 }
6876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 /* script-local variables */
6878 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* buffer-local variables */
6882 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885
6886 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006890#ifdef FEAT_AUTOCMD
6891 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#ifdef FEAT_WINDOWS
6897 /* tabpage-local variables */
6898 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6900 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901#endif
6902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* function-local variables */
6907 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6910 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 }
6912
Bram Moolenaard812df62008-11-09 12:46:09 +00006913 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006915
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006918#endif
6919
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006922#endif
6923
6924#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006928#ifdef FEAT_CHANNEL
6929 abort = abort || set_ref_in_channel(copyID);
6930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006969 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 */
6971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 dict_T *dd, *dd_next;
6975 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 int did_free = FALSE;
6977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 */
6981 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 {
6983 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 /* Free the Dictionary and ordinary items it contains, but don't
6987 * recurse into Lists and Dictionaries, they will be in the list
6988 * of dicts or list of lists. */
6989 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dd = dd_next;
6993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
6995 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 * Go through the list of lists and free items without the copyID.
6997 * But don't free a list that has a watcher (used in a for loop), these
6998 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
7000 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007001 {
7002 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7004 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007006 /* Free the List and ordinary items it contains, but don't recurse
7007 * into Lists and Dictionaries, they will be in the list of dicts
7008 * or list of lists. */
7009 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 ll = ll_next;
7013 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026{
7027 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 hashtab_T *cur_ht;
7031 ht_stack_T *ht_stack = NULL;
7032 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 cur_ht = ht;
7035 for (;;)
7036 {
7037 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 /* Mark each item in the hashtab. If the item contains a hashtab
7040 * it is added to ht_stack, if it contains a list it is added to
7041 * list_stack. */
7042 todo = (int)cur_ht->ht_used;
7043 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7044 if (!HASHITEM_EMPTY(hi))
7045 {
7046 --todo;
7047 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7048 &ht_stack, list_stack);
7049 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051
7052 if (ht_stack == NULL)
7053 break;
7054
7055 /* take an item from the stack */
7056 cur_ht = ht_stack->ht;
7057 tempitem = ht_stack;
7058 ht_stack = ht_stack->prev;
7059 free(tempitem);
7060 }
7061
7062 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063}
7064
7065/*
7066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7068 *
7069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 listitem_T *li;
7075 int abort = FALSE;
7076 list_T *cur_l;
7077 list_stack_T *list_stack = NULL;
7078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 cur_l = l;
7081 for (;;)
7082 {
7083 if (!abort)
7084 /* Mark each item in the list. If the item contains a hashtab
7085 * it is added to ht_stack, if it contains a list it is added to
7086 * list_stack. */
7087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7089 ht_stack, &list_stack);
7090 if (list_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_l = list_stack->list;
7095 tempitem = list_stack;
7096 list_stack = list_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "list_stack" is used to add lists to be marked. Can be NULL.
7106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7107 *
7108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111set_ref_in_item(
7112 typval_T *tv,
7113 int copyID,
7114 ht_stack_T **ht_stack,
7115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 dict_T *dd;
7118 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 dd = tv->vval.v_dict;
7124 if (dd != NULL && dd->dv_copyID != copyID)
7125 {
7126 /* Didn't see this dict yet. */
7127 dd->dv_copyID = copyID;
7128 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7131 }
7132 else
7133 {
7134 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7135 if (newitem == NULL)
7136 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 else
7138 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 newitem->ht = &dd->dv_hashtab;
7140 newitem->prev = *ht_stack;
7141 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007144 }
7145 }
7146 else if (tv->v_type == VAR_LIST)
7147 {
7148 ll = tv->vval.v_list;
7149 if (ll != NULL && ll->lv_copyID != copyID)
7150 {
7151 /* Didn't see this list yet. */
7152 ll->lv_copyID = copyID;
7153 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 abort = set_ref_in_list(ll, copyID, ht_stack);
7156 }
7157 else
7158 {
7159 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 if (newitem == NULL)
7162 abort = TRUE;
7163 else
7164 {
7165 newitem->list = ll;
7166 newitem->prev = *list_stack;
7167 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Allocate an empty header for a dictionary.
7177 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007178 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007185 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007186 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 if (first_dict != NULL)
7188 first_dict->dv_used_prev = d;
7189 d->dv_used_next = first_dict;
7190 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007191 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200}
7201
7202/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007203 * Allocate an empty dict for a return value.
7204 * Returns OK or FAIL.
7205 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007208{
7209 dict_T *d = dict_alloc();
7210
7211 if (d == NULL)
7212 return FAIL;
7213
7214 rettv->vval.v_dict = d;
7215 rettv->v_type = VAR_DICT;
7216 ++d->dv_refcount;
7217 return OK;
7218}
7219
7220
7221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 * Unreference a Dictionary: decrement the reference count and free it when it
7223 * becomes zero.
7224 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007228 if (d != NULL && --d->dv_refcount <= 0)
7229 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007233 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Ignores the reference count.
7235 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237dict_free(
7238 dict_T *d,
7239 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245 /* Remove the dict from the list of dicts for garbage collection. */
7246 if (d->dv_used_prev == NULL)
7247 first_dict = d->dv_used_next;
7248 else
7249 d->dv_used_prev->dv_used_next = d->dv_used_next;
7250 if (d->dv_used_next != NULL)
7251 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7252
7253 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 if (!HASHITEM_EMPTY(hi))
7259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 /* Remove the item before deleting it, just in case there is
7261 * something recursive causing trouble. */
7262 di = HI2DI(hi);
7263 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 if (recurse || (di->di_tv.v_type != VAR_LIST
7265 && di->di_tv.v_type != VAR_DICT))
7266 clear_tv(&di->di_tv);
7267 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 --todo;
7269 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 vim_free(d);
7273}
7274
7275/*
7276 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277 * The "key" is copied to the new item.
7278 * Note that the value of the item "di_tv" still needs to be initialized!
7279 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007281 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007290 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293}
7294
7295/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 * Make a copy of a Dictionary item.
7297 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007318dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (HASHITEM_EMPTY(hi))
7324 EMSG2(_(e_intern2), "dictitem_remove()");
7325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 dictitem_free(item);
7328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Free a dict item. Also clears the value.
7332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007337 if (item->di_flags & DI_FLAGS_ALLOC)
7338 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339}
7340
7341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7343 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007344 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Returns NULL when out of memory.
7346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *copy;
7351 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354
7355 if (orig == NULL)
7356 return NULL;
7357
7358 copy = dict_alloc();
7359 if (copy != NULL)
7360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 if (copyID != 0)
7362 {
7363 orig->dv_copyID = copyID;
7364 orig->dv_copydict = copy;
7365 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 --todo;
7372
7373 di = dictitem_alloc(hi->hi_key);
7374 if (di == NULL)
7375 break;
7376 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 {
7378 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7379 copyID) == FAIL)
7380 {
7381 vim_free(di);
7382 break;
7383 }
7384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 else
7386 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7387 if (dict_add(copy, di) == FAIL)
7388 {
7389 dictitem_free(di);
7390 break;
7391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 if (todo > 0)
7397 {
7398 dict_unref(copy);
7399 copy = NULL;
7400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 }
7402
7403 return copy;
7404}
7405
7406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007408 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414}
7415
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007417 * Add a number or string entry to dictionary "d".
7418 * When "str" is NULL use number "nr", otherwise use "str".
7419 * Returns FAIL when out of memory and when key already exists.
7420 */
7421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007422dict_add_nr_str(
7423 dict_T *d,
7424 char *key,
7425 long nr,
7426 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007427{
7428 dictitem_T *item;
7429
7430 item = dictitem_alloc((char_u *)key);
7431 if (item == NULL)
7432 return FAIL;
7433 item->di_tv.v_lock = 0;
7434 if (str == NULL)
7435 {
7436 item->di_tv.v_type = VAR_NUMBER;
7437 item->di_tv.vval.v_number = nr;
7438 }
7439 else
7440 {
7441 item->di_tv.v_type = VAR_STRING;
7442 item->di_tv.vval.v_string = vim_strsave(str);
7443 }
7444 if (dict_add(d, item) == FAIL)
7445 {
7446 dictitem_free(item);
7447 return FAIL;
7448 }
7449 return OK;
7450}
7451
7452/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007453 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 item->di_tv.v_type = VAR_LIST;
7466 item->di_tv.vval.v_list = list;
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007472 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007473 return OK;
7474}
7475
7476/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 * Get the number of items in a Dictionary.
7478 */
7479 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 if (d == NULL)
7483 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007484 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485}
7486
7487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 * Find item "key[len]" in Dictionary "d".
7489 * If "len" is negative use strlen(key).
7490 * Returns NULL when not found.
7491 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007492 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007493dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495#define AKEYLEN 200
7496 char_u buf[AKEYLEN];
7497 char_u *akey;
7498 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 if (len < 0)
7502 akey = key;
7503 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 tofree = akey = vim_strnsave(key, len);
7506 if (akey == NULL)
7507 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 else
7510 {
7511 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007512 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 akey = buf;
7514 }
7515
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 vim_free(tofree);
7518 if (HASHITEM_EMPTY(hi))
7519 return NULL;
7520 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521}
7522
7523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007524 * Get a string item from a dictionary.
7525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526 * Returns NULL if the entry doesn't exist or out of memory.
7527 */
7528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530{
7531 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 s = get_tv_string(&di->di_tv);
7538 if (save && s != NULL)
7539 s = vim_strsave(s);
7540 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541}
7542
7543/*
7544 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007545 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546 */
7547 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549{
7550 dictitem_T *di;
7551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return 0;
7555 return get_tv_number(&di->di_tv);
7556}
7557
7558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 * Return an allocated string with the string representation of a Dictionary.
7560 * May return NULL.
7561 */
7562 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
7565 garray_T ga;
7566 int first = TRUE;
7567 char_u *tofree;
7568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 return NULL;
7576 ga_init2(&ga, (int)sizeof(char), 80);
7577 ga_append(&ga, '{');
7578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007580 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 --todo;
7585
7586 if (first)
7587 first = FALSE;
7588 else
7589 ga_concat(&ga, (char_u *)", ");
7590
7591 tofree = string_quote(hi->hi_key, FALSE);
7592 if (tofree != NULL)
7593 {
7594 ga_concat(&ga, tofree);
7595 vim_free(tofree);
7596 }
7597 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 if (s != NULL)
7600 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007604 line_breakcheck();
7605
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (todo > 0)
7609 {
7610 vim_free(ga.ga_data);
7611 return NULL;
7612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 ga_append(&ga, '}');
7615 ga_append(&ga, NUL);
7616 return (char_u *)ga.ga_data;
7617}
7618
7619/*
7620 * Allocate a variable for a Dictionary and fill it from "*arg".
7621 * Return OK or FAIL. Returns NOTDONE for {expr}.
7622 */
7623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625{
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d = NULL;
7627 typval_T tvkey;
7628 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007629 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633
7634 /*
7635 * First check if it's not a curly-braces thing: {expr}.
7636 * Must do this without evaluating, otherwise a function may be called
7637 * twice. Unfortunately this means we need to call eval1() twice for the
7638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 if (*start != '}')
7642 {
7643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7644 return FAIL;
7645 if (*start == '}')
7646 return NOTDONE;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 if (evaluate)
7650 {
7651 d = dict_alloc();
7652 if (d == NULL)
7653 return FAIL;
7654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 tvkey.v_type = VAR_UNKNOWN;
7656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 *arg = skipwhite(*arg + 1);
7659 while (**arg != '}' && **arg != NUL)
7660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 if (**arg != ':')
7664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 goto failret;
7668 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007671 key = get_tv_string_buf_chk(&tvkey, buf);
7672 if (key == NULL || *key == NUL)
7673 {
7674 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7675 if (key != NULL)
7676 EMSG(_(e_emptykey));
7677 clear_tv(&tvkey);
7678 goto failret;
7679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 *arg = skipwhite(*arg + 1);
7683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7684 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007685 if (evaluate)
7686 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 }
7689 if (evaluate)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 if (item != NULL)
7693 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007694 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 clear_tv(&tv);
7697 goto failret;
7698 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 item = dictitem_alloc(key);
7700 clear_tv(&tvkey);
7701 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 if (dict_add(d, item) == FAIL)
7706 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 }
7708 }
7709
7710 if (**arg == '}')
7711 break;
7712 if (**arg != ',')
7713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007714 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
7717 *arg = skipwhite(*arg + 1);
7718 }
7719
7720 if (**arg != '}')
7721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007722 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723failret:
7724 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007725 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 return FAIL;
7727 }
7728
7729 *arg = skipwhite(*arg + 1);
7730 if (evaluate)
7731 {
7732 rettv->v_type = VAR_DICT;
7733 rettv->vval.v_dict = d;
7734 ++d->dv_refcount;
7735 }
7736
7737 return OK;
7738}
7739
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007740#if defined(FEAT_CHANNEL) || defined(PROTO)
7741/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007742 * Decrement the reference count on "channel" and maybe free it when it goes
7743 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007744 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 */
7746 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007747channel_unref(channel_T *channel)
7748{
7749 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007750 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007751 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007752}
7753#endif
7754
Bram Moolenaar65edff82016-02-21 16:40:11 +01007755#if defined(FEAT_JOB) || defined(PROTO)
7756static job_T *first_job = NULL;
7757
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 static void
7759job_free(job_T *job)
7760{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007761# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007762 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007763 if (job->jv_channel != NULL)
7764 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007765 /* The link from the channel to the job doesn't count as a reference,
7766 * thus don't decrement the refcount of the job. The reference from
7767 * the job to the channel does count the refrence, decrement it and
7768 * NULL the reference. We don't set ch_job_killed, unreferencing the
7769 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 job->jv_channel->ch_job = NULL;
7771 channel_unref(job->jv_channel);
7772 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007773# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007774 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007775
7776 if (job->jv_next != NULL)
7777 job->jv_next->jv_prev = job->jv_prev;
7778 if (job->jv_prev == NULL)
7779 first_job = job->jv_next;
7780 else
7781 job->jv_prev->jv_next = job->jv_next;
7782
7783 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007784 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 vim_free(job);
7786}
7787
7788 static void
7789job_unref(job_T *job)
7790{
7791 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007792 {
7793 /* Do not free the job when it has not ended yet and there is a
7794 * "stoponexit" flag or an exit callback. */
7795 if (job->jv_status != JOB_STARTED
7796 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007797 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007798 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007799 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007800# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007801 else if (job->jv_channel != NULL)
7802 {
7803 /* Do remove the link to the channel, otherwise it hangs
7804 * around until Vim exits. See job_free() for refcount. */
7805 job->jv_channel->ch_job = NULL;
7806 channel_unref(job->jv_channel);
7807 job->jv_channel = NULL;
7808 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007809# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007810 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007811}
7812
7813/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007814 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007815 */
7816 static job_T *
7817job_alloc(void)
7818{
7819 job_T *job;
7820
7821 job = (job_T *)alloc_clear(sizeof(job_T));
7822 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007823 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007824 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007825 job->jv_stoponexit = vim_strsave((char_u *)"term");
7826
7827 if (first_job != NULL)
7828 {
7829 first_job->jv_prev = job;
7830 job->jv_next = first_job;
7831 }
7832 first_job = job;
7833 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007834 return job;
7835}
7836
Bram Moolenaar65edff82016-02-21 16:40:11 +01007837 static void
7838job_set_options(job_T *job, jobopt_T *opt)
7839{
7840 if (opt->jo_set & JO_STOPONEXIT)
7841 {
7842 vim_free(job->jv_stoponexit);
7843 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7844 job->jv_stoponexit = NULL;
7845 else
7846 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7847 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007848 if (opt->jo_set & JO_EXIT_CB)
7849 {
7850 vim_free(job->jv_exit_cb);
7851 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7852 job->jv_exit_cb = NULL;
7853 else
7854 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7855 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007856}
7857
7858/*
7859 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7860 */
7861 void
7862job_stop_on_exit()
7863{
7864 job_T *job;
7865
7866 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007867 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007868 mch_stop_job(job, job->jv_stoponexit);
7869}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007870#endif
7871
Bram Moolenaar17a13432016-01-24 14:22:10 +01007872 static char *
7873get_var_special_name(int nr)
7874{
7875 switch (nr)
7876 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007877 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007878 case VVAL_TRUE: return "v:true";
7879 case VVAL_NONE: return "v:none";
7880 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007881 }
7882 EMSG2(_(e_intern2), "get_var_special_name()");
7883 return "42";
7884}
7885
Bram Moolenaar8c711452005-01-14 21:53:12 +00007886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887 * Return a string with the string representation of a variable.
7888 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007889 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007891 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007892 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 */
7894 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895echo_string(
7896 typval_T *tv,
7897 char_u **tofree,
7898 char_u *numbuf,
7899 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 static int recurse = 0;
7902 char_u *r = NULL;
7903
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007906 if (!did_echo_string_emsg)
7907 {
7908 /* Only give this message once for a recursive call to avoid
7909 * flooding the user with errors. And stop iterating over lists
7910 * and dicts. */
7911 did_echo_string_emsg = TRUE;
7912 EMSG(_("E724: variable nested too deep for displaying"));
7913 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007915 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 }
7917 ++recurse;
7918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 switch (tv->v_type)
7920 {
7921 case VAR_FUNC:
7922 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007923 r = tv->vval.v_string;
7924 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007927 if (tv->vval.v_list == NULL)
7928 {
7929 *tofree = NULL;
7930 r = NULL;
7931 }
7932 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7933 {
7934 *tofree = NULL;
7935 r = (char_u *)"[...]";
7936 }
7937 else
7938 {
7939 tv->vval.v_list->lv_copyID = copyID;
7940 *tofree = list2string(tv, copyID);
7941 r = *tofree;
7942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007944
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007946 if (tv->vval.v_dict == NULL)
7947 {
7948 *tofree = NULL;
7949 r = NULL;
7950 }
7951 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7952 {
7953 *tofree = NULL;
7954 r = (char_u *)"{...}";
7955 }
7956 else
7957 {
7958 tv->vval.v_dict->dv_copyID = copyID;
7959 *tofree = dict2string(tv, copyID);
7960 r = *tofree;
7961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 case VAR_STRING:
7965 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007966 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 *tofree = NULL;
7970 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 *tofree = NULL;
7976 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7977 r = numbuf;
7978 break;
7979#endif
7980
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007981 case VAR_SPECIAL:
7982 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007983 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007984 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986
Bram Moolenaar8502c702014-06-17 12:51:16 +02007987 if (--recurse == 0)
7988 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990}
7991
7992/*
7993 * Return a string with the string representation of a variable.
7994 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7995 * "numbuf" is used for a number.
7996 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007997 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 */
7999 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000tv2string(
8001 typval_T *tv,
8002 char_u **tofree,
8003 char_u *numbuf,
8004 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005{
8006 switch (tv->v_type)
8007 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008 case VAR_FUNC:
8009 *tofree = string_quote(tv->vval.v_string, TRUE);
8010 return *tofree;
8011 case VAR_STRING:
8012 *tofree = string_quote(tv->vval.v_string, FALSE);
8013 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008015#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008016 *tofree = NULL;
8017 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8018 return numbuf;
8019#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008023 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008024 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008025 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008026 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008028 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008029 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030}
8031
8032/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 * Return string "str" in ' quotes, doubling ' characters.
8034 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008036 */
8037 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039{
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008041 char_u *p, *r, *s;
8042
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 len = (function ? 13 : 3);
8044 if (str != NULL)
8045 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008046 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 for (p = str; *p != NUL; mb_ptr_adv(p))
8048 if (*p == '\'')
8049 ++len;
8050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008051 s = r = alloc(len);
8052 if (r != NULL)
8053 {
8054 if (function)
8055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008057 r += 10;
8058 }
8059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 if (str != NULL)
8062 for (p = str; *p != NUL; )
8063 {
8064 if (*p == '\'')
8065 *r++ = '\'';
8066 MB_COPY_CHAR(p, r);
8067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008069 if (function)
8070 *r++ = ')';
8071 *r++ = NUL;
8072 }
8073 return s;
8074}
8075
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008076#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077/*
8078 * Convert the string "text" to a floating point number.
8079 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8080 * this always uses a decimal point.
8081 * Returns the length of the text that was consumed.
8082 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084string2float(
8085 char_u *text,
8086 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087{
8088 char *s = (char *)text;
8089 float_T f;
8090
8091 f = strtod(s, &s);
8092 *value = f;
8093 return (int)((char_u *)s - text);
8094}
8095#endif
8096
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 * Get the value of an environment variable.
8099 * "arg" is pointing to the '$'. It is advanced to after the name.
8100 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008101 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 */
8103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008104get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 char_u *string = NULL;
8107 int len;
8108 int cc;
8109 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008110 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 ++*arg;
8113 name = *arg;
8114 len = get_env_len(arg);
8115 if (evaluate)
8116 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008117 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008118 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008119
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008120 cc = name[len];
8121 name[len] = NUL;
8122 /* first try vim_getenv(), fast for normal environment vars */
8123 string = vim_getenv(name, &mustfree);
8124 if (string != NULL && *string != NUL)
8125 {
8126 if (!mustfree)
8127 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008129 else
8130 {
8131 if (mustfree)
8132 vim_free(string);
8133
8134 /* next try expanding things like $VIM and ${HOME} */
8135 string = expand_env_save(name - 1);
8136 if (string != NULL && *string == '$')
8137 {
8138 vim_free(string);
8139 string = NULL;
8140 }
8141 }
8142 name[len] = cc;
8143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 rettv->v_type = VAR_STRING;
8145 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147
8148 return OK;
8149}
8150
8151/*
8152 * Array with names and number of arguments of all internal functions
8153 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8154 */
8155static struct fst
8156{
8157 char *f_name; /* function name */
8158 char f_min_argc; /* minimal number of arguments */
8159 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008160 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008161 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162} functions[] =
8163{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008168 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008169 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008170 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"append", 2, 2, f_append},
8172 {"argc", 0, 0, f_argc},
8173 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008174 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008175 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008176#ifdef FEAT_FLOAT
8177 {"asin", 1, 1, f_asin}, /* WJMc */
8178#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008179 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008180 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008181 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008182 {"assert_false", 1, 2, f_assert_false},
8183 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184#ifdef FEAT_FLOAT
8185 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008186 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"bufexists", 1, 1, f_bufexists},
8191 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8192 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8193 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8194 {"buflisted", 1, 1, f_buflisted},
8195 {"bufloaded", 1, 1, f_bufloaded},
8196 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008197 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"bufwinnr", 1, 1, f_bufwinnr},
8199 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008200 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008201 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
8204 {"ceil", 1, 1, f_ceil},
8205#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008206#ifdef FEAT_CHANNEL
8207 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008208 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8209 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008210 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008211# ifdef FEAT_JOB
8212 {"ch_getjob", 1, 1, f_ch_getjob},
8213# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008214 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008215 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008216 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008217 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008218 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008219 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8220 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008221 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008222 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008223#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008224 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008225 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008227 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008229#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008230 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008231 {"complete_add", 1, 1, f_complete_add},
8232 {"complete_check", 0, 0, f_complete_check},
8233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236#ifdef FEAT_FLOAT
8237 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008238 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008242 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008243 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008244 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008246 {"diff_filler", 1, 1, f_diff_filler},
8247 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008248 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008249 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008251 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"eventhandler", 0, 0, f_eventhandler},
8253 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008254 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008256#ifdef FEAT_FLOAT
8257 {"exp", 1, 1, f_exp},
8258#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008259 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008260 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008261 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8263 {"filereadable", 1, 1, f_filereadable},
8264 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008266 {"finddir", 1, 3, f_finddir},
8267 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268#ifdef FEAT_FLOAT
8269 {"float2nr", 1, 1, f_float2nr},
8270 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008271 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008272#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008273 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"fnamemodify", 2, 2, f_fnamemodify},
8275 {"foldclosed", 1, 1, f_foldclosed},
8276 {"foldclosedend", 1, 1, f_foldclosedend},
8277 {"foldlevel", 1, 1, f_foldlevel},
8278 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008279 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008282 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008283 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008284 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008285 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getchar", 0, 1, f_getchar},
8287 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008288 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"getcmdline", 0, 0, f_getcmdline},
8290 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008291 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008292 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008293 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008294 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008295 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008296 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"getfsize", 1, 1, f_getfsize},
8298 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008299 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008300 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008301 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008302 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008303 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008304 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008305 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008306 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008308 {"gettabvar", 2, 3, f_gettabvar},
8309 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"getwinposx", 0, 0, f_getwinposx},
8311 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008312 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008313 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008314 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008315 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008318 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008319 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8321 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8322 {"histadd", 2, 2, f_histadd},
8323 {"histdel", 1, 2, f_histdel},
8324 {"histget", 1, 2, f_histget},
8325 {"histnr", 1, 1, f_histnr},
8326 {"hlID", 1, 1, f_hlID},
8327 {"hlexists", 1, 1, f_hlexists},
8328 {"hostname", 0, 0, f_hostname},
8329 {"iconv", 3, 3, f_iconv},
8330 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008332 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008334 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"inputrestore", 0, 0, f_inputrestore},
8336 {"inputsave", 0, 0, f_inputsave},
8337 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008339 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008341 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008342#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8343 {"isnan", 1, 1, f_isnan},
8344#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008345 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008346#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008347# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008348 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008349# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008350 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008351 {"job_start", 1, 2, f_job_start},
8352 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008353 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008354#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008356 {"js_decode", 1, 1, f_js_decode},
8357 {"js_encode", 1, 1, f_js_encode},
8358 {"json_decode", 1, 1, f_json_decode},
8359 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"libcall", 3, 3, f_libcall},
8364 {"libcallnr", 3, 3, f_libcallnr},
8365 {"line", 1, 1, f_line},
8366 {"line2byte", 1, 1, f_line2byte},
8367 {"lispindent", 1, 1, f_lispindent},
8368 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 {"log10", 1, 1, f_log10},
8372#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008373#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008374 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008375#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008376 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008377 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008378 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008379 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008380 {"matchadd", 2, 5, f_matchadd},
8381 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008382 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008383 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008384 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008385 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008386 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008387 {"max", 1, 1, f_max},
8388 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008389#ifdef vim_mkdir
8390 {"mkdir", 1, 3, f_mkdir},
8391#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008392 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008393#ifdef FEAT_MZSCHEME
8394 {"mzeval", 1, 1, f_mzeval},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008397 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008398 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008399 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008400#ifdef FEAT_PERL
8401 {"perleval", 1, 1, f_perleval},
8402#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403#ifdef FEAT_FLOAT
8404 {"pow", 2, 2, f_pow},
8405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008407 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008409#ifdef FEAT_PYTHON3
8410 {"py3eval", 1, 1, f_py3eval},
8411#endif
8412#ifdef FEAT_PYTHON
8413 {"pyeval", 1, 1, f_pyeval},
8414#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008416 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008417 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008418#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008419 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008420#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008421 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"remote_expr", 2, 3, f_remote_expr},
8423 {"remote_foreground", 1, 1, f_remote_foreground},
8424 {"remote_peek", 1, 2, f_remote_peek},
8425 {"remote_read", 1, 1, f_remote_read},
8426 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008427 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008429 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008431 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008432#ifdef FEAT_FLOAT
8433 {"round", 1, 1, f_round},
8434#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008435 {"screenattr", 2, 2, f_screenattr},
8436 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008437 {"screencol", 0, 0, f_screencol},
8438 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008439 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008440 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008441 {"searchpair", 3, 7, f_searchpair},
8442 {"searchpairpos", 3, 7, f_searchpairpos},
8443 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"server2client", 2, 2, f_server2client},
8445 {"serverlist", 0, 0, f_serverlist},
8446 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008447 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"setcmdpos", 1, 1, f_setcmdpos},
8449 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008450 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008451 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008452 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008453 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008455 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008456 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008458#ifdef FEAT_CRYPT
8459 {"sha256", 1, 1, f_sha256},
8460#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008461 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008462 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464#ifdef FEAT_FLOAT
8465 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008466 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008467#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008468 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008469 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008470 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008471 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008472 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#ifdef FEAT_FLOAT
8474 {"sqrt", 1, 1, f_sqrt},
8475 {"str2float", 1, 1, f_str2float},
8476#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008477 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008478 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008479 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480#ifdef HAVE_STRFTIME
8481 {"strftime", 1, 2, f_strftime},
8482#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {"strlen", 1, 1, f_strlen},
8486 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008487 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008489 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008490 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {"substitute", 4, 4, f_substitute},
8492 {"synID", 3, 3, f_synID},
8493 {"synIDattr", 2, 3, f_synIDattr},
8494 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008495 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008496 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008497 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008498 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008499 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008500 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008502 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008503 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008504#ifdef FEAT_FLOAT
8505 {"tan", 1, 1, f_tan},
8506 {"tanh", 1, 1, f_tanh},
8507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008509 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"tolower", 1, 1, f_tolower},
8511 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008512 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008513#ifdef FEAT_FLOAT
8514 {"trunc", 1, 1, f_trunc},
8515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008517 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008518 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008519 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008520 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {"virtcol", 1, 1, f_virtcol},
8522 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008523 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {"winbufnr", 1, 1, f_winbufnr},
8525 {"wincol", 0, 0, f_wincol},
8526 {"winheight", 1, 1, f_winheight},
8527 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008528 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008530 {"winrestview", 1, 1, f_winrestview},
8531 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008533 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008534 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008535 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536};
8537
8538#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8539
8540/*
8541 * Function given to ExpandGeneric() to obtain the list of internal
8542 * or user defined function names.
8543 */
8544 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008545get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 static int intidx = -1;
8548 char_u *name;
8549
8550 if (idx == 0)
8551 intidx = -1;
8552 if (intidx < 0)
8553 {
8554 name = get_user_func_name(xp, idx);
8555 if (name != NULL)
8556 return name;
8557 }
8558 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8559 {
8560 STRCPY(IObuff, functions[intidx].f_name);
8561 STRCAT(IObuff, "(");
8562 if (functions[intidx].f_max_argc == 0)
8563 STRCAT(IObuff, ")");
8564 return IObuff;
8565 }
8566
8567 return NULL;
8568}
8569
8570/*
8571 * Function given to ExpandGeneric() to obtain the list of internal or
8572 * user defined variable or function names.
8573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008575get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 static int intidx = -1;
8578 char_u *name;
8579
8580 if (idx == 0)
8581 intidx = -1;
8582 if (intidx < 0)
8583 {
8584 name = get_function_name(xp, idx);
8585 if (name != NULL)
8586 return name;
8587 }
8588 return get_user_var_name(xp, ++intidx);
8589}
8590
8591#endif /* FEAT_CMDL_COMPL */
8592
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008593#if defined(EBCDIC) || defined(PROTO)
8594/*
8595 * Compare struct fst by function name.
8596 */
8597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008598compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008599{
8600 struct fst *p1 = (struct fst *)s1;
8601 struct fst *p2 = (struct fst *)s2;
8602
8603 return STRCMP(p1->f_name, p2->f_name);
8604}
8605
8606/*
8607 * Sort the function table by function name.
8608 * The sorting of the table above is ASCII dependant.
8609 * On machines using EBCDIC we have to sort it.
8610 */
8611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008612sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008613{
8614 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8615
8616 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8617}
8618#endif
8619
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*
8622 * Find internal function in table above.
8623 * Return index, or -1 if not found
8624 */
8625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008626find_internal_func(
8627 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 int first = 0;
8630 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8631 int cmp;
8632 int x;
8633
8634 /*
8635 * Find the function name in the table. Binary search.
8636 */
8637 while (first <= last)
8638 {
8639 x = first + ((unsigned)(last - first) >> 1);
8640 cmp = STRCMP(name, functions[x].f_name);
8641 if (cmp < 0)
8642 last = x - 1;
8643 else if (cmp > 0)
8644 first = x + 1;
8645 else
8646 return x;
8647 }
8648 return -1;
8649}
8650
8651/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8653 * name it contains, otherwise return "name".
8654 */
8655 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008656deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008657{
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 int cc;
8660
8661 cc = name[*lenp];
8662 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008663 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 {
8669 *lenp = 0;
8670 return (char_u *)""; /* just in case */
8671 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008672 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008673 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674 }
8675
8676 return name;
8677}
8678
8679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 * Allocate a variable for the result of a function.
8681 * Return OK or FAIL.
8682 */
8683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008684get_func_tv(
8685 char_u *name, /* name of the function */
8686 int len, /* length of "name" */
8687 typval_T *rettv,
8688 char_u **arg, /* argument, pointing to the '(' */
8689 linenr_T firstline, /* first line of range */
8690 linenr_T lastline, /* last line of range */
8691 int *doesrange, /* return: function handled range */
8692 int evaluate,
8693 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694{
8695 char_u *argp;
8696 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008697 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 int argcount = 0; /* number of arguments found */
8699
8700 /*
8701 * Get the arguments.
8702 */
8703 argp = *arg;
8704 while (argcount < MAX_FUNC_ARGS)
8705 {
8706 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8707 if (*argp == ')' || *argp == ',' || *argp == NUL)
8708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8710 {
8711 ret = FAIL;
8712 break;
8713 }
8714 ++argcount;
8715 if (*argp != ',')
8716 break;
8717 }
8718 if (*argp == ')')
8719 ++argp;
8720 else
8721 ret = FAIL;
8722
8723 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008725 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 {
8728 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008729 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008731 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
8734 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008735 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737 *arg = skipwhite(argp);
8738 return ret;
8739}
8740
8741
8742/*
8743 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008744 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008745 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748call_func(
8749 char_u *funcname, /* name of the function */
8750 int len, /* length of "name" */
8751 typval_T *rettv, /* return value goes here */
8752 int argcount, /* number of "argvars" */
8753 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008754 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008755 linenr_T firstline, /* first line of range */
8756 linenr_T lastline, /* last line of range */
8757 int *doesrange, /* return: function handled range */
8758 int evaluate,
8759 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762#define ERROR_UNKNOWN 0
8763#define ERROR_TOOMANY 1
8764#define ERROR_TOOFEW 2
8765#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766#define ERROR_DICT 4
8767#define ERROR_NONE 5
8768#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 int error = ERROR_NONE;
8770 int i;
8771 int llen;
8772 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773#define FLEN_FIXED 40
8774 char_u fname_buf[FLEN_FIXED + 1];
8775 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008776 char_u *name;
8777
8778 /* Make a copy of the name, if it comes from a funcref variable it could
8779 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008780 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008781 if (name == NULL)
8782 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 /*
8785 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8786 * Change <SNR>123_name() to K_SNR 123_name().
8787 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 llen = eval_fname_script(name);
8790 if (llen > 0)
8791 {
8792 fname_buf[0] = K_SPECIAL;
8793 fname_buf[1] = KS_EXTRA;
8794 fname_buf[2] = (int)KE_SNR;
8795 i = 3;
8796 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8797 {
8798 if (current_SID <= 0)
8799 error = ERROR_SCRIPT;
8800 else
8801 {
8802 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8803 i = (int)STRLEN(fname_buf);
8804 }
8805 }
8806 if (i + STRLEN(name + llen) < FLEN_FIXED)
8807 {
8808 STRCPY(fname_buf + i, name + llen);
8809 fname = fname_buf;
8810 }
8811 else
8812 {
8813 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8814 if (fname == NULL)
8815 error = ERROR_OTHER;
8816 else
8817 {
8818 mch_memmove(fname, fname_buf, (size_t)i);
8819 STRCPY(fname + i, name + llen);
8820 }
8821 }
8822 }
8823 else
8824 fname = name;
8825
8826 *doesrange = FALSE;
8827
8828
8829 /* execute the function if no errors detected and executing */
8830 if (evaluate && error == ERROR_NONE)
8831 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008832 char_u *rfname = fname;
8833
8834 /* Ignore "g:" before a function name. */
8835 if (fname[0] == 'g' && fname[1] == ':')
8836 rfname = fname + 2;
8837
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008838 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8839 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 error = ERROR_UNKNOWN;
8841
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008842 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
8844 /*
8845 * User defined function.
8846 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008847 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 /* Trigger FuncUndefined event, may load the function. */
8851 if (fp == NULL
8852 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008853 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008854 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008857 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 }
8859#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008860 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008861 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008862 {
8863 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008864 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008865 }
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 if (fp != NULL)
8868 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008869 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008875 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008876 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 else
8878 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008879 int did_save_redo = FALSE;
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 /*
8882 * Call the user function.
8883 * Save and restore search patterns, script variables and
8884 * redo buffer.
8885 */
8886 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008887#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008888 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008889#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 {
8891 saveRedobuff();
8892 did_save_redo = TRUE;
8893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008894 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008896 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008897 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8898 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8899 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008900 /* Function was unreferenced while being used, free it
8901 * now. */
8902 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008903 if (did_save_redo)
8904 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 restore_search_patterns();
8906 error = ERROR_NONE;
8907 }
8908 }
8909 }
8910 else
8911 {
8912 /*
8913 * Find the function name in the table, call its implementation.
8914 */
8915 i = find_internal_func(fname);
8916 if (i >= 0)
8917 {
8918 if (argcount < functions[i].f_min_argc)
8919 error = ERROR_TOOFEW;
8920 else if (argcount > functions[i].f_max_argc)
8921 error = ERROR_TOOMANY;
8922 else
8923 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008924 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 error = ERROR_NONE;
8927 }
8928 }
8929 }
8930 /*
8931 * The function call (or "FuncUndefined" autocommand sequence) might
8932 * have been aborted by an error, an interrupt, or an explicitly thrown
8933 * exception that has not been caught so far. This situation can be
8934 * tested for by calling aborting(). For an error in an internal
8935 * function or for the "E132" error in call_user_func(), however, the
8936 * throw point at which the "force_abort" flag (temporarily reset by
8937 * emsg()) is normally updated has not been reached yet. We need to
8938 * update that flag first to make aborting() reliable.
8939 */
8940 update_force_abort();
8941 }
8942 if (error == ERROR_NONE)
8943 ret = OK;
8944
8945 /*
8946 * Report an error unless the argument evaluation or function call has been
8947 * cancelled due to an aborting error, an interrupt, or an exception.
8948 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 if (!aborting())
8950 {
8951 switch (error)
8952 {
8953 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008954 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008955 break;
8956 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008957 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008958 break;
8959 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008960 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008961 name);
8962 break;
8963 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008964 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008965 name);
8966 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008968 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 name);
8970 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008971 }
8972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (fname != name && fname != fname_buf)
8975 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008976 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978 return ret;
8979}
8980
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008981/*
8982 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008983 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008984 */
8985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008986emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008987{
8988 char_u *p;
8989
8990 if (*name == K_SPECIAL)
8991 p = concat_str((char_u *)"<SNR>", name + 3);
8992 else
8993 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008994 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008995 if (p != name)
8996 vim_free(p);
8997}
8998
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008999/*
9000 * Return TRUE for a non-zero Number and a non-empty String.
9001 */
9002 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009003non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009004{
9005 return ((argvars[0].v_type == VAR_NUMBER
9006 && argvars[0].vval.v_number != 0)
9007 || (argvars[0].v_type == VAR_STRING
9008 && argvars[0].vval.v_string != NULL
9009 && *argvars[0].vval.v_string != NUL));
9010}
9011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012/*********************************************
9013 * Implementation of the built-in functions
9014 */
9015
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009017static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009018
9019/*
9020 * Get the float value of "argvars[0]" into "f".
9021 * Returns FAIL when the argument is not a Number or Float.
9022 */
9023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009025{
9026 if (argvars[0].v_type == VAR_FLOAT)
9027 {
9028 *f = argvars[0].vval.v_float;
9029 return OK;
9030 }
9031 if (argvars[0].v_type == VAR_NUMBER)
9032 {
9033 *f = (float_T)argvars[0].vval.v_number;
9034 return OK;
9035 }
9036 EMSG(_("E808: Number or Float required"));
9037 return FAIL;
9038}
9039
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009040/*
9041 * "abs(expr)" function
9042 */
9043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009044f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009045{
9046 if (argvars[0].v_type == VAR_FLOAT)
9047 {
9048 rettv->v_type = VAR_FLOAT;
9049 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9050 }
9051 else
9052 {
9053 varnumber_T n;
9054 int error = FALSE;
9055
9056 n = get_tv_number_chk(&argvars[0], &error);
9057 if (error)
9058 rettv->vval.v_number = -1;
9059 else if (n > 0)
9060 rettv->vval.v_number = n;
9061 else
9062 rettv->vval.v_number = -n;
9063 }
9064}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009065
9066/*
9067 * "acos()" function
9068 */
9069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009070f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009071{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009072 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009073
9074 rettv->v_type = VAR_FLOAT;
9075 if (get_float_arg(argvars, &f) == OK)
9076 rettv->vval.v_float = acos(f);
9077 else
9078 rettv->vval.v_float = 0.0;
9079}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009080#endif
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009083 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 */
9085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009086f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009091 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009093 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009094 && !tv_check_lock(l->lv_lock,
9095 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009096 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098 }
9099 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009100 EMSG(_(e_listreq));
9101}
9102
9103/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009104 * "alloc_fail(id, countdown, repeat)" function
9105 */
9106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009107f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009108{
9109 if (argvars[0].v_type != VAR_NUMBER
9110 || argvars[0].vval.v_number <= 0
9111 || argvars[1].v_type != VAR_NUMBER
9112 || argvars[1].vval.v_number < 0
9113 || argvars[2].v_type != VAR_NUMBER)
9114 EMSG(_(e_invarg));
9115 else
9116 {
9117 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009118 if (alloc_fail_id >= aid_last)
9119 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009120 alloc_fail_countdown = argvars[1].vval.v_number;
9121 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009122 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009123 }
9124}
9125
9126/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009127 * "and(expr, expr)" function
9128 */
9129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009130f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009131{
9132 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9133 & get_tv_number_chk(&argvars[1], NULL);
9134}
9135
9136/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009137 * "append(lnum, string/list)" function
9138 */
9139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009140f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009141{
9142 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009144 list_T *l = NULL;
9145 listitem_T *li = NULL;
9146 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 long added = 0;
9148
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009149 /* When coming here from Insert mode, sync undo, so that this can be
9150 * undone separately from what was previously inserted. */
9151 if (u_sync_once == 2)
9152 {
9153 u_sync_once = 1; /* notify that u_sync() was called */
9154 u_sync(TRUE);
9155 }
9156
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157 lnum = get_tv_lnum(argvars);
9158 if (lnum >= 0
9159 && lnum <= curbuf->b_ml.ml_line_count
9160 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009161 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009162 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 l = argvars[1].vval.v_list;
9165 if (l == NULL)
9166 return;
9167 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009168 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009169 for (;;)
9170 {
9171 if (l == NULL)
9172 tv = &argvars[1]; /* append a string */
9173 else if (li == NULL)
9174 break; /* end of list */
9175 else
9176 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 line = get_tv_string_chk(tv);
9178 if (line == NULL) /* type error */
9179 {
9180 rettv->vval.v_number = 1; /* Failed */
9181 break;
9182 }
9183 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184 ++added;
9185 if (l == NULL)
9186 break;
9187 li = li->li_next;
9188 }
9189
9190 appended_lines_mark(lnum, added);
9191 if (curwin->w_cursor.lnum > lnum)
9192 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009194 else
9195 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196}
9197
9198/*
9199 * "argc()" function
9200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009202f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
9207/*
9208 * "argidx()" function
9209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009211f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009213 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214}
9215
9216/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009217 * "arglistid()" function
9218 */
9219 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009220f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009221{
9222 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009223
9224 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009225 wp = find_tabwin(&argvars[0], &argvars[1]);
9226 if (wp != NULL)
9227 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009228}
9229
9230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 * "argv(nr)" function
9232 */
9233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009234f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235{
9236 int idx;
9237
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009238 if (argvars[0].v_type != VAR_UNKNOWN)
9239 {
9240 idx = get_tv_number_chk(&argvars[0], NULL);
9241 if (idx >= 0 && idx < ARGCOUNT)
9242 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9243 else
9244 rettv->vval.v_string = NULL;
9245 rettv->v_type = VAR_STRING;
9246 }
9247 else if (rettv_list_alloc(rettv) == OK)
9248 for (idx = 0; idx < ARGCOUNT; ++idx)
9249 list_append_string(rettv->vval.v_list,
9250 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251}
9252
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009253static void prepare_assert_error(garray_T*gap);
9254static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9255static void assert_error(garray_T *gap);
9256static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009257
9258/*
9259 * Prepare "gap" for an assert error and add the sourcing position.
9260 */
9261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009262prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009263{
9264 char buf[NUMBUFLEN];
9265
9266 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009267 if (sourcing_name != NULL)
9268 {
9269 ga_concat(gap, sourcing_name);
9270 if (sourcing_lnum > 0)
9271 ga_concat(gap, (char_u *)" ");
9272 }
9273 if (sourcing_lnum > 0)
9274 {
9275 sprintf(buf, "line %ld", (long)sourcing_lnum);
9276 ga_concat(gap, (char_u *)buf);
9277 }
9278 if (sourcing_name != NULL || sourcing_lnum > 0)
9279 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009280}
9281
9282/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009283 * Append "str" to "gap", escaping unprintable characters.
9284 * Changes NL to \n, CR to \r, etc.
9285 */
9286 static void
9287ga_concat_esc(garray_T *gap, char_u *str)
9288{
9289 char_u *p;
9290 char_u buf[NUMBUFLEN];
9291
9292 for (p = str; *p != NUL; ++p)
9293 switch (*p)
9294 {
9295 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9296 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9297 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9298 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9299 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9300 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9301 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9302 default:
9303 if (*p < ' ')
9304 {
9305 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9306 ga_concat(gap, buf);
9307 }
9308 else
9309 ga_append(gap, *p);
9310 break;
9311 }
9312}
9313
9314/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 * Fill "gap" with information about an assert error.
9316 */
9317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009318fill_assert_error(
9319 garray_T *gap,
9320 typval_T *opt_msg_tv,
9321 char_u *exp_str,
9322 typval_T *exp_tv,
9323 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324{
9325 char_u numbuf[NUMBUFLEN];
9326 char_u *tofree;
9327
9328 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9329 {
9330 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9331 vim_free(tofree);
9332 }
9333 else
9334 {
9335 ga_concat(gap, (char_u *)"Expected ");
9336 if (exp_str == NULL)
9337 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009338 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009339 vim_free(tofree);
9340 }
9341 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009342 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009344 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009345 vim_free(tofree);
9346 }
9347}
Bram Moolenaar43345542015-11-29 17:35:35 +01009348
9349/*
9350 * Add an assert error to v:errors.
9351 */
9352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009354{
9355 struct vimvar *vp = &vimvars[VV_ERRORS];
9356
9357 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9358 /* Make sure v:errors is a list. */
9359 set_vim_var_list(VV_ERRORS, list_alloc());
9360 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9361}
9362
Bram Moolenaar43345542015-11-29 17:35:35 +01009363/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009364 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009365 */
9366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009367f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009368{
9369 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009370
9371 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9372 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373 prepare_assert_error(&ga);
9374 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9375 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009376 ga_clear(&ga);
9377 }
9378}
9379
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009380/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009381 * "assert_exception(string[, msg])" function
9382 */
9383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009384f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009385{
9386 garray_T ga;
9387 char *error;
9388
9389 error = (char *)get_tv_string_chk(&argvars[0]);
9390 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9391 {
9392 prepare_assert_error(&ga);
9393 ga_concat(&ga, (char_u *)"v:exception is not set");
9394 assert_error(&ga);
9395 ga_clear(&ga);
9396 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009397 else if (error != NULL
9398 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009399 {
9400 prepare_assert_error(&ga);
9401 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9402 &vimvars[VV_EXCEPTION].vv_tv);
9403 assert_error(&ga);
9404 ga_clear(&ga);
9405 }
9406}
9407
9408/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009409 * "assert_fails(cmd [, error])" function
9410 */
9411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009412f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009413{
9414 char_u *cmd = get_tv_string_chk(&argvars[0]);
9415 garray_T ga;
9416
9417 called_emsg = FALSE;
9418 suppress_errthrow = TRUE;
9419 emsg_silent = TRUE;
9420 do_cmdline_cmd(cmd);
9421 if (!called_emsg)
9422 {
9423 prepare_assert_error(&ga);
9424 ga_concat(&ga, (char_u *)"command did not fail: ");
9425 ga_concat(&ga, cmd);
9426 assert_error(&ga);
9427 ga_clear(&ga);
9428 }
9429 else if (argvars[1].v_type != VAR_UNKNOWN)
9430 {
9431 char_u buf[NUMBUFLEN];
9432 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9433
9434 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9435 {
9436 prepare_assert_error(&ga);
9437 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9438 &vimvars[VV_ERRMSG].vv_tv);
9439 assert_error(&ga);
9440 ga_clear(&ga);
9441 }
9442 }
9443
9444 called_emsg = FALSE;
9445 suppress_errthrow = FALSE;
9446 emsg_silent = FALSE;
9447 emsg_on_display = FALSE;
9448 set_vim_var_string(VV_ERRMSG, NULL, 0);
9449}
9450
9451/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009452 * Common for assert_true() and assert_false().
9453 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009455assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009456{
9457 int error = FALSE;
9458 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009459
Bram Moolenaar37127922016-02-06 20:29:28 +01009460 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009461 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009462 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 if (argvars[0].v_type != VAR_NUMBER
9464 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9465 || error)
9466 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467 prepare_assert_error(&ga);
9468 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009469 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009470 NULL, &argvars[0]);
9471 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009472 ga_clear(&ga);
9473 }
9474}
9475
9476/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009477 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009478 */
9479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009480f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009481{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009482 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009483}
9484
9485/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009486 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009487 */
9488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009489f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009490{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009491 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009492}
9493
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009494#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009496 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009497 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009500{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009501 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = asin(f);
9506 else
9507 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009508}
9509
9510/*
9511 * "atan()" function
9512 */
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009515{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009516 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009517
9518 rettv->v_type = VAR_FLOAT;
9519 if (get_float_arg(argvars, &f) == OK)
9520 rettv->vval.v_float = atan(f);
9521 else
9522 rettv->vval.v_float = 0.0;
9523}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009524
9525/*
9526 * "atan2()" function
9527 */
9528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009529f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009530{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009531 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009532
9533 rettv->v_type = VAR_FLOAT;
9534 if (get_float_arg(argvars, &fx) == OK
9535 && get_float_arg(&argvars[1], &fy) == OK)
9536 rettv->vval.v_float = atan2(fx, fy);
9537 else
9538 rettv->vval.v_float = 0.0;
9539}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009540#endif
9541
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542/*
9543 * "browse(save, title, initdir, default)" function
9544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548#ifdef FEAT_BROWSE
9549 int save;
9550 char_u *title;
9551 char_u *initdir;
9552 char_u *defname;
9553 char_u buf[NUMBUFLEN];
9554 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 save = get_tv_number_chk(&argvars[0], &error);
9558 title = get_tv_string_chk(&argvars[1]);
9559 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9560 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 if (error || title == NULL || initdir == NULL || defname == NULL)
9563 rettv->vval.v_string = NULL;
9564 else
9565 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009566 do_browse(save ? BROWSE_SAVE : 0,
9567 title, defname, NULL, initdir, NULL, curbuf);
9568#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009570#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572}
9573
9574/*
9575 * "browsedir(title, initdir)" function
9576 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009578f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579{
9580#ifdef FEAT_BROWSE
9581 char_u *title;
9582 char_u *initdir;
9583 char_u buf[NUMBUFLEN];
9584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 title = get_tv_string_chk(&argvars[0]);
9586 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009588 if (title == NULL || initdir == NULL)
9589 rettv->vval.v_string = NULL;
9590 else
9591 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009592 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597}
9598
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009599static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009600
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601/*
9602 * Find a buffer by number or exact name.
9603 */
9604 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009605find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606{
9607 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009609 if (avar->v_type == VAR_NUMBER)
9610 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009611 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009614 if (buf == NULL)
9615 {
9616 /* No full path name match, try a match with a URL or a "nofile"
9617 * buffer, these don't use the full path. */
9618 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9619 if (buf->b_fname != NULL
9620 && (path_with_url(buf->b_fname)
9621#ifdef FEAT_QUICKFIX
9622 || bt_nofile(buf)
9623#endif
9624 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009625 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009626 break;
9627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 }
9629 return buf;
9630}
9631
9632/*
9633 * "bufexists(expr)" function
9634 */
9635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009636f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
9641/*
9642 * "buflisted(expr)" function
9643 */
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 buf_T *buf;
9648
9649 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
9653/*
9654 * "bufloaded(expr)" function
9655 */
9656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009657f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659 buf_T *buf;
9660
9661 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009662 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 static buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009666buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 int save_magic;
9669 char_u *save_cpo;
9670 buf_T *buf;
9671
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9673 save_magic = p_magic;
9674 p_magic = TRUE;
9675 save_cpo = p_cpo;
9676 p_cpo = (char_u *)"";
9677
9678 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009679 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680
9681 p_magic = save_magic;
9682 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009683 return buf;
9684}
9685
9686/*
9687 * Get buffer by number or pattern.
9688 */
9689 static buf_T *
9690get_buf_tv(typval_T *tv, int curtab_only)
9691{
9692 char_u *name = tv->vval.v_string;
9693 buf_T *buf;
9694
9695 if (tv->v_type == VAR_NUMBER)
9696 return buflist_findnr((int)tv->vval.v_number);
9697 if (tv->v_type != VAR_STRING)
9698 return NULL;
9699 if (name == NULL || *name == NUL)
9700 return curbuf;
9701 if (name[0] == '$' && name[1] == NUL)
9702 return lastbuf;
9703
9704 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705
9706 /* If not found, try expanding the name, like done for bufexists(). */
9707 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
9710 return buf;
9711}
9712
9713/*
9714 * "bufname(expr)" function
9715 */
9716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 buf_T *buf;
9720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009721 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009723 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 --emsg_off;
9730}
9731
9732/*
9733 * "bufnr(expr)" function
9734 */
9735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009736f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009739 int error = FALSE;
9740 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009744 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009745 --emsg_off;
9746
9747 /* If the buffer isn't found and the second argument is not zero create a
9748 * new buffer. */
9749 if (buf == NULL
9750 && argvars[1].v_type != VAR_UNKNOWN
9751 && get_tv_number_chk(&argvars[1], &error) != 0
9752 && !error
9753 && (name = get_tv_string_chk(&argvars[0])) != NULL
9754 && !error)
9755 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9756
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761}
9762
9763/*
9764 * "bufwinnr(nr)" function
9765 */
9766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009767f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
9769#ifdef FEAT_WINDOWS
9770 win_T *wp;
9771 int winnr = 0;
9772#endif
9773 buf_T *buf;
9774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009777 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778#ifdef FEAT_WINDOWS
9779 for (wp = firstwin; wp; wp = wp->w_next)
9780 {
9781 ++winnr;
9782 if (wp->w_buffer == buf)
9783 break;
9784 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009785 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#endif
9789 --emsg_off;
9790}
9791
9792/*
9793 * "byte2line(byte)" function
9794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009796f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009799 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800#else
9801 long boff = 0;
9802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 (linenr_T)0, &boff);
9809#endif
9810}
9811
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009813byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814{
9815#ifdef FEAT_MBYTE
9816 char_u *t;
9817#endif
9818 char_u *str;
9819 long idx;
9820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009821 str = get_tv_string_chk(&argvars[0]);
9822 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009825 return;
9826
9827#ifdef FEAT_MBYTE
9828 t = str;
9829 for ( ; idx > 0; idx--)
9830 {
9831 if (*t == NUL) /* EOL reached */
9832 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009833 if (enc_utf8 && comp)
9834 t += utf_ptr2len(t);
9835 else
9836 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009837 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009838 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009839#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009842#endif
9843}
9844
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009845/*
9846 * "byteidx()" function
9847 */
9848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009849f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009850{
9851 byteidx(argvars, rettv, FALSE);
9852}
9853
9854/*
9855 * "byteidxcomp()" function
9856 */
9857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009858f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009859{
9860 byteidx(argvars, rettv, TRUE);
9861}
9862
Bram Moolenaardb913952012-06-29 12:54:53 +02009863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009864func_call(
9865 char_u *name,
9866 typval_T *args,
9867 dict_T *selfdict,
9868 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009869{
9870 listitem_T *item;
9871 typval_T argv[MAX_FUNC_ARGS + 1];
9872 int argc = 0;
9873 int dummy;
9874 int r = 0;
9875
9876 for (item = args->vval.v_list->lv_first; item != NULL;
9877 item = item->li_next)
9878 {
9879 if (argc == MAX_FUNC_ARGS)
9880 {
9881 EMSG(_("E699: Too many arguments"));
9882 break;
9883 }
9884 /* Make a copy of each argument. This is needed to be able to set
9885 * v_lock to VAR_FIXED in the copy without changing the original list.
9886 */
9887 copy_tv(&item->li_tv, &argv[argc++]);
9888 }
9889
9890 if (item == NULL)
9891 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9892 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9893 &dummy, TRUE, selfdict);
9894
9895 /* Free the arguments. */
9896 while (argc > 0)
9897 clear_tv(&argv[--argc]);
9898
9899 return r;
9900}
9901
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009902/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009903 * "call(func, arglist)" function
9904 */
9905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009906f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907{
9908 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009909 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009911 if (argvars[1].v_type != VAR_LIST)
9912 {
9913 EMSG(_(e_listreq));
9914 return;
9915 }
9916 if (argvars[1].vval.v_list == NULL)
9917 return;
9918
9919 if (argvars[0].v_type == VAR_FUNC)
9920 func = argvars[0].vval.v_string;
9921 else
9922 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 if (*func == NUL)
9924 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925
Bram Moolenaare9a41262005-01-15 22:18:47 +00009926 if (argvars[2].v_type != VAR_UNKNOWN)
9927 {
9928 if (argvars[2].v_type != VAR_DICT)
9929 {
9930 EMSG(_(e_dictreq));
9931 return;
9932 }
9933 selfdict = argvars[2].vval.v_dict;
9934 }
9935
Bram Moolenaardb913952012-06-29 12:54:53 +02009936 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009937}
9938
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009939#ifdef FEAT_FLOAT
9940/*
9941 * "ceil({float})" function
9942 */
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009945{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009946 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947
9948 rettv->v_type = VAR_FLOAT;
9949 if (get_float_arg(argvars, &f) == OK)
9950 rettv->vval.v_float = ceil(f);
9951 else
9952 rettv->vval.v_float = 0.0;
9953}
9954#endif
9955
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009956#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009957/*
9958 * Get a callback from "arg". It can be a Funcref or a function name.
9959 * When "arg" is zero return an empty string.
9960 * Return NULL for an invalid argument.
9961 */
9962 static char_u *
9963get_callback(typval_T *arg)
9964{
9965 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9966 return arg->vval.v_string;
9967 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9968 return (char_u *)"";
9969 EMSG(_("E999: Invalid callback argument"));
9970 return NULL;
9971}
9972
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009973 static int
9974handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9975{
9976 char_u *val = get_tv_string(item);
9977
9978 opt->jo_set |= jo;
9979 if (STRCMP(val, "nl") == 0)
9980 *modep = MODE_NL;
9981 else if (STRCMP(val, "raw") == 0)
9982 *modep = MODE_RAW;
9983 else if (STRCMP(val, "js") == 0)
9984 *modep = MODE_JS;
9985 else if (STRCMP(val, "json") == 0)
9986 *modep = MODE_JSON;
9987 else
9988 {
9989 EMSG2(_(e_invarg2), val);
9990 return FAIL;
9991 }
9992 return OK;
9993}
9994
Bram Moolenaar187db502016-02-27 14:44:26 +01009995 static int
9996handle_io(typval_T *item, int part, jobopt_T *opt)
9997{
9998 char_u *val = get_tv_string(item);
9999
10000 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
10001 if (STRCMP(val, "null") == 0)
10002 opt->jo_io[part] = JIO_NULL;
10003 else if (STRCMP(val, "pipe") == 0)
10004 opt->jo_io[part] = JIO_PIPE;
10005 else if (STRCMP(val, "file") == 0)
10006 opt->jo_io[part] = JIO_FILE;
10007 else if (STRCMP(val, "buffer") == 0)
10008 opt->jo_io[part] = JIO_BUFFER;
10009 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10010 opt->jo_io[part] = JIO_OUT;
10011 else
10012 {
10013 EMSG2(_(e_invarg2), val);
10014 return FAIL;
10015 }
10016 return OK;
10017}
10018
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010019 static void
10020clear_job_options(jobopt_T *opt)
10021{
10022 vim_memset(opt, 0, sizeof(jobopt_T));
10023}
10024
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010025/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010026 * Get the PART_ number from the first character of an option name.
10027 */
10028 static int
10029part_from_char(int c)
10030{
10031 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10032}
10033
10034/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010035 * Get the option entries from the dict in "tv", parse them and put the result
10036 * in "opt".
10037 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010038 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010039 */
10040 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010041get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010042{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010044 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010045 dict_T *dict;
10046 int todo;
10047 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010048 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010049
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010050 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010051 if (tv->v_type == VAR_UNKNOWN)
10052 return OK;
10053 if (tv->v_type != VAR_DICT)
10054 {
10055 EMSG(_(e_invarg));
10056 return FAIL;
10057 }
10058 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010059 if (dict == NULL)
10060 return OK;
10061
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010062 todo = (int)dict->dv_hashtab.ht_used;
10063 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10064 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010065 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010066 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010067
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010068 if (STRCMP(hi->hi_key, "mode") == 0)
10069 {
10070 if (!(supported & JO_MODE))
10071 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010072 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010073 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010074 }
10075 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10076 {
10077 if (!(supported & JO_IN_MODE))
10078 break;
10079 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10080 == FAIL)
10081 return FAIL;
10082 }
10083 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10084 {
10085 if (!(supported & JO_OUT_MODE))
10086 break;
10087 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10088 == FAIL)
10089 return FAIL;
10090 }
10091 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10092 {
10093 if (!(supported & JO_ERR_MODE))
10094 break;
10095 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10096 == FAIL)
10097 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010098 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010099 else if (STRCMP(hi->hi_key, "in-io") == 0
10100 || STRCMP(hi->hi_key, "out-io") == 0
10101 || STRCMP(hi->hi_key, "err-io") == 0)
10102 {
10103 if (!(supported & JO_OUT_IO))
10104 break;
10105 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10106 return FAIL;
10107 }
10108 else if (STRCMP(hi->hi_key, "in-name") == 0
10109 || STRCMP(hi->hi_key, "out-name") == 0
10110 || STRCMP(hi->hi_key, "err-name") == 0)
10111 {
10112 part = part_from_char(*hi->hi_key);
10113
10114 if (!(supported & JO_OUT_IO))
10115 break;
10116 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10117 opt->jo_io_name[part] =
10118 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10119 }
Bram Moolenaar014069a2016-03-03 22:51:40 +010010120 else if (STRCMP(hi->hi_key, "in-top") == 0
10121 || STRCMP(hi->hi_key, "in-bot") == 0)
10122 {
10123 linenr_T *lp;
10124
10125 if (!(supported & JO_OUT_IO))
10126 break;
10127 if (hi->hi_key[3] == 't')
10128 {
10129 lp = &opt->jo_in_top;
10130 opt->jo_set |= JO_IN_TOP;
10131 }
10132 else
10133 {
10134 lp = &opt->jo_in_bot;
10135 opt->jo_set |= JO_IN_BOT;
10136 }
10137 *lp = get_tv_number(item);
10138 if (*lp < 0)
10139 {
10140 EMSG2(_(e_invarg2), get_tv_string(item));
10141 return FAIL;
10142 }
10143 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010144 else if (STRCMP(hi->hi_key, "callback") == 0)
10145 {
10146 if (!(supported & JO_CALLBACK))
10147 break;
10148 opt->jo_set |= JO_CALLBACK;
10149 opt->jo_callback = get_callback(item);
10150 if (opt->jo_callback == NULL)
10151 {
10152 EMSG2(_(e_invarg2), "callback");
10153 return FAIL;
10154 }
10155 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010156 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10157 {
10158 if (!(supported & JO_OUT_CALLBACK))
10159 break;
10160 opt->jo_set |= JO_OUT_CALLBACK;
10161 opt->jo_out_cb = get_callback(item);
10162 if (opt->jo_out_cb == NULL)
10163 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010164 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010165 return FAIL;
10166 }
10167 }
10168 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10169 {
10170 if (!(supported & JO_ERR_CALLBACK))
10171 break;
10172 opt->jo_set |= JO_ERR_CALLBACK;
10173 opt->jo_err_cb = get_callback(item);
10174 if (opt->jo_err_cb == NULL)
10175 {
10176 EMSG2(_(e_invarg2), "err-cb");
10177 return FAIL;
10178 }
10179 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010180 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10181 {
10182 if (!(supported & JO_CLOSE_CALLBACK))
10183 break;
10184 opt->jo_set |= JO_CLOSE_CALLBACK;
10185 opt->jo_close_cb = get_callback(item);
10186 if (opt->jo_close_cb == NULL)
10187 {
10188 EMSG2(_(e_invarg2), "close-cb");
10189 return FAIL;
10190 }
10191 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010192 else if (STRCMP(hi->hi_key, "waittime") == 0)
10193 {
10194 if (!(supported & JO_WAITTIME))
10195 break;
10196 opt->jo_set |= JO_WAITTIME;
10197 opt->jo_waittime = get_tv_number(item);
10198 }
10199 else if (STRCMP(hi->hi_key, "timeout") == 0)
10200 {
10201 if (!(supported & JO_TIMEOUT))
10202 break;
10203 opt->jo_set |= JO_TIMEOUT;
10204 opt->jo_timeout = get_tv_number(item);
10205 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010206 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10207 {
10208 if (!(supported & JO_OUT_TIMEOUT))
10209 break;
10210 opt->jo_set |= JO_OUT_TIMEOUT;
10211 opt->jo_out_timeout = get_tv_number(item);
10212 }
10213 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10214 {
10215 if (!(supported & JO_ERR_TIMEOUT))
10216 break;
10217 opt->jo_set |= JO_ERR_TIMEOUT;
10218 opt->jo_err_timeout = get_tv_number(item);
10219 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010220 else if (STRCMP(hi->hi_key, "part") == 0)
10221 {
10222 if (!(supported & JO_PART))
10223 break;
10224 opt->jo_set |= JO_PART;
10225 val = get_tv_string(item);
10226 if (STRCMP(val, "err") == 0)
10227 opt->jo_part = PART_ERR;
10228 else
10229 {
10230 EMSG2(_(e_invarg2), val);
10231 return FAIL;
10232 }
10233 }
10234 else if (STRCMP(hi->hi_key, "id") == 0)
10235 {
10236 if (!(supported & JO_ID))
10237 break;
10238 opt->jo_set |= JO_ID;
10239 opt->jo_id = get_tv_number(item);
10240 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010241 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10242 {
10243 if (!(supported & JO_STOPONEXIT))
10244 break;
10245 opt->jo_set |= JO_STOPONEXIT;
10246 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10247 opt->jo_soe_buf);
10248 if (opt->jo_stoponexit == NULL)
10249 {
10250 EMSG2(_(e_invarg2), "stoponexit");
10251 return FAIL;
10252 }
10253 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010254 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10255 {
10256 if (!(supported & JO_EXIT_CB))
10257 break;
10258 opt->jo_set |= JO_EXIT_CB;
10259 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010260 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010261 {
10262 EMSG2(_(e_invarg2), "exit-cb");
10263 return FAIL;
10264 }
10265 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010266 else
10267 break;
10268 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010269 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010270 if (todo > 0)
10271 {
10272 EMSG2(_(e_invarg2), hi->hi_key);
10273 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010274 }
10275
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010276 return OK;
10277}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010278#endif
10279
10280#ifdef FEAT_CHANNEL
10281/*
10282 * Get the channel from the argument.
10283 * Returns NULL if the handle is invalid.
10284 */
10285 static channel_T *
10286get_channel_arg(typval_T *tv)
10287{
10288 channel_T *channel;
10289
10290 if (tv->v_type != VAR_CHANNEL)
10291 {
10292 EMSG2(_(e_invarg2), get_tv_string(tv));
10293 return NULL;
10294 }
10295 channel = tv->vval.v_channel;
10296
10297 if (channel == NULL || !channel_is_open(channel))
10298 {
10299 EMSG(_("E906: not an open channel"));
10300 return NULL;
10301 }
10302 return channel;
10303}
10304
10305/*
10306 * "ch_close()" function
10307 */
10308 static void
10309f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10310{
10311 channel_T *channel = get_channel_arg(&argvars[0]);
10312
10313 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010314 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010315 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010316 channel_clear(channel);
10317 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010318}
10319
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010320/*
10321 * "ch_getbufnr()" function
10322 */
10323 static void
10324f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10325{
10326 channel_T *channel = get_channel_arg(&argvars[0]);
10327
10328 rettv->vval.v_number = -1;
10329 if (channel != NULL)
10330 {
10331 char_u *what = get_tv_string(&argvars[1]);
10332 int part;
10333
10334 if (STRCMP(what, "err") == 0)
10335 part = PART_ERR;
10336 else if (STRCMP(what, "out") == 0)
10337 part = PART_OUT;
10338 else if (STRCMP(what, "in") == 0)
10339 part = PART_IN;
10340 else
10341 part = PART_SOCK;
10342 if (channel->ch_part[part].ch_buffer != NULL)
10343 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10344 }
10345}
10346
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010347# ifdef FEAT_JOB
10348/*
10349 * "ch_getjob()" function
10350 */
10351 static void
10352f_ch_getjob(typval_T *argvars, typval_T *rettv)
10353{
10354 channel_T *channel = get_channel_arg(&argvars[0]);
10355
10356 if (channel != NULL)
10357 {
10358 rettv->v_type = VAR_JOB;
10359 rettv->vval.v_job = channel->ch_job;
10360 if (channel->ch_job != NULL)
10361 ++channel->ch_job->jv_refcount;
10362 }
10363}
10364# endif
10365
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010366/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010367 * "ch_log()" function
10368 */
10369 static void
10370f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10371{
10372 char_u *msg = get_tv_string(&argvars[0]);
10373 channel_T *channel = NULL;
10374
10375 if (argvars[1].v_type != VAR_UNKNOWN)
10376 channel = get_channel_arg(&argvars[1]);
10377
10378 ch_log(channel, (char *)msg);
10379}
10380
10381/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010382 * "ch_logfile()" function
10383 */
10384 static void
10385f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10386{
10387 char_u *fname;
10388 char_u *opt = (char_u *)"";
10389 char_u buf[NUMBUFLEN];
10390 FILE *file = NULL;
10391
10392 fname = get_tv_string(&argvars[0]);
10393 if (argvars[1].v_type == VAR_STRING)
10394 opt = get_tv_string_buf(&argvars[1], buf);
10395 if (*fname != NUL)
10396 {
10397 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10398 if (file == NULL)
10399 {
10400 EMSG2(_(e_notopen), fname);
10401 return;
10402 }
10403 }
10404 ch_logfile(file);
10405}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010406
10407/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010408 * "ch_open()" function
10409 */
10410 static void
10411f_ch_open(typval_T *argvars, typval_T *rettv)
10412{
10413 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010414 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010415 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010416 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010417 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010418 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010419
10420 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010421 rettv->v_type = VAR_CHANNEL;
10422 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010423
10424 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010425 if (argvars[1].v_type != VAR_UNKNOWN
10426 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010427 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010428 EMSG(_(e_invarg));
10429 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010430 }
10431
10432 /* parse address */
10433 p = vim_strchr(address, ':');
10434 if (p == NULL)
10435 {
10436 EMSG2(_(e_invarg2), address);
10437 return;
10438 }
10439 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010440 port = strtol((char *)p, &rest, 10);
10441 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010442 {
10443 p[-1] = ':';
10444 EMSG2(_(e_invarg2), address);
10445 return;
10446 }
10447
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010448 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010449 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010450 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010451 opt.jo_timeout = 2000;
10452 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010453 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010454 return;
10455 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010456 {
10457 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010458 return;
10459 }
10460
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010461 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010462 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010463 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010464 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010465 opt.jo_set = JO_ALL;
10466 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010467 }
10468}
10469
10470/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010471 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010472 */
10473 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010474common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010475{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010476 channel_T *channel;
10477 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010478 jobopt_T opt;
10479 int mode;
10480 int timeout;
10481 int id = -1;
10482 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010483
10484 /* return an empty string by default */
10485 rettv->v_type = VAR_STRING;
10486 rettv->vval.v_string = NULL;
10487
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010488 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010489 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10490 == FAIL)
10491 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492
Bram Moolenaar77073442016-02-13 23:23:53 +010010493 channel = get_channel_arg(&argvars[0]);
10494 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010495 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010496 if (opt.jo_set & JO_PART)
10497 part = opt.jo_part;
10498 else
10499 part = channel_part_read(channel);
10500 mode = channel_get_mode(channel, part);
10501 timeout = channel_get_timeout(channel, part);
10502 if (opt.jo_set & JO_TIMEOUT)
10503 timeout = opt.jo_timeout;
10504
10505 if (raw || mode == MODE_RAW || mode == MODE_NL)
10506 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10507 else
10508 {
10509 if (opt.jo_set & JO_ID)
10510 id = opt.jo_id;
10511 channel_read_json_block(channel, part, timeout, id, &listtv);
10512 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010513 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010514 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010515 vim_free(listtv);
10516 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010517 else
10518 {
10519 rettv->v_type = VAR_SPECIAL;
10520 rettv->vval.v_number = VVAL_NONE;
10521 }
10522 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010523 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010524}
10525
10526/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010527 * "ch_read()" function
10528 */
10529 static void
10530f_ch_read(typval_T *argvars, typval_T *rettv)
10531{
10532 common_channel_read(argvars, rettv, FALSE);
10533}
10534
10535/*
10536 * "ch_readraw()" function
10537 */
10538 static void
10539f_ch_readraw(typval_T *argvars, typval_T *rettv)
10540{
10541 common_channel_read(argvars, rettv, TRUE);
10542}
10543
10544/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010545 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010546 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010547 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010548 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010549 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010550 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010551send_common(
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010552 typval_T *argvars,
10553 char_u *text,
10554 int id,
10555 int eval,
10556 jobopt_T *opt,
10557 char *fun,
10558 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010559{
Bram Moolenaar77073442016-02-13 23:23:53 +010010560 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010561 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010562
Bram Moolenaar77073442016-02-13 23:23:53 +010010563 channel = get_channel_arg(&argvars[0]);
10564 if (channel == NULL)
10565 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010566 part_send = channel_part_send(channel);
10567 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010568
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010569 clear_job_options(opt);
10570 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010571 return NULL;
10572
Bram Moolenaara07fec92016-02-05 21:04:08 +010010573 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010574 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10575 * allowed. */
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010576 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010577 {
10578 if (eval)
10579 {
10580 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10581 return NULL;
10582 }
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010583 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010584 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010585
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010586 if (channel_send(channel, part_send, text, fun) == OK
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010587 && opt->jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010588 return channel;
10589 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010590}
10591
10592/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010593 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010594 */
10595 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010596ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010597{
10598 char_u *text;
10599 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010600 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010601 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010602 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010603 int part_send;
10604 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010605 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010606 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010607
10608 /* return an empty string by default */
10609 rettv->v_type = VAR_STRING;
10610 rettv->vval.v_string = NULL;
10611
Bram Moolenaar77073442016-02-13 23:23:53 +010010612 channel = get_channel_arg(&argvars[0]);
10613 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010614 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010615 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010616
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010617 ch_mode = channel_get_mode(channel, part_send);
10618 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010619 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010620 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010621 return;
10622 }
10623
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010624 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010625 text = json_encode_nr_expr(id, &argvars[1],
10626 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010627 if (text == NULL)
10628 return;
10629
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010630 channel = send_common(argvars, text, id, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010631 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010632 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010633 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010634 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010635 if (opt.jo_set & JO_TIMEOUT)
10636 timeout = opt.jo_timeout;
10637 else
10638 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010639 timeout = channel_get_timeout(channel, part_read);
10640 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10641 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010642 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010643 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010644
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010645 /* Move the item from the list and then change the type to
10646 * avoid the value being freed. */
10647 *rettv = list->lv_last->li_tv;
10648 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010649 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010650 }
10651 }
10652}
10653
10654/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010655 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010656 */
10657 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010658f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10659{
10660 ch_expr_common(argvars, rettv, TRUE);
10661}
10662
10663/*
10664 * "ch_sendexpr()" function
10665 */
10666 static void
10667f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10668{
10669 ch_expr_common(argvars, rettv, FALSE);
10670}
10671
10672/*
10673 * common for "ch_evalraw()" and "ch_sendraw()"
10674 */
10675 static void
10676ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010677{
10678 char_u buf[NUMBUFLEN];
10679 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010680 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010681 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010682 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010683 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010684
10685 /* return an empty string by default */
10686 rettv->v_type = VAR_STRING;
10687 rettv->vval.v_string = NULL;
10688
10689 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010690 channel = send_common(argvars, text, 0, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010691 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10692 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010693 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010694 if (opt.jo_set & JO_TIMEOUT)
10695 timeout = opt.jo_timeout;
10696 else
10697 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010698 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10699 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010700}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010701
10702/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010703 * "ch_evalraw()" function
10704 */
10705 static void
10706f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10707{
10708 ch_raw_common(argvars, rettv, TRUE);
10709}
10710
10711/*
10712 * "ch_sendraw()" function
10713 */
10714 static void
10715f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10716{
10717 ch_raw_common(argvars, rettv, FALSE);
10718}
10719
10720/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010721 * "ch_setoptions()" function
10722 */
10723 static void
10724f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10725{
10726 channel_T *channel;
10727 jobopt_T opt;
10728
10729 channel = get_channel_arg(&argvars[0]);
10730 if (channel == NULL)
10731 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010732 clear_job_options(&opt);
10733 if (get_job_options(&argvars[1], &opt,
10734 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010735 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010736 channel_set_options(channel, &opt);
10737}
10738
10739/*
10740 * "ch_status()" function
10741 */
10742 static void
10743f_ch_status(typval_T *argvars, typval_T *rettv)
10744{
10745 /* return an empty string by default */
10746 rettv->v_type = VAR_STRING;
10747
10748 if (argvars[0].v_type != VAR_CHANNEL)
10749 {
10750 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10751 rettv->vval.v_string = NULL;
10752 }
10753 else
10754 rettv->vval.v_string = vim_strsave(
10755 (char_u *)channel_status(argvars[0].vval.v_channel));
10756}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010757#endif
10758
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010759/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010760 * "changenr()" function
10761 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010763f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010764{
10765 rettv->vval.v_number = curbuf->b_u_seq_cur;
10766}
10767
10768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 * "char2nr(string)" function
10770 */
10771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010772f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
10774#ifdef FEAT_MBYTE
10775 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010776 {
10777 int utf8 = 0;
10778
10779 if (argvars[1].v_type != VAR_UNKNOWN)
10780 utf8 = get_tv_number_chk(&argvars[1], NULL);
10781
10782 if (utf8)
10783 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10784 else
10785 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 else
10788#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790}
10791
10792/*
10793 * "cindent(lnum)" function
10794 */
10795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010796f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797{
10798#ifdef FEAT_CINDENT
10799 pos_T pos;
10800 linenr_T lnum;
10801
10802 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10805 {
10806 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010807 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 curwin->w_cursor = pos;
10809 }
10810 else
10811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010812 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813}
10814
10815/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010816 * "clearmatches()" function
10817 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010819f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010820{
10821#ifdef FEAT_SEARCH_EXTRA
10822 clear_matches(curwin);
10823#endif
10824}
10825
10826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 * "col(string)" function
10828 */
10829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010830f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832 colnr_T col = 0;
10833 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010834 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010836 fp = var2fpos(&argvars[0], FALSE, &fnum);
10837 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
10839 if (fp->col == MAXCOL)
10840 {
10841 /* '> can be MAXCOL, get the length of the line then */
10842 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010843 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 else
10845 col = MAXCOL;
10846 }
10847 else
10848 {
10849 col = fp->col + 1;
10850#ifdef FEAT_VIRTUALEDIT
10851 /* col(".") when the cursor is on the NUL at the end of the line
10852 * because of "coladd" can be seen as an extra column. */
10853 if (virtual_active() && fp == &curwin->w_cursor)
10854 {
10855 char_u *p = ml_get_cursor();
10856
10857 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10858 curwin->w_virtcol - curwin->w_cursor.coladd))
10859 {
10860# ifdef FEAT_MBYTE
10861 int l;
10862
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010863 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 col += l;
10865# else
10866 if (*p != NUL && p[1] == NUL)
10867 ++col;
10868# endif
10869 }
10870 }
10871#endif
10872 }
10873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
Bram Moolenaar572cb562005-08-05 21:35:02 +000010877#if defined(FEAT_INS_EXPAND)
10878/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010879 * "complete()" function
10880 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010882f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010883{
10884 int startcol;
10885
10886 if ((State & INSERT) == 0)
10887 {
10888 EMSG(_("E785: complete() can only be used in Insert mode"));
10889 return;
10890 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010891
10892 /* Check for undo allowed here, because if something was already inserted
10893 * the line was already saved for undo and this check isn't done. */
10894 if (!undo_allowed())
10895 return;
10896
Bram Moolenaarade00832006-03-10 21:46:58 +000010897 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10898 {
10899 EMSG(_(e_invarg));
10900 return;
10901 }
10902
10903 startcol = get_tv_number_chk(&argvars[0], NULL);
10904 if (startcol <= 0)
10905 return;
10906
10907 set_completion(startcol - 1, argvars[1].vval.v_list);
10908}
10909
10910/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010911 * "complete_add()" function
10912 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010914f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010915{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010916 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010917}
10918
10919/*
10920 * "complete_check()" function
10921 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010923f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010924{
10925 int saved = RedrawingDisabled;
10926
10927 RedrawingDisabled = 0;
10928 ins_compl_check_keys(0);
10929 rettv->vval.v_number = compl_interrupted;
10930 RedrawingDisabled = saved;
10931}
10932#endif
10933
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934/*
10935 * "confirm(message, buttons[, default [, type]])" function
10936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010938f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10941 char_u *message;
10942 char_u *buttons = NULL;
10943 char_u buf[NUMBUFLEN];
10944 char_u buf2[NUMBUFLEN];
10945 int def = 1;
10946 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 char_u *typestr;
10948 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 message = get_tv_string_chk(&argvars[0]);
10951 if (message == NULL)
10952 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10956 if (buttons == NULL)
10957 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010961 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10964 if (typestr == NULL)
10965 error = TRUE;
10966 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 switch (TOUPPER_ASC(*typestr))
10969 {
10970 case 'E': type = VIM_ERROR; break;
10971 case 'Q': type = VIM_QUESTION; break;
10972 case 'I': type = VIM_INFO; break;
10973 case 'W': type = VIM_WARNING; break;
10974 case 'G': type = VIM_GENERIC; break;
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 }
10977 }
10978 }
10979 }
10980
10981 if (buttons == NULL || *buttons == NUL)
10982 buttons = (char_u *)_("&Ok");
10983
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010984 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010985 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010986 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987#endif
10988}
10989
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010990/*
10991 * "copy()" function
10992 */
10993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010994f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010995{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010996 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010997}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010999#ifdef FEAT_FLOAT
11000/*
11001 * "cos()" function
11002 */
11003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011004f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011005{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011006 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011007
11008 rettv->v_type = VAR_FLOAT;
11009 if (get_float_arg(argvars, &f) == OK)
11010 rettv->vval.v_float = cos(f);
11011 else
11012 rettv->vval.v_float = 0.0;
11013}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011014
11015/*
11016 * "cosh()" function
11017 */
11018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011020{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011021 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011022
11023 rettv->v_type = VAR_FLOAT;
11024 if (get_float_arg(argvars, &f) == OK)
11025 rettv->vval.v_float = cosh(f);
11026 else
11027 rettv->vval.v_float = 0.0;
11028}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011029#endif
11030
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011032 * "count()" function
11033 */
11034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011035f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011036{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011037 long n = 0;
11038 int ic = FALSE;
11039
Bram Moolenaare9a41262005-01-15 22:18:47 +000011040 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011041 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 listitem_T *li;
11043 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011045
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 if ((l = argvars[0].vval.v_list) != NULL)
11047 {
11048 li = l->lv_first;
11049 if (argvars[2].v_type != VAR_UNKNOWN)
11050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 int error = FALSE;
11052
11053 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011054 if (argvars[3].v_type != VAR_UNKNOWN)
11055 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 idx = get_tv_number_chk(&argvars[3], &error);
11057 if (!error)
11058 {
11059 li = list_find(l, idx);
11060 if (li == NULL)
11061 EMSGN(_(e_listidx), idx);
11062 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 if (error)
11065 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 }
11067
11068 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011069 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 ++n;
11071 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011072 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011073 else if (argvars[0].v_type == VAR_DICT)
11074 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011075 int todo;
11076 dict_T *d;
11077 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011079 if ((d = argvars[0].vval.v_dict) != NULL)
11080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081 int error = FALSE;
11082
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083 if (argvars[2].v_type != VAR_UNKNOWN)
11084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086 if (argvars[3].v_type != VAR_UNKNOWN)
11087 EMSG(_(e_invarg));
11088 }
11089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011090 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011091 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011092 {
11093 if (!HASHITEM_EMPTY(hi))
11094 {
11095 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011096 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011097 ++n;
11098 }
11099 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011100 }
11101 }
11102 else
11103 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011104 rettv->vval.v_number = n;
11105}
11106
11107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11109 *
11110 * Checks the existence of a cscope connection.
11111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011113f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114{
11115#ifdef FEAT_CSCOPE
11116 int num = 0;
11117 char_u *dbpath = NULL;
11118 char_u *prepend = NULL;
11119 char_u buf[NUMBUFLEN];
11120
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011121 if (argvars[0].v_type != VAR_UNKNOWN
11122 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011124 num = (int)get_tv_number(&argvars[0]);
11125 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011126 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 }
11129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011130 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131#endif
11132}
11133
11134/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011135 * "cursor(lnum, col)" function, or
11136 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011138 * Moves the cursor to the specified line and column.
11139 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011142f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143{
11144 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011145#ifdef FEAT_VIRTUALEDIT
11146 long coladd = 0;
11147#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011148 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011150 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011151 if (argvars[1].v_type == VAR_UNKNOWN)
11152 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011153 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011154 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011155
Bram Moolenaar493c1782014-05-28 14:34:46 +020011156 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011157 {
11158 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011159 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011160 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011161 line = pos.lnum;
11162 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011163#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011164 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011165#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011166 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011167 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011168 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011169 set_curswant = FALSE;
11170 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011171 }
11172 else
11173 {
11174 line = get_tv_lnum(argvars);
11175 col = get_tv_number_chk(&argvars[1], NULL);
11176#ifdef FEAT_VIRTUALEDIT
11177 if (argvars[2].v_type != VAR_UNKNOWN)
11178 coladd = get_tv_number_chk(&argvars[2], NULL);
11179#endif
11180 }
11181 if (line < 0 || col < 0
11182#ifdef FEAT_VIRTUALEDIT
11183 || coladd < 0
11184#endif
11185 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187 if (line > 0)
11188 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 if (col > 0)
11190 curwin->w_cursor.col = col - 1;
11191#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011192 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193#endif
11194
11195 /* Make sure the cursor is in a valid position. */
11196 check_cursor();
11197#ifdef FEAT_MBYTE
11198 /* Correct cursor for multi-byte character. */
11199 if (has_mbyte)
11200 mb_adjust_cursor();
11201#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011202
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011203 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011204 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205}
11206
11207/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011208 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 */
11210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011211f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011213 int noref = 0;
11214
11215 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011217 if (noref < 0 || noref > 1)
11218 EMSG(_(e_invarg));
11219 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011220 {
11221 current_copyID += COPYID_INC;
11222 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225
11226/*
11227 * "delete()" function
11228 */
11229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011230f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011232 char_u nbuf[NUMBUFLEN];
11233 char_u *name;
11234 char_u *flags;
11235
11236 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011238 return;
11239
11240 name = get_tv_string(&argvars[0]);
11241 if (name == NULL || *name == NUL)
11242 {
11243 EMSG(_(e_invarg));
11244 return;
11245 }
11246
11247 if (argvars[1].v_type != VAR_UNKNOWN)
11248 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011250 flags = (char_u *)"";
11251
11252 if (*flags == NUL)
11253 /* delete a file */
11254 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11255 else if (STRCMP(flags, "d") == 0)
11256 /* delete an empty directory */
11257 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11258 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011259 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011260 rettv->vval.v_number = delete_recursive(name);
11261 else
11262 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263}
11264
11265/*
11266 * "did_filetype()" function
11267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011269f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270{
11271#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011272 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273#endif
11274}
11275
11276/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011277 * "diff_filler()" function
11278 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011280f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011281{
11282#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011284#endif
11285}
11286
11287/*
11288 * "diff_hlID()" function
11289 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011291f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011292{
11293#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011295 static linenr_T prev_lnum = 0;
11296 static int changedtick = 0;
11297 static int fnum = 0;
11298 static int change_start = 0;
11299 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011300 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011301 int filler_lines;
11302 int col;
11303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 if (lnum < 0) /* ignore type error in {lnum} arg */
11305 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011306 if (lnum != prev_lnum
11307 || changedtick != curbuf->b_changedtick
11308 || fnum != curbuf->b_fnum)
11309 {
11310 /* New line, buffer, change: need to get the values. */
11311 filler_lines = diff_check(curwin, lnum);
11312 if (filler_lines < 0)
11313 {
11314 if (filler_lines == -1)
11315 {
11316 change_start = MAXCOL;
11317 change_end = -1;
11318 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11319 hlID = HLF_ADD; /* added line */
11320 else
11321 hlID = HLF_CHD; /* changed line */
11322 }
11323 else
11324 hlID = HLF_ADD; /* added line */
11325 }
11326 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011327 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011328 prev_lnum = lnum;
11329 changedtick = curbuf->b_changedtick;
11330 fnum = curbuf->b_fnum;
11331 }
11332
11333 if (hlID == HLF_CHD || hlID == HLF_TXD)
11334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011336 if (col >= change_start && col <= change_end)
11337 hlID = HLF_TXD; /* changed text */
11338 else
11339 hlID = HLF_CHD; /* changed line */
11340 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011341 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011342#endif
11343}
11344
11345/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011346 * "disable_char_avail_for_testing({expr})" function
11347 */
11348 static void
11349f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11350{
11351 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11352}
11353
11354/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011355 * "empty({expr})" function
11356 */
11357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011358f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011359{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011360 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011361
11362 switch (argvars[0].v_type)
11363 {
11364 case VAR_STRING:
11365 case VAR_FUNC:
11366 n = argvars[0].vval.v_string == NULL
11367 || *argvars[0].vval.v_string == NUL;
11368 break;
11369 case VAR_NUMBER:
11370 n = argvars[0].vval.v_number == 0;
11371 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011372 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011373#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011374 n = argvars[0].vval.v_float == 0.0;
11375 break;
11376#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011377 case VAR_LIST:
11378 n = argvars[0].vval.v_list == NULL
11379 || argvars[0].vval.v_list->lv_first == NULL;
11380 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 case VAR_DICT:
11382 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011383 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011384 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011385 case VAR_SPECIAL:
11386 n = argvars[0].vval.v_number != VVAL_TRUE;
11387 break;
11388
Bram Moolenaar835dc632016-02-07 14:27:38 +010011389 case VAR_JOB:
11390#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011391 n = argvars[0].vval.v_job == NULL
11392 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11393 break;
11394#endif
11395 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011396#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011397 n = argvars[0].vval.v_channel == NULL
11398 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011399 break;
11400#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011401 case VAR_UNKNOWN:
11402 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11403 n = TRUE;
11404 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011405 }
11406
11407 rettv->vval.v_number = n;
11408}
11409
11410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 * "escape({string}, {chars})" function
11412 */
11413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011414f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415{
11416 char_u buf[NUMBUFLEN];
11417
Bram Moolenaar758711c2005-02-02 23:11:38 +000011418 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11419 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421}
11422
11423/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011424 * "eval()" function
11425 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011427f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011428{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011429 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 s = get_tv_string_chk(&argvars[0]);
11432 if (s != NULL)
11433 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011434
Bram Moolenaar615b9972015-01-14 17:15:05 +010011435 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11437 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011438 if (p != NULL && !aborting())
11439 EMSG2(_(e_invexpr2), p);
11440 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011442 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011444 else if (*s != NUL)
11445 EMSG(_(e_trailing));
11446}
11447
11448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 * "eventhandler()" function
11450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011452f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455}
11456
11457/*
11458 * "executable()" function
11459 */
11460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011461f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011463 char_u *name = get_tv_string(&argvars[0]);
11464
11465 /* Check in $PATH and also check directly if there is a directory name. */
11466 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11467 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011468}
11469
11470/*
11471 * "exepath()" function
11472 */
11473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011474f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011475{
11476 char_u *p = NULL;
11477
Bram Moolenaarb5971142015-03-21 17:32:19 +010011478 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011479 rettv->v_type = VAR_STRING;
11480 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481}
11482
11483/*
11484 * "exists()" function
11485 */
11486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011487f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488{
11489 char_u *p;
11490 char_u *name;
11491 int n = FALSE;
11492 int len = 0;
11493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495 if (*p == '$') /* environment variable */
11496 {
11497 /* first try "normal" environment variables (fast) */
11498 if (mch_getenv(p + 1) != NULL)
11499 n = TRUE;
11500 else
11501 {
11502 /* try expanding things like $VIM and ${HOME} */
11503 p = expand_env_save(p);
11504 if (p != NULL && *p != '$')
11505 n = TRUE;
11506 vim_free(p);
11507 }
11508 }
11509 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011510 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011512 if (*skipwhite(p) != NUL)
11513 n = FALSE; /* trailing garbage */
11514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515 else if (*p == '*') /* internal or user defined function */
11516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011517 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519 else if (*p == ':')
11520 {
11521 n = cmd_exists(p + 1);
11522 }
11523 else if (*p == '#')
11524 {
11525#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011526 if (p[1] == '#')
11527 n = autocmd_supported(p + 2);
11528 else
11529 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530#endif
11531 }
11532 else /* internal variable */
11533 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011534 char_u *tofree;
11535 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011537 /* get_name_len() takes care of expanding curly braces */
11538 name = p;
11539 len = get_name_len(&p, &tofree, TRUE, FALSE);
11540 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011542 if (tofree != NULL)
11543 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011544 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011545 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011547 /* handle d.key, l[idx], f(expr) */
11548 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11549 if (n)
11550 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 }
11552 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011553 if (*p != NUL)
11554 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 }
11558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011559 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560}
11561
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011562#ifdef FEAT_FLOAT
11563/*
11564 * "exp()" function
11565 */
11566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011567f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011568{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011569 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011570
11571 rettv->v_type = VAR_FLOAT;
11572 if (get_float_arg(argvars, &f) == OK)
11573 rettv->vval.v_float = exp(f);
11574 else
11575 rettv->vval.v_float = 0.0;
11576}
11577#endif
11578
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579/*
11580 * "expand()" function
11581 */
11582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584{
11585 char_u *s;
11586 int len;
11587 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011588 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011591 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011594 if (argvars[1].v_type != VAR_UNKNOWN
11595 && argvars[2].v_type != VAR_UNKNOWN
11596 && get_tv_number_chk(&argvars[2], &error)
11597 && !error)
11598 {
11599 rettv->v_type = VAR_LIST;
11600 rettv->vval.v_list = NULL;
11601 }
11602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011603 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 if (*s == '%' || *s == '#' || *s == '<')
11605 {
11606 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011607 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011609 if (rettv->v_type == VAR_LIST)
11610 {
11611 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11612 list_append_string(rettv->vval.v_list, result, -1);
11613 else
11614 vim_free(result);
11615 }
11616 else
11617 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 }
11619 else
11620 {
11621 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011622 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 if (argvars[1].v_type != VAR_UNKNOWN
11624 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011625 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 if (!error)
11627 {
11628 ExpandInit(&xpc);
11629 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011630 if (p_wic)
11631 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011632 if (rettv->v_type == VAR_STRING)
11633 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11634 options, WILD_ALL);
11635 else if (rettv_list_alloc(rettv) != FAIL)
11636 {
11637 int i;
11638
11639 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11640 for (i = 0; i < xpc.xp_numfiles; i++)
11641 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11642 ExpandCleanup(&xpc);
11643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 }
11645 else
11646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 }
11648}
11649
11650/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011651 * Go over all entries in "d2" and add them to "d1".
11652 * When "action" is "error" then a duplicate key is an error.
11653 * When "action" is "force" then a duplicate key is overwritten.
11654 * Otherwise duplicate keys are ignored ("action" is "keep").
11655 */
11656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011657dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011658{
11659 dictitem_T *di1;
11660 hashitem_T *hi2;
11661 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011662 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011663
11664 todo = (int)d2->dv_hashtab.ht_used;
11665 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11666 {
11667 if (!HASHITEM_EMPTY(hi2))
11668 {
11669 --todo;
11670 di1 = dict_find(d1, hi2->hi_key, -1);
11671 if (d1->dv_scope != 0)
11672 {
11673 /* Disallow replacing a builtin function in l: and g:.
11674 * Check the key to be valid when adding to any
11675 * scope. */
11676 if (d1->dv_scope == VAR_DEF_SCOPE
11677 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11678 && var_check_func_name(hi2->hi_key,
11679 di1 == NULL))
11680 break;
11681 if (!valid_varname(hi2->hi_key))
11682 break;
11683 }
11684 if (di1 == NULL)
11685 {
11686 di1 = dictitem_copy(HI2DI(hi2));
11687 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11688 dictitem_free(di1);
11689 }
11690 else if (*action == 'e')
11691 {
11692 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11693 break;
11694 }
11695 else if (*action == 'f' && HI2DI(hi2) != di1)
11696 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011697 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11698 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011699 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011700 clear_tv(&di1->di_tv);
11701 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11702 }
11703 }
11704 }
11705}
11706
11707/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011708 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011709 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011710 */
11711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011712f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011713{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011714 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011715
Bram Moolenaare9a41262005-01-15 22:18:47 +000011716 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011717 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011718 list_T *l1, *l2;
11719 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011720 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011722
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 l1 = argvars[0].vval.v_list;
11724 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011725 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011726 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 {
11728 if (argvars[2].v_type != VAR_UNKNOWN)
11729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 before = get_tv_number_chk(&argvars[2], &error);
11731 if (error)
11732 return; /* type error; errmsg already given */
11733
Bram Moolenaar758711c2005-02-02 23:11:38 +000011734 if (before == l1->lv_len)
11735 item = NULL;
11736 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011737 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011738 item = list_find(l1, before);
11739 if (item == NULL)
11740 {
11741 EMSGN(_(e_listidx), before);
11742 return;
11743 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 }
11745 }
11746 else
11747 item = NULL;
11748 list_extend(l1, l2, item);
11749
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 copy_tv(&argvars[0], rettv);
11751 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011753 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11754 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011755 dict_T *d1, *d2;
11756 char_u *action;
11757 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011758
11759 d1 = argvars[0].vval.v_dict;
11760 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011761 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011762 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011763 {
11764 /* Check the third argument. */
11765 if (argvars[2].v_type != VAR_UNKNOWN)
11766 {
11767 static char *(av[]) = {"keep", "force", "error"};
11768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011769 action = get_tv_string_chk(&argvars[2]);
11770 if (action == NULL)
11771 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 for (i = 0; i < 3; ++i)
11773 if (STRCMP(action, av[i]) == 0)
11774 break;
11775 if (i == 3)
11776 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011777 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 return;
11779 }
11780 }
11781 else
11782 action = (char_u *)"force";
11783
Bram Moolenaara9922d62013-05-30 13:01:18 +020011784 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786 copy_tv(&argvars[0], rettv);
11787 }
11788 }
11789 else
11790 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011791}
11792
11793/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011794 * "feedkeys()" function
11795 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011797f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011798{
11799 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011800 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011801 char_u *keys, *flags;
11802 char_u nbuf[NUMBUFLEN];
11803 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011804 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011805 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011806
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011807 /* This is not allowed in the sandbox. If the commands would still be
11808 * executed in the sandbox it would be OK, but it probably happens later,
11809 * when "sandbox" is no longer set. */
11810 if (check_secure())
11811 return;
11812
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011813 keys = get_tv_string(&argvars[0]);
11814 if (*keys != NUL)
11815 {
11816 if (argvars[1].v_type != VAR_UNKNOWN)
11817 {
11818 flags = get_tv_string_buf(&argvars[1], nbuf);
11819 for ( ; *flags != NUL; ++flags)
11820 {
11821 switch (*flags)
11822 {
11823 case 'n': remap = FALSE; break;
11824 case 'm': remap = TRUE; break;
11825 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011826 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011827 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011828 }
11829 }
11830 }
11831
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011832 /* Need to escape K_SPECIAL and CSI before putting the string in the
11833 * typeahead buffer. */
11834 keys_esc = vim_strsave_escape_csi(keys);
11835 if (keys_esc != NULL)
11836 {
11837 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011838 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011839 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011840 if (vgetc_busy)
11841 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011842 if (execute)
11843 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011844 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011845 }
11846}
11847
11848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 * "filereadable()" function
11850 */
11851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011852f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011854 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855 char_u *p;
11856 int n;
11857
Bram Moolenaarc236c162008-07-13 17:41:49 +000011858#ifndef O_NONBLOCK
11859# define O_NONBLOCK 0
11860#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011861 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011862 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11863 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 {
11865 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011866 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 }
11868 else
11869 n = FALSE;
11870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011871 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872}
11873
11874/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011875 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 * rights to write into.
11877 */
11878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011879f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011881 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882}
11883
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011884 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011885findfilendir(
11886 typval_T *argvars UNUSED,
11887 typval_T *rettv,
11888 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011889{
11890#ifdef FEAT_SEARCHPATH
11891 char_u *fname;
11892 char_u *fresult = NULL;
11893 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11894 char_u *p;
11895 char_u pathbuf[NUMBUFLEN];
11896 int count = 1;
11897 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011898 int error = FALSE;
11899#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011900
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011901 rettv->vval.v_string = NULL;
11902 rettv->v_type = VAR_STRING;
11903
11904#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11910 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011911 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 else
11913 {
11914 if (*p != NUL)
11915 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011918 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011920 }
11921
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011922 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11923 error = TRUE;
11924
11925 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011927 do
11928 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011929 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011930 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 fresult = find_file_in_path_option(first ? fname : NULL,
11932 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011933 0, first, path,
11934 find_what,
11935 curbuf->b_ffname,
11936 find_what == FINDFILE_DIR
11937 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011939
11940 if (fresult != NULL && rettv->v_type == VAR_LIST)
11941 list_append_string(rettv->vval.v_list, fresult, -1);
11942
11943 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011945
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011946 if (rettv->v_type == VAR_STRING)
11947 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011948#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949}
11950
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011951static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11952static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011953
11954/*
11955 * Implementation of map() and filter().
11956 */
11957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011958filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011959{
11960 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011962 listitem_T *li, *nli;
11963 list_T *l = NULL;
11964 dictitem_T *di;
11965 hashtab_T *ht;
11966 hashitem_T *hi;
11967 dict_T *d = NULL;
11968 typval_T save_val;
11969 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011971 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011972 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011973 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011974 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011975 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011976 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011977
Bram Moolenaare9a41262005-01-15 22:18:47 +000011978 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011979 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011980 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011981 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011982 return;
11983 }
11984 else if (argvars[0].v_type == VAR_DICT)
11985 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011986 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011987 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011988 return;
11989 }
11990 else
11991 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011992 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 return;
11994 }
11995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011996 expr = get_tv_string_buf_chk(&argvars[1], buf);
11997 /* On type errors, the preceding call has already displayed an error
11998 * message. Avoid a misleading error message for an empty string that
11999 * was not passed as argument. */
12000 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 prepare_vimvar(VV_VAL, &save_val);
12003 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012004
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012005 /* We reset "did_emsg" to be able to detect whether an error
12006 * occurred during evaluation of the expression. */
12007 save_did_emsg = did_emsg;
12008 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012009
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012010 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012011 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 vimvars[VV_KEY].vv_type = VAR_STRING;
12014
12015 ht = &d->dv_hashtab;
12016 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012017 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 if (!HASHITEM_EMPTY(hi))
12021 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012022 int r;
12023
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012024 --todo;
12025 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012026 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012027 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12028 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012029 break;
12030 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012031 r = filter_map_one(&di->di_tv, expr, map, &rem);
12032 clear_tv(&vimvars[VV_KEY].vv_tv);
12033 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 break;
12035 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012036 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012037 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12038 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012039 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012040 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 }
12043 }
12044 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 }
12046 else
12047 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012048 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 for (li = l->lv_first; li != NULL; li = nli)
12051 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012052 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012053 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012054 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012055 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012056 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012057 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012058 break;
12059 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012061 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012062 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012063 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012064
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012065 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012067
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012068 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012069 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012070
12071 copy_tv(&argvars[0], rettv);
12072}
12073
12074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012075filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012076{
Bram Moolenaar33570922005-01-25 22:26:29 +000012077 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012078 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012079 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080
Bram Moolenaar33570922005-01-25 22:26:29 +000012081 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012082 s = expr;
12083 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012084 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012085 if (*s != NUL) /* check for trailing chars after expr */
12086 {
12087 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012088 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012089 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012090 }
12091 if (map)
12092 {
12093 /* map(): replace the list item value */
12094 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012095 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012096 *tv = rettv;
12097 }
12098 else
12099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012100 int error = FALSE;
12101
Bram Moolenaare9a41262005-01-15 22:18:47 +000012102 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012103 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012105 /* On type error, nothing has been removed; return FAIL to stop the
12106 * loop. The error message was given by get_tv_number_chk(). */
12107 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012108 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012110 retval = OK;
12111theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012112 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012113 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012114}
12115
12116/*
12117 * "filter()" function
12118 */
12119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012120f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012121{
12122 filter_map(argvars, rettv, FALSE);
12123}
12124
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126 * "finddir({fname}[, {path}[, {count}]])" function
12127 */
12128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012129f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012131 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132}
12133
12134/*
12135 * "findfile({fname}[, {path}[, {count}]])" function
12136 */
12137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012138f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012140 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141}
12142
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012143#ifdef FEAT_FLOAT
12144/*
12145 * "float2nr({float})" function
12146 */
12147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012148f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012149{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012150 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012151
12152 if (get_float_arg(argvars, &f) == OK)
12153 {
12154 if (f < -0x7fffffff)
12155 rettv->vval.v_number = -0x7fffffff;
12156 else if (f > 0x7fffffff)
12157 rettv->vval.v_number = 0x7fffffff;
12158 else
12159 rettv->vval.v_number = (varnumber_T)f;
12160 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012161}
12162
12163/*
12164 * "floor({float})" function
12165 */
12166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012167f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012168{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012169 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012170
12171 rettv->v_type = VAR_FLOAT;
12172 if (get_float_arg(argvars, &f) == OK)
12173 rettv->vval.v_float = floor(f);
12174 else
12175 rettv->vval.v_float = 0.0;
12176}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012177
12178/*
12179 * "fmod()" function
12180 */
12181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012183{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012184 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012185
12186 rettv->v_type = VAR_FLOAT;
12187 if (get_float_arg(argvars, &fx) == OK
12188 && get_float_arg(&argvars[1], &fy) == OK)
12189 rettv->vval.v_float = fmod(fx, fy);
12190 else
12191 rettv->vval.v_float = 0.0;
12192}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012193#endif
12194
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012196 * "fnameescape({string})" function
12197 */
12198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012199f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012200{
12201 rettv->vval.v_string = vim_strsave_fnameescape(
12202 get_tv_string(&argvars[0]), FALSE);
12203 rettv->v_type = VAR_STRING;
12204}
12205
12206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 * "fnamemodify({fname}, {mods})" function
12208 */
12209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012210f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 char_u *fname;
12213 char_u *mods;
12214 int usedlen = 0;
12215 int len;
12216 char_u *fbuf = NULL;
12217 char_u buf[NUMBUFLEN];
12218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012219 fname = get_tv_string_chk(&argvars[0]);
12220 mods = get_tv_string_buf_chk(&argvars[1], buf);
12221 if (fname == NULL || mods == NULL)
12222 fname = NULL;
12223 else
12224 {
12225 len = (int)STRLEN(fname);
12226 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 vim_free(fbuf);
12235}
12236
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012237static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238
12239/*
12240 * "foldclosed()" function
12241 */
12242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012243foldclosed_both(
12244 typval_T *argvars UNUSED,
12245 typval_T *rettv,
12246 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
12248#ifdef FEAT_FOLDING
12249 linenr_T lnum;
12250 linenr_T first, last;
12251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12254 {
12255 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12256 {
12257 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 return;
12262 }
12263 }
12264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266}
12267
12268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012269 * "foldclosed()" function
12270 */
12271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012272f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012273{
12274 foldclosed_both(argvars, rettv, FALSE);
12275}
12276
12277/*
12278 * "foldclosedend()" function
12279 */
12280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282{
12283 foldclosed_both(argvars, rettv, TRUE);
12284}
12285
12286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 * "foldlevel()" function
12288 */
12289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012290f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291{
12292#ifdef FEAT_FOLDING
12293 linenr_T lnum;
12294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299}
12300
12301/*
12302 * "foldtext()" function
12303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012305f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307#ifdef FEAT_FOLDING
12308 linenr_T lnum;
12309 char_u *s;
12310 char_u *r;
12311 int len;
12312 char *txt;
12313#endif
12314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012318 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12319 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12320 <= curbuf->b_ml.ml_line_count
12321 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 {
12323 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012324 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12325 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 {
12327 if (!linewhite(lnum))
12328 break;
12329 ++lnum;
12330 }
12331
12332 /* Find interesting text in this line. */
12333 s = skipwhite(ml_get(lnum));
12334 /* skip C comment-start */
12335 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012338 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012339 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012340 {
12341 s = skipwhite(ml_get(lnum + 1));
12342 if (*s == '*')
12343 s = skipwhite(s + 1);
12344 }
12345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 txt = _("+-%s%3ld lines: ");
12347 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 + 20 /* for %3ld */
12350 + STRLEN(s))); /* concatenated */
12351 if (r != NULL)
12352 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012353 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12354 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12355 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 len = (int)STRLEN(r);
12357 STRCAT(r, s);
12358 /* remove 'foldmarker' and 'commentstring' */
12359 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 }
12362 }
12363#endif
12364}
12365
12366/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012367 * "foldtextresult(lnum)" function
12368 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012371{
12372#ifdef FEAT_FOLDING
12373 linenr_T lnum;
12374 char_u *text;
12375 char_u buf[51];
12376 foldinfo_T foldinfo;
12377 int fold_count;
12378#endif
12379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->v_type = VAR_STRING;
12381 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012382#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 /* treat illegal types and illegal string values for {lnum} the same */
12385 if (lnum < 0)
12386 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012387 fold_count = foldedCount(curwin, lnum, &foldinfo);
12388 if (fold_count > 0)
12389 {
12390 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12391 &foldinfo, buf);
12392 if (text == buf)
12393 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012395 }
12396#endif
12397}
12398
12399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 * "foreground()" function
12401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012403f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405#ifdef FEAT_GUI
12406 if (gui.in_use)
12407 gui_mch_set_foreground();
12408#else
12409# ifdef WIN32
12410 win32_set_foreground();
12411# endif
12412#endif
12413}
12414
12415/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 * "function()" function
12417 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012419f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420{
12421 char_u *s;
12422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012424 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012425 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012426 /* Don't check an autoload name for existence here. */
12427 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012428 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012429 else
12430 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012431 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012432 {
12433 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012434 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012435
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012436 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12437 * also be called from another script. Using trans_function_name()
12438 * would also work, but some plugins depend on the name being
12439 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012440 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012441 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012442 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012443 if (rettv->vval.v_string != NULL)
12444 {
12445 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012446 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012447 }
12448 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012449 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012450 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012452 }
12453}
12454
12455/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012456 * "garbagecollect()" function
12457 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012459f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012460{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012461 /* This is postponed until we are back at the toplevel, because we may be
12462 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12463 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012464
12465 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12466 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012467}
12468
12469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012470 * "get()" function
12471 */
12472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012473f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474{
Bram Moolenaar33570922005-01-25 22:26:29 +000012475 listitem_T *li;
12476 list_T *l;
12477 dictitem_T *di;
12478 dict_T *d;
12479 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480
Bram Moolenaare9a41262005-01-15 22:18:47 +000012481 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012482 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012483 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012485 int error = FALSE;
12486
12487 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12488 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012489 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012492 else if (argvars[0].v_type == VAR_DICT)
12493 {
12494 if ((d = argvars[0].vval.v_dict) != NULL)
12495 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012496 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012497 if (di != NULL)
12498 tv = &di->di_tv;
12499 }
12500 }
12501 else
12502 EMSG2(_(e_listdictarg), "get()");
12503
12504 if (tv == NULL)
12505 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012506 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012507 copy_tv(&argvars[2], rettv);
12508 }
12509 else
12510 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511}
12512
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012513static 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 +000012514
12515/*
12516 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012517 * Return a range (from start to end) of lines in rettv from the specified
12518 * buffer.
12519 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012520 */
12521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012522get_buffer_lines(
12523 buf_T *buf,
12524 linenr_T start,
12525 linenr_T end,
12526 int retlist,
12527 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012528{
12529 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012530
Bram Moolenaar959a1432013-12-14 12:17:38 +010012531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012533 if (retlist && rettv_list_alloc(rettv) == FAIL)
12534 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012535
12536 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12537 return;
12538
12539 if (!retlist)
12540 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012541 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12542 p = ml_get_buf(buf, start, FALSE);
12543 else
12544 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012545 rettv->vval.v_string = vim_strsave(p);
12546 }
12547 else
12548 {
12549 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012550 return;
12551
12552 if (start < 1)
12553 start = 1;
12554 if (end > buf->b_ml.ml_line_count)
12555 end = buf->b_ml.ml_line_count;
12556 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012557 if (list_append_string(rettv->vval.v_list,
12558 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012559 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012560 }
12561}
12562
12563/*
12564 * "getbufline()" function
12565 */
12566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012567f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012568{
12569 linenr_T lnum;
12570 linenr_T end;
12571 buf_T *buf;
12572
12573 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12574 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012575 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012576 --emsg_off;
12577
Bram Moolenaar661b1822005-07-28 22:36:45 +000012578 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012579 if (argvars[2].v_type == VAR_UNKNOWN)
12580 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012581 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012582 end = get_tv_lnum_buf(&argvars[2], buf);
12583
Bram Moolenaar342337a2005-07-21 21:11:17 +000012584 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012585}
12586
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587/*
12588 * "getbufvar()" function
12589 */
12590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012591f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592{
12593 buf_T *buf;
12594 buf_T *save_curbuf;
12595 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012596 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012597 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012599 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12600 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012602 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012603
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012604 rettv->v_type = VAR_STRING;
12605 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012606
12607 if (buf != NULL && varname != NULL)
12608 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012609 /* set curbuf to be our buf, temporarily */
12610 save_curbuf = curbuf;
12611 curbuf = buf;
12612
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012614 {
12615 if (get_option_tv(&varname, rettv, TRUE) == OK)
12616 done = TRUE;
12617 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012618 else if (STRCMP(varname, "changedtick") == 0)
12619 {
12620 rettv->v_type = VAR_NUMBER;
12621 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012622 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012623 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624 else
12625 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012626 /* Look up the variable. */
12627 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12628 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12629 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012631 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012633 done = TRUE;
12634 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012636
12637 /* restore previous notion of curbuf */
12638 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012639 }
12640
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012641 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12642 /* use the default value */
12643 copy_tv(&argvars[2], rettv);
12644
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 --emsg_off;
12646}
12647
12648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 * "getchar()" function
12650 */
12651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012652f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653{
12654 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012655 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012657 /* Position the cursor. Needed after a message that ends in a space. */
12658 windgoto(msg_row, msg_col);
12659
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660 ++no_mapping;
12661 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012662 for (;;)
12663 {
12664 if (argvars[0].v_type == VAR_UNKNOWN)
12665 /* getchar(): blocking wait. */
12666 n = safe_vgetc();
12667 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12668 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012669 n = vpeekc_any();
12670 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012671 /* illegal argument or getchar(0) and no char avail: return zero */
12672 n = 0;
12673 else
12674 /* getchar(0) and char avail: return char */
12675 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012676
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012677 if (n == K_IGNORE)
12678 continue;
12679 break;
12680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 --no_mapping;
12682 --allow_keys;
12683
Bram Moolenaar219b8702006-11-01 14:32:36 +000012684 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12685 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12686 vimvars[VV_MOUSE_COL].vv_nr = 0;
12687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 if (IS_SPECIAL(n) || mod_mask != 0)
12690 {
12691 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12692 int i = 0;
12693
12694 /* Turn a special key into three bytes, plus modifier. */
12695 if (mod_mask != 0)
12696 {
12697 temp[i++] = K_SPECIAL;
12698 temp[i++] = KS_MODIFIER;
12699 temp[i++] = mod_mask;
12700 }
12701 if (IS_SPECIAL(n))
12702 {
12703 temp[i++] = K_SPECIAL;
12704 temp[i++] = K_SECOND(n);
12705 temp[i++] = K_THIRD(n);
12706 }
12707#ifdef FEAT_MBYTE
12708 else if (has_mbyte)
12709 i += (*mb_char2bytes)(n, temp + i);
12710#endif
12711 else
12712 temp[i++] = n;
12713 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->v_type = VAR_STRING;
12715 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012716
12717#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012718 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012719 {
12720 int row = mouse_row;
12721 int col = mouse_col;
12722 win_T *win;
12723 linenr_T lnum;
12724# ifdef FEAT_WINDOWS
12725 win_T *wp;
12726# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012727 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012728
12729 if (row >= 0 && col >= 0)
12730 {
12731 /* Find the window at the mouse coordinates and compute the
12732 * text position. */
12733 win = mouse_find_win(&row, &col);
12734 (void)mouse_comp_pos(win, &row, &col, &lnum);
12735# ifdef FEAT_WINDOWS
12736 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012737 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012738# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012739 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012740 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12741 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12742 }
12743 }
12744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 }
12746}
12747
12748/*
12749 * "getcharmod()" function
12750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012752f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755}
12756
12757/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012758 * "getcharsearch()" function
12759 */
12760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012762{
12763 if (rettv_dict_alloc(rettv) != FAIL)
12764 {
12765 dict_T *dict = rettv->vval.v_dict;
12766
12767 dict_add_nr_str(dict, "char", 0L, last_csearch());
12768 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12769 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12770 }
12771}
12772
12773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 * "getcmdline()" function
12775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012777f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 rettv->v_type = VAR_STRING;
12780 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781}
12782
12783/*
12784 * "getcmdpos()" function
12785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012787f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790}
12791
12792/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012793 * "getcmdtype()" function
12794 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012796f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012797{
12798 rettv->v_type = VAR_STRING;
12799 rettv->vval.v_string = alloc(2);
12800 if (rettv->vval.v_string != NULL)
12801 {
12802 rettv->vval.v_string[0] = get_cmdline_type();
12803 rettv->vval.v_string[1] = NUL;
12804 }
12805}
12806
12807/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012808 * "getcmdwintype()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012812{
12813 rettv->v_type = VAR_STRING;
12814 rettv->vval.v_string = NULL;
12815#ifdef FEAT_CMDWIN
12816 rettv->vval.v_string = alloc(2);
12817 if (rettv->vval.v_string != NULL)
12818 {
12819 rettv->vval.v_string[0] = cmdwin_type;
12820 rettv->vval.v_string[1] = NUL;
12821 }
12822#endif
12823}
12824
12825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 * "getcwd()" function
12827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012829f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012831 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012832 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012835 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012836
12837 wp = find_tabwin(&argvars[0], &argvars[1]);
12838 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012840 if (wp->w_localdir != NULL)
12841 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12842 else if(globaldir != NULL)
12843 rettv->vval.v_string = vim_strsave(globaldir);
12844 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012845 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012846 cwd = alloc(MAXPATHL);
12847 if (cwd != NULL)
12848 {
12849 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12850 rettv->vval.v_string = vim_strsave(cwd);
12851 vim_free(cwd);
12852 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012853 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012854#ifdef BACKSLASH_IN_FILENAME
12855 if (rettv->vval.v_string != NULL)
12856 slash_adjust(rettv->vval.v_string);
12857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 }
12859}
12860
12861/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012862 * "getfontname()" function
12863 */
12864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012865f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012866{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012867 rettv->v_type = VAR_STRING;
12868 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012869#ifdef FEAT_GUI
12870 if (gui.in_use)
12871 {
12872 GuiFont font;
12873 char_u *name = NULL;
12874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012875 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012876 {
12877 /* Get the "Normal" font. Either the name saved by
12878 * hl_set_font_name() or from the font ID. */
12879 font = gui.norm_font;
12880 name = hl_get_font_name();
12881 }
12882 else
12883 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012885 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12886 return;
12887 font = gui_mch_get_font(name, FALSE);
12888 if (font == NOFONT)
12889 return; /* Invalid font name, return empty string. */
12890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012892 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012893 gui_mch_free_font(font);
12894 }
12895#endif
12896}
12897
12898/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012899 * "getfperm({fname})" function
12900 */
12901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012902f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012903{
12904 char_u *fname;
12905 struct stat st;
12906 char_u *perm = NULL;
12907 char_u flags[] = "rwx";
12908 int i;
12909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012912 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012913 if (mch_stat((char *)fname, &st) >= 0)
12914 {
12915 perm = vim_strsave((char_u *)"---------");
12916 if (perm != NULL)
12917 {
12918 for (i = 0; i < 9; i++)
12919 {
12920 if (st.st_mode & (1 << (8 - i)))
12921 perm[i] = flags[i % 3];
12922 }
12923 }
12924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012925 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012926}
12927
12928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 * "getfsize({fname})" function
12930 */
12931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012932f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933{
12934 char_u *fname;
12935 struct stat st;
12936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012937 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940
12941 if (mch_stat((char *)fname, &st) >= 0)
12942 {
12943 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012946 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012948
12949 /* non-perfect check for overflow */
12950 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12951 rettv->vval.v_number = -2;
12952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 }
12954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956}
12957
12958/*
12959 * "getftime({fname})" function
12960 */
12961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012962f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963{
12964 char_u *fname;
12965 struct stat st;
12966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968
12969 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973}
12974
12975/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012976 * "getftype({fname})" function
12977 */
12978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012979f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012980{
12981 char_u *fname;
12982 struct stat st;
12983 char_u *type = NULL;
12984 char *t;
12985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012989 if (mch_lstat((char *)fname, &st) >= 0)
12990 {
12991#ifdef S_ISREG
12992 if (S_ISREG(st.st_mode))
12993 t = "file";
12994 else if (S_ISDIR(st.st_mode))
12995 t = "dir";
12996# ifdef S_ISLNK
12997 else if (S_ISLNK(st.st_mode))
12998 t = "link";
12999# endif
13000# ifdef S_ISBLK
13001 else if (S_ISBLK(st.st_mode))
13002 t = "bdev";
13003# endif
13004# ifdef S_ISCHR
13005 else if (S_ISCHR(st.st_mode))
13006 t = "cdev";
13007# endif
13008# ifdef S_ISFIFO
13009 else if (S_ISFIFO(st.st_mode))
13010 t = "fifo";
13011# endif
13012# ifdef S_ISSOCK
13013 else if (S_ISSOCK(st.st_mode))
13014 t = "fifo";
13015# endif
13016 else
13017 t = "other";
13018#else
13019# ifdef S_IFMT
13020 switch (st.st_mode & S_IFMT)
13021 {
13022 case S_IFREG: t = "file"; break;
13023 case S_IFDIR: t = "dir"; break;
13024# ifdef S_IFLNK
13025 case S_IFLNK: t = "link"; break;
13026# endif
13027# ifdef S_IFBLK
13028 case S_IFBLK: t = "bdev"; break;
13029# endif
13030# ifdef S_IFCHR
13031 case S_IFCHR: t = "cdev"; break;
13032# endif
13033# ifdef S_IFIFO
13034 case S_IFIFO: t = "fifo"; break;
13035# endif
13036# ifdef S_IFSOCK
13037 case S_IFSOCK: t = "socket"; break;
13038# endif
13039 default: t = "other";
13040 }
13041# else
13042 if (mch_isdir(fname))
13043 t = "dir";
13044 else
13045 t = "file";
13046# endif
13047#endif
13048 type = vim_strsave((char_u *)t);
13049 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013051}
13052
13053/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013054 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013055 */
13056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013058{
13059 linenr_T lnum;
13060 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013061 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062
13063 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013064 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013065 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013066 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013067 retlist = FALSE;
13068 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013070 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013071 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013072 retlist = TRUE;
13073 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013074
Bram Moolenaar342337a2005-07-21 21:11:17 +000013075 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076}
13077
13078/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013079 * "getmatches()" function
13080 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013082f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013083{
13084#ifdef FEAT_SEARCH_EXTRA
13085 dict_T *dict;
13086 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013087 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013088
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013089 if (rettv_list_alloc(rettv) == OK)
13090 {
13091 while (cur != NULL)
13092 {
13093 dict = dict_alloc();
13094 if (dict == NULL)
13095 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013096 if (cur->match.regprog == NULL)
13097 {
13098 /* match added with matchaddpos() */
13099 for (i = 0; i < MAXPOSMATCH; ++i)
13100 {
13101 llpos_T *llpos;
13102 char buf[6];
13103 list_T *l;
13104
13105 llpos = &cur->pos.pos[i];
13106 if (llpos->lnum == 0)
13107 break;
13108 l = list_alloc();
13109 if (l == NULL)
13110 break;
13111 list_append_number(l, (varnumber_T)llpos->lnum);
13112 if (llpos->col > 0)
13113 {
13114 list_append_number(l, (varnumber_T)llpos->col);
13115 list_append_number(l, (varnumber_T)llpos->len);
13116 }
13117 sprintf(buf, "pos%d", i + 1);
13118 dict_add_list(dict, buf, l);
13119 }
13120 }
13121 else
13122 {
13123 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13124 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013125 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013126 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13127 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013128# ifdef FEAT_CONCEAL
13129 if (cur->conceal_char)
13130 {
13131 char_u buf[MB_MAXBYTES + 1];
13132
13133 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13134 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13135 }
13136# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013137 list_append_dict(rettv->vval.v_list, dict);
13138 cur = cur->next;
13139 }
13140 }
13141#endif
13142}
13143
13144/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013145 * "getpid()" function
13146 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013148f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013149{
13150 rettv->vval.v_number = mch_get_pid();
13151}
13152
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013153static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013154
13155/*
13156 * "getcurpos()" function
13157 */
13158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013159f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013160{
13161 getpos_both(argvars, rettv, TRUE);
13162}
13163
Bram Moolenaar18081e32008-02-20 19:11:07 +000013164/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013165 * "getpos(string)" function
13166 */
13167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013168f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013169{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013170 getpos_both(argvars, rettv, FALSE);
13171}
13172
13173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013174getpos_both(
13175 typval_T *argvars,
13176 typval_T *rettv,
13177 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013178{
Bram Moolenaara5525202006-03-02 22:52:09 +000013179 pos_T *fp;
13180 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013181 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013182
13183 if (rettv_list_alloc(rettv) == OK)
13184 {
13185 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013186 if (getcurpos)
13187 fp = &curwin->w_cursor;
13188 else
13189 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013190 if (fnum != -1)
13191 list_append_number(l, (varnumber_T)fnum);
13192 else
13193 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013194 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13195 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013196 list_append_number(l, (fp != NULL)
13197 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013198 : (varnumber_T)0);
13199 list_append_number(l,
13200#ifdef FEAT_VIRTUALEDIT
13201 (fp != NULL) ? (varnumber_T)fp->coladd :
13202#endif
13203 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013204 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013205 {
13206 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013207 list_append_number(l, curwin->w_curswant == MAXCOL ?
13208 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013209 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013210 }
13211 else
13212 rettv->vval.v_number = FALSE;
13213}
13214
13215/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013216 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013217 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013219f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013220{
13221#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013222 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013223#endif
13224
Bram Moolenaar2641f772005-03-25 21:58:17 +000013225#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013226 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013227 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013228 wp = NULL;
13229 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13230 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013231 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013232 if (wp == NULL)
13233 return;
13234 }
13235
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013236 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013237 }
13238#endif
13239}
13240
13241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 * "getreg()" function
13243 */
13244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013245f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
13247 char_u *strregname;
13248 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013249 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013250 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013253 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013255 strregname = get_tv_string_chk(&argvars[0]);
13256 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013257 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013259 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013260 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13261 return_list = get_tv_number_chk(&argvars[2], &error);
13262 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013265 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013266
13267 if (error)
13268 return;
13269
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 regname = (strregname == NULL ? '"' : *strregname);
13271 if (regname == 0)
13272 regname = '"';
13273
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013274 if (return_list)
13275 {
13276 rettv->v_type = VAR_LIST;
13277 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13278 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013279 if (rettv->vval.v_list != NULL)
13280 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013281 }
13282 else
13283 {
13284 rettv->v_type = VAR_STRING;
13285 rettv->vval.v_string = get_reg_contents(regname,
13286 arg2 ? GREG_EXPR_SRC : 0);
13287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288}
13289
13290/*
13291 * "getregtype()" function
13292 */
13293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013294f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295{
13296 char_u *strregname;
13297 int regname;
13298 char_u buf[NUMBUFLEN + 2];
13299 long reglen = 0;
13300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013301 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 {
13303 strregname = get_tv_string_chk(&argvars[0]);
13304 if (strregname == NULL) /* type error; errmsg already given */
13305 {
13306 rettv->v_type = VAR_STRING;
13307 rettv->vval.v_string = NULL;
13308 return;
13309 }
13310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 else
13312 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013313 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314
13315 regname = (strregname == NULL ? '"' : *strregname);
13316 if (regname == 0)
13317 regname = '"';
13318
13319 buf[0] = NUL;
13320 buf[1] = NUL;
13321 switch (get_reg_type(regname, &reglen))
13322 {
13323 case MLINE: buf[0] = 'V'; break;
13324 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 case MBLOCK:
13326 buf[0] = Ctrl_V;
13327 sprintf((char *)buf + 1, "%ld", reglen + 1);
13328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 rettv->v_type = VAR_STRING;
13331 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332}
13333
13334/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013335 * "gettabvar()" function
13336 */
13337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013338f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013339{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013340 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013341 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013342 dictitem_T *v;
13343 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013344 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013345
13346 rettv->v_type = VAR_STRING;
13347 rettv->vval.v_string = NULL;
13348
13349 varname = get_tv_string_chk(&argvars[1]);
13350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13351 if (tp != NULL && varname != NULL)
13352 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013353 /* Set tp to be our tabpage, temporarily. Also set the window to the
13354 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013355 if (switch_win(&oldcurwin, &oldtabpage,
13356 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013357 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013358 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013359 /* look up the variable */
13360 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13361 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13362 if (v != NULL)
13363 {
13364 copy_tv(&v->di_tv, rettv);
13365 done = TRUE;
13366 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013367 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013368
13369 /* restore previous notion of curwin */
13370 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013371 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013372
13373 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013374 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013375}
13376
13377/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013378 * "gettabwinvar()" function
13379 */
13380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013381f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013382{
13383 getwinvar(argvars, rettv, 1);
13384}
13385
13386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 * "getwinposx()" function
13388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013390f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013392 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef FEAT_GUI
13394 if (gui.in_use)
13395 {
13396 int x, y;
13397
13398 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400 }
13401#endif
13402}
13403
13404/*
13405 * "getwinposy()" function
13406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013408f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411#ifdef FEAT_GUI
13412 if (gui.in_use)
13413 {
13414 int x, y;
13415
13416 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 }
13419#endif
13420}
13421
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013422/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013423 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013424 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013425 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013426find_win_by_nr(
13427 typval_T *vp,
13428 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013429{
13430#ifdef FEAT_WINDOWS
13431 win_T *wp;
13432#endif
13433 int nr;
13434
13435 nr = get_tv_number_chk(vp, NULL);
13436
13437#ifdef FEAT_WINDOWS
13438 if (nr < 0)
13439 return NULL;
13440 if (nr == 0)
13441 return curwin;
13442
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013443 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13444 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013445 if (--nr <= 0)
13446 break;
13447 return wp;
13448#else
13449 if (nr == 0 || nr == 1)
13450 return curwin;
13451 return NULL;
13452#endif
13453}
13454
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013456 * Find window specified by "wvp" in tabpage "tvp".
13457 */
13458 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013459find_tabwin(
13460 typval_T *wvp, /* VAR_UNKNOWN for current window */
13461 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013462{
13463 win_T *wp = NULL;
13464 tabpage_T *tp = NULL;
13465 long n;
13466
13467 if (wvp->v_type != VAR_UNKNOWN)
13468 {
13469 if (tvp->v_type != VAR_UNKNOWN)
13470 {
13471 n = get_tv_number(tvp);
13472 if (n >= 0)
13473 tp = find_tabpage(n);
13474 }
13475 else
13476 tp = curtab;
13477
13478 if (tp != NULL)
13479 wp = find_win_by_nr(wvp, tp);
13480 }
13481 else
13482 wp = curwin;
13483
13484 return wp;
13485}
13486
13487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 * "getwinvar()" function
13489 */
13490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013491f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013493 getwinvar(argvars, rettv, 0);
13494}
13495
13496/*
13497 * getwinvar() and gettabwinvar()
13498 */
13499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013500getwinvar(
13501 typval_T *argvars,
13502 typval_T *rettv,
13503 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013504{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013505 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013507 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013508 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013509 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013510#ifdef FEAT_WINDOWS
13511 win_T *oldcurwin;
13512 tabpage_T *oldtabpage;
13513 int need_switch_win;
13514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013516#ifdef FEAT_WINDOWS
13517 if (off == 1)
13518 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13519 else
13520 tp = curtab;
13521#endif
13522 win = find_win_by_nr(&argvars[off], tp);
13523 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013526 rettv->v_type = VAR_STRING;
13527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528
13529 if (win != NULL && varname != NULL)
13530 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013531#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013532 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013533 * otherwise the window is not valid. Only do this when needed,
13534 * autocommands get blocked. */
13535 need_switch_win = !(tp == curtab && win == curwin);
13536 if (!need_switch_win
13537 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13538#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013539 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013540 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013541 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013542 if (get_option_tv(&varname, rettv, 1) == OK)
13543 done = TRUE;
13544 }
13545 else
13546 {
13547 /* Look up the variable. */
13548 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13549 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13550 varname, FALSE);
13551 if (v != NULL)
13552 {
13553 copy_tv(&v->di_tv, rettv);
13554 done = TRUE;
13555 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013558
Bram Moolenaarba117c22015-09-29 16:53:22 +020013559#ifdef FEAT_WINDOWS
13560 if (need_switch_win)
13561 /* restore previous notion of curwin */
13562 restore_win(oldcurwin, oldtabpage, TRUE);
13563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 }
13565
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013566 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13567 /* use the default return value */
13568 copy_tv(&argvars[off + 2], rettv);
13569
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 --emsg_off;
13571}
13572
13573/*
13574 * "glob()" function
13575 */
13576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013577f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013579 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013581 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013583 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013584 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013586 if (argvars[1].v_type != VAR_UNKNOWN)
13587 {
13588 if (get_tv_number_chk(&argvars[1], &error))
13589 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013590 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013591 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013592 if (get_tv_number_chk(&argvars[2], &error))
13593 {
13594 rettv->v_type = VAR_LIST;
13595 rettv->vval.v_list = NULL;
13596 }
13597 if (argvars[3].v_type != VAR_UNKNOWN
13598 && get_tv_number_chk(&argvars[3], &error))
13599 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013600 }
13601 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013602 if (!error)
13603 {
13604 ExpandInit(&xpc);
13605 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013606 if (p_wic)
13607 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013608 if (rettv->v_type == VAR_STRING)
13609 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013610 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013611 else if (rettv_list_alloc(rettv) != FAIL)
13612 {
13613 int i;
13614
13615 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13616 NULL, options, WILD_ALL_KEEP);
13617 for (i = 0; i < xpc.xp_numfiles; i++)
13618 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13619
13620 ExpandCleanup(&xpc);
13621 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013622 }
13623 else
13624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
13627/*
13628 * "globpath()" function
13629 */
13630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013631f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013633 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013636 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013637 garray_T ga;
13638 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013640 /* When the optional second argument is non-zero, don't remove matches
13641 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013643 if (argvars[2].v_type != VAR_UNKNOWN)
13644 {
13645 if (get_tv_number_chk(&argvars[2], &error))
13646 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013647 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013648 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013649 if (get_tv_number_chk(&argvars[3], &error))
13650 {
13651 rettv->v_type = VAR_LIST;
13652 rettv->vval.v_list = NULL;
13653 }
13654 if (argvars[4].v_type != VAR_UNKNOWN
13655 && get_tv_number_chk(&argvars[4], &error))
13656 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013657 }
13658 }
13659 if (file != NULL && !error)
13660 {
13661 ga_init2(&ga, (int)sizeof(char_u *), 10);
13662 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13663 if (rettv->v_type == VAR_STRING)
13664 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13665 else if (rettv_list_alloc(rettv) != FAIL)
13666 for (i = 0; i < ga.ga_len; ++i)
13667 list_append_string(rettv->vval.v_list,
13668 ((char_u **)(ga.ga_data))[i], -1);
13669 ga_clear_strings(&ga);
13670 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673}
13674
13675/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013676 * "glob2regpat()" function
13677 */
13678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013679f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013680{
13681 char_u *pat = get_tv_string_chk(&argvars[0]);
13682
13683 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013684 rettv->vval.v_string = (pat == NULL)
13685 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013686}
13687
13688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689 * "has()" function
13690 */
13691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013692f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693{
13694 int i;
13695 char_u *name;
13696 int n = FALSE;
13697 static char *(has_list[]) =
13698 {
13699#ifdef AMIGA
13700 "amiga",
13701# ifdef FEAT_ARP
13702 "arp",
13703# endif
13704#endif
13705#ifdef __BEOS__
13706 "beos",
13707#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013708#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 "mac",
13710#endif
13711#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013712 "macunix", /* built with 'darwin' enabled */
13713#endif
13714#if defined(__APPLE__) && __APPLE__ == 1
13715 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#ifdef __QNX__
13718 "qnx",
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef UNIX
13721 "unix",
13722#endif
13723#ifdef VMS
13724 "vms",
13725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726#ifdef WIN32
13727 "win32",
13728#endif
13729#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13730 "win32unix",
13731#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013732#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 "win64",
13734#endif
13735#ifdef EBCDIC
13736 "ebcdic",
13737#endif
13738#ifndef CASE_INSENSITIVE_FILENAME
13739 "fname_case",
13740#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013741#ifdef HAVE_ACL
13742 "acl",
13743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#ifdef FEAT_ARABIC
13745 "arabic",
13746#endif
13747#ifdef FEAT_AUTOCMD
13748 "autocmd",
13749#endif
13750#ifdef FEAT_BEVAL
13751 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013752# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13753 "balloon_multiline",
13754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#endif
13756#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13757 "builtin_terms",
13758# ifdef ALL_BUILTIN_TCAPS
13759 "all_builtin_terms",
13760# endif
13761#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013762#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13763 || defined(FEAT_GUI_W32) \
13764 || defined(FEAT_GUI_MOTIF))
13765 "browsefilter",
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef FEAT_BYTEOFF
13768 "byte_offset",
13769#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013770#ifdef FEAT_CHANNEL
13771 "channel",
13772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773#ifdef FEAT_CINDENT
13774 "cindent",
13775#endif
13776#ifdef FEAT_CLIENTSERVER
13777 "clientserver",
13778#endif
13779#ifdef FEAT_CLIPBOARD
13780 "clipboard",
13781#endif
13782#ifdef FEAT_CMDL_COMPL
13783 "cmdline_compl",
13784#endif
13785#ifdef FEAT_CMDHIST
13786 "cmdline_hist",
13787#endif
13788#ifdef FEAT_COMMENTS
13789 "comments",
13790#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013791#ifdef FEAT_CONCEAL
13792 "conceal",
13793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef FEAT_CRYPT
13795 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013796 "crypt-blowfish",
13797 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#endif
13799#ifdef FEAT_CSCOPE
13800 "cscope",
13801#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013802#ifdef FEAT_CURSORBIND
13803 "cursorbind",
13804#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013805#ifdef CURSOR_SHAPE
13806 "cursorshape",
13807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808#ifdef DEBUG
13809 "debug",
13810#endif
13811#ifdef FEAT_CON_DIALOG
13812 "dialog_con",
13813#endif
13814#ifdef FEAT_GUI_DIALOG
13815 "dialog_gui",
13816#endif
13817#ifdef FEAT_DIFF
13818 "diff",
13819#endif
13820#ifdef FEAT_DIGRAPHS
13821 "digraphs",
13822#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013823#ifdef FEAT_DIRECTX
13824 "directx",
13825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#ifdef FEAT_DND
13827 "dnd",
13828#endif
13829#ifdef FEAT_EMACS_TAGS
13830 "emacs_tags",
13831#endif
13832 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013833 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834#ifdef FEAT_SEARCH_EXTRA
13835 "extra_search",
13836#endif
13837#ifdef FEAT_FKMAP
13838 "farsi",
13839#endif
13840#ifdef FEAT_SEARCHPATH
13841 "file_in_path",
13842#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013843#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013844 "filterpipe",
13845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846#ifdef FEAT_FIND_ID
13847 "find_in_path",
13848#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013849#ifdef FEAT_FLOAT
13850 "float",
13851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852#ifdef FEAT_FOLDING
13853 "folding",
13854#endif
13855#ifdef FEAT_FOOTER
13856 "footer",
13857#endif
13858#if !defined(USE_SYSTEM) && defined(UNIX)
13859 "fork",
13860#endif
13861#ifdef FEAT_GETTEXT
13862 "gettext",
13863#endif
13864#ifdef FEAT_GUI
13865 "gui",
13866#endif
13867#ifdef FEAT_GUI_ATHENA
13868# ifdef FEAT_GUI_NEXTAW
13869 "gui_neXtaw",
13870# else
13871 "gui_athena",
13872# endif
13873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#ifdef FEAT_GUI_GTK
13875 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013876# ifdef USE_GTK3
13877 "gui_gtk3",
13878# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013882#ifdef FEAT_GUI_GNOME
13883 "gui_gnome",
13884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885#ifdef FEAT_GUI_MAC
13886 "gui_mac",
13887#endif
13888#ifdef FEAT_GUI_MOTIF
13889 "gui_motif",
13890#endif
13891#ifdef FEAT_GUI_PHOTON
13892 "gui_photon",
13893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894#ifdef FEAT_GUI_W32
13895 "gui_win32",
13896#endif
13897#ifdef FEAT_HANGULIN
13898 "hangul_input",
13899#endif
13900#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13901 "iconv",
13902#endif
13903#ifdef FEAT_INS_EXPAND
13904 "insert_expand",
13905#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013906#ifdef FEAT_JOB
13907 "job",
13908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909#ifdef FEAT_JUMPLIST
13910 "jumplist",
13911#endif
13912#ifdef FEAT_KEYMAP
13913 "keymap",
13914#endif
13915#ifdef FEAT_LANGMAP
13916 "langmap",
13917#endif
13918#ifdef FEAT_LIBCALL
13919 "libcall",
13920#endif
13921#ifdef FEAT_LINEBREAK
13922 "linebreak",
13923#endif
13924#ifdef FEAT_LISP
13925 "lispindent",
13926#endif
13927#ifdef FEAT_LISTCMDS
13928 "listcmds",
13929#endif
13930#ifdef FEAT_LOCALMAP
13931 "localmap",
13932#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013933#ifdef FEAT_LUA
13934# ifndef DYNAMIC_LUA
13935 "lua",
13936# endif
13937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938#ifdef FEAT_MENU
13939 "menu",
13940#endif
13941#ifdef FEAT_SESSION
13942 "mksession",
13943#endif
13944#ifdef FEAT_MODIFY_FNAME
13945 "modify_fname",
13946#endif
13947#ifdef FEAT_MOUSE
13948 "mouse",
13949#endif
13950#ifdef FEAT_MOUSESHAPE
13951 "mouseshape",
13952#endif
13953#if defined(UNIX) || defined(VMS)
13954# ifdef FEAT_MOUSE_DEC
13955 "mouse_dec",
13956# endif
13957# ifdef FEAT_MOUSE_GPM
13958 "mouse_gpm",
13959# endif
13960# ifdef FEAT_MOUSE_JSB
13961 "mouse_jsbterm",
13962# endif
13963# ifdef FEAT_MOUSE_NET
13964 "mouse_netterm",
13965# endif
13966# ifdef FEAT_MOUSE_PTERM
13967 "mouse_pterm",
13968# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013969# ifdef FEAT_MOUSE_SGR
13970 "mouse_sgr",
13971# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013972# ifdef FEAT_SYSMOUSE
13973 "mouse_sysmouse",
13974# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013975# ifdef FEAT_MOUSE_URXVT
13976 "mouse_urxvt",
13977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978# ifdef FEAT_MOUSE_XTERM
13979 "mouse_xterm",
13980# endif
13981#endif
13982#ifdef FEAT_MBYTE
13983 "multi_byte",
13984#endif
13985#ifdef FEAT_MBYTE_IME
13986 "multi_byte_ime",
13987#endif
13988#ifdef FEAT_MULTI_LANG
13989 "multi_lang",
13990#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013991#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013992#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013993 "mzscheme",
13994#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996#ifdef FEAT_OLE
13997 "ole",
13998#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013999 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000#ifdef FEAT_PATH_EXTRA
14001 "path_extra",
14002#endif
14003#ifdef FEAT_PERL
14004#ifndef DYNAMIC_PERL
14005 "perl",
14006#endif
14007#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014008#ifdef FEAT_PERSISTENT_UNDO
14009 "persistent_undo",
14010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef FEAT_PYTHON
14012#ifndef DYNAMIC_PYTHON
14013 "python",
14014#endif
14015#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014016#ifdef FEAT_PYTHON3
14017#ifndef DYNAMIC_PYTHON3
14018 "python3",
14019#endif
14020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021#ifdef FEAT_POSTSCRIPT
14022 "postscript",
14023#endif
14024#ifdef FEAT_PRINTER
14025 "printer",
14026#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014027#ifdef FEAT_PROFILE
14028 "profile",
14029#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014030#ifdef FEAT_RELTIME
14031 "reltime",
14032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef FEAT_QUICKFIX
14034 "quickfix",
14035#endif
14036#ifdef FEAT_RIGHTLEFT
14037 "rightleft",
14038#endif
14039#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14040 "ruby",
14041#endif
14042#ifdef FEAT_SCROLLBIND
14043 "scrollbind",
14044#endif
14045#ifdef FEAT_CMDL_INFO
14046 "showcmd",
14047 "cmdline_info",
14048#endif
14049#ifdef FEAT_SIGNS
14050 "signs",
14051#endif
14052#ifdef FEAT_SMARTINDENT
14053 "smartindent",
14054#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014055#ifdef STARTUPTIME
14056 "startuptime",
14057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058#ifdef FEAT_STL_OPT
14059 "statusline",
14060#endif
14061#ifdef FEAT_SUN_WORKSHOP
14062 "sun_workshop",
14063#endif
14064#ifdef FEAT_NETBEANS_INTG
14065 "netbeans_intg",
14066#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014067#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014068 "spell",
14069#endif
14070#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 "syntax",
14072#endif
14073#if defined(USE_SYSTEM) || !defined(UNIX)
14074 "system",
14075#endif
14076#ifdef FEAT_TAG_BINS
14077 "tag_binary",
14078#endif
14079#ifdef FEAT_TAG_OLDSTATIC
14080 "tag_old_static",
14081#endif
14082#ifdef FEAT_TAG_ANYWHITE
14083 "tag_any_white",
14084#endif
14085#ifdef FEAT_TCL
14086# ifndef DYNAMIC_TCL
14087 "tcl",
14088# endif
14089#endif
14090#ifdef TERMINFO
14091 "terminfo",
14092#endif
14093#ifdef FEAT_TERMRESPONSE
14094 "termresponse",
14095#endif
14096#ifdef FEAT_TEXTOBJ
14097 "textobjects",
14098#endif
14099#ifdef HAVE_TGETENT
14100 "tgetent",
14101#endif
14102#ifdef FEAT_TITLE
14103 "title",
14104#endif
14105#ifdef FEAT_TOOLBAR
14106 "toolbar",
14107#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014108#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14109 "unnamedplus",
14110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111#ifdef FEAT_USR_CMDS
14112 "user-commands", /* was accidentally included in 5.4 */
14113 "user_commands",
14114#endif
14115#ifdef FEAT_VIMINFO
14116 "viminfo",
14117#endif
14118#ifdef FEAT_VERTSPLIT
14119 "vertsplit",
14120#endif
14121#ifdef FEAT_VIRTUALEDIT
14122 "virtualedit",
14123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#ifdef FEAT_VISUALEXTRA
14126 "visualextra",
14127#endif
14128#ifdef FEAT_VREPLACE
14129 "vreplace",
14130#endif
14131#ifdef FEAT_WILDIGN
14132 "wildignore",
14133#endif
14134#ifdef FEAT_WILDMENU
14135 "wildmenu",
14136#endif
14137#ifdef FEAT_WINDOWS
14138 "windows",
14139#endif
14140#ifdef FEAT_WAK
14141 "winaltkeys",
14142#endif
14143#ifdef FEAT_WRITEBACKUP
14144 "writebackup",
14145#endif
14146#ifdef FEAT_XIM
14147 "xim",
14148#endif
14149#ifdef FEAT_XFONTSET
14150 "xfontset",
14151#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014152#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014153 "xpm",
14154 "xpm_w32", /* for backward compatibility */
14155#else
14156# if defined(HAVE_XPM)
14157 "xpm",
14158# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160#ifdef USE_XSMP
14161 "xsmp",
14162#endif
14163#ifdef USE_XSMP_INTERACT
14164 "xsmp_interact",
14165#endif
14166#ifdef FEAT_XCLIPBOARD
14167 "xterm_clipboard",
14168#endif
14169#ifdef FEAT_XTERM_SAVE
14170 "xterm_save",
14171#endif
14172#if defined(UNIX) && defined(FEAT_X11)
14173 "X11",
14174#endif
14175 NULL
14176 };
14177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 for (i = 0; has_list[i] != NULL; ++i)
14180 if (STRICMP(name, has_list[i]) == 0)
14181 {
14182 n = TRUE;
14183 break;
14184 }
14185
14186 if (n == FALSE)
14187 {
14188 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014189 {
14190 if (name[5] == '-'
14191 && STRLEN(name) > 11
14192 && vim_isdigit(name[6])
14193 && vim_isdigit(name[8])
14194 && vim_isdigit(name[10]))
14195 {
14196 int major = atoi((char *)name + 6);
14197 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014198
14199 /* Expect "patch-9.9.01234". */
14200 n = (major < VIM_VERSION_MAJOR
14201 || (major == VIM_VERSION_MAJOR
14202 && (minor < VIM_VERSION_MINOR
14203 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014204 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014205 }
14206 else
14207 n = has_patch(atoi((char *)name + 5));
14208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 else if (STRICMP(name, "vim_starting") == 0)
14210 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014211#ifdef FEAT_MBYTE
14212 else if (STRICMP(name, "multi_byte_encoding") == 0)
14213 n = has_mbyte;
14214#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014215#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14216 else if (STRICMP(name, "balloon_multiline") == 0)
14217 n = multiline_balloon_available();
14218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef DYNAMIC_TCL
14220 else if (STRICMP(name, "tcl") == 0)
14221 n = tcl_enabled(FALSE);
14222#endif
14223#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14224 else if (STRICMP(name, "iconv") == 0)
14225 n = iconv_enabled(FALSE);
14226#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014227#ifdef DYNAMIC_LUA
14228 else if (STRICMP(name, "lua") == 0)
14229 n = lua_enabled(FALSE);
14230#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014231#ifdef DYNAMIC_MZSCHEME
14232 else if (STRICMP(name, "mzscheme") == 0)
14233 n = mzscheme_enabled(FALSE);
14234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235#ifdef DYNAMIC_RUBY
14236 else if (STRICMP(name, "ruby") == 0)
14237 n = ruby_enabled(FALSE);
14238#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014239#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240#ifdef DYNAMIC_PYTHON
14241 else if (STRICMP(name, "python") == 0)
14242 n = python_enabled(FALSE);
14243#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014244#endif
14245#ifdef FEAT_PYTHON3
14246#ifdef DYNAMIC_PYTHON3
14247 else if (STRICMP(name, "python3") == 0)
14248 n = python3_enabled(FALSE);
14249#endif
14250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251#ifdef DYNAMIC_PERL
14252 else if (STRICMP(name, "perl") == 0)
14253 n = perl_enabled(FALSE);
14254#endif
14255#ifdef FEAT_GUI
14256 else if (STRICMP(name, "gui_running") == 0)
14257 n = (gui.in_use || gui.starting);
14258# ifdef FEAT_GUI_W32
14259 else if (STRICMP(name, "gui_win32s") == 0)
14260 n = gui_is_win32s();
14261# endif
14262# ifdef FEAT_BROWSE
14263 else if (STRICMP(name, "browse") == 0)
14264 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14265# endif
14266#endif
14267#ifdef FEAT_SYN_HL
14268 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014269 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270#endif
14271#if defined(WIN3264)
14272 else if (STRICMP(name, "win95") == 0)
14273 n = mch_windows95();
14274#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014275#ifdef FEAT_NETBEANS_INTG
14276 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014277 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 }
14280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282}
14283
14284/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014285 * "has_key()" function
14286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014289{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014290 if (argvars[0].v_type != VAR_DICT)
14291 {
14292 EMSG(_(e_dictreq));
14293 return;
14294 }
14295 if (argvars[0].vval.v_dict == NULL)
14296 return;
14297
14298 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014299 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014300}
14301
14302/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014303 * "haslocaldir()" function
14304 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014307{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014308 win_T *wp = NULL;
14309
14310 wp = find_tabwin(&argvars[0], &argvars[1]);
14311 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014312}
14313
14314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 * "hasmapto()" function
14316 */
14317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014318f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
14320 char_u *name;
14321 char_u *mode;
14322 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014323 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 mode = (char_u *)"nvo";
14328 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014331 if (argvars[2].v_type != VAR_UNKNOWN)
14332 abbr = get_tv_number(&argvars[2]);
14333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334
Bram Moolenaar2c932302006-03-18 21:42:09 +000014335 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
14342 * "histadd()" function
14343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014345f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346{
14347#ifdef FEAT_CMDHIST
14348 int histype;
14349 char_u *str;
14350 char_u buf[NUMBUFLEN];
14351#endif
14352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 if (check_restricted() || check_secure())
14355 return;
14356#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14358 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 if (histype >= 0)
14360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 if (*str != NUL)
14363 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014364 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 return;
14368 }
14369 }
14370#endif
14371}
14372
14373/*
14374 * "histdel()" function
14375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014377f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379#ifdef FEAT_CMDHIST
14380 int n;
14381 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14385 if (str == NULL)
14386 n = 0;
14387 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014390 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 else
14395 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 get_tv_string_buf(&argvars[1], buf));
14398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399#endif
14400}
14401
14402/*
14403 * "histget()" function
14404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014406f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408#ifdef FEAT_CMDHIST
14409 int type;
14410 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14414 if (str == NULL)
14415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 {
14418 type = get_histtype(str);
14419 if (argvars[1].v_type == VAR_UNKNOWN)
14420 idx = get_history_idx(type);
14421 else
14422 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14423 /* -1 on type error */
14424 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430}
14431
14432/*
14433 * "histnr()" function
14434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014436f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437{
14438 int i;
14439
14440#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 char_u *history = get_tv_string_chk(&argvars[0]);
14442
14443 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 if (i >= HIST_CMD && i < HIST_COUNT)
14445 i = get_history_idx(i);
14446 else
14447#endif
14448 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450}
14451
14452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 * "highlightID(name)" function
14454 */
14455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014456f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459}
14460
14461/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462 * "highlight_exists()" function
14463 */
14464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014465f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466{
14467 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14468}
14469
14470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 * "hostname()" function
14472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 char_u hostname[256];
14477
14478 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->v_type = VAR_STRING;
14480 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481}
14482
14483/*
14484 * iconv() function
14485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014487f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488{
14489#ifdef FEAT_MBYTE
14490 char_u buf1[NUMBUFLEN];
14491 char_u buf2[NUMBUFLEN];
14492 char_u *from, *to, *str;
14493 vimconv_T vimconv;
14494#endif
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498
14499#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 str = get_tv_string(&argvars[0]);
14501 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14502 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 vimconv.vc_type = CONV_NONE;
14504 convert_setup(&vimconv, from, to);
14505
14506 /* If the encodings are equal, no conversion needed. */
14507 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
14512 convert_setup(&vimconv, NULL, NULL);
14513 vim_free(from);
14514 vim_free(to);
14515#endif
14516}
14517
14518/*
14519 * "indent()" function
14520 */
14521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014522f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523{
14524 linenr_T lnum;
14525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531}
14532
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014533/*
14534 * "index()" function
14535 */
14536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014537f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014538{
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 list_T *l;
14540 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014541 long idx = 0;
14542 int ic = FALSE;
14543
14544 rettv->vval.v_number = -1;
14545 if (argvars[0].v_type != VAR_LIST)
14546 {
14547 EMSG(_(e_listreq));
14548 return;
14549 }
14550 l = argvars[0].vval.v_list;
14551 if (l != NULL)
14552 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014553 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014554 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 int error = FALSE;
14557
Bram Moolenaar758711c2005-02-02 23:11:38 +000014558 /* Start at specified item. Use the cached index that list_find()
14559 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014561 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 ic = get_tv_number_chk(&argvars[3], &error);
14564 if (error)
14565 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014567
Bram Moolenaar758711c2005-02-02 23:11:38 +000014568 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014569 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014570 {
14571 rettv->vval.v_number = idx;
14572 break;
14573 }
14574 }
14575}
14576
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577static int inputsecret_flag = 0;
14578
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014579static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014580
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014582 * This function is used by f_input() and f_inputdialog() functions. The third
14583 * argument to f_input() specifies the type of completion to use at the
14584 * prompt. The third argument to f_inputdialog() specifies the value to return
14585 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 */
14587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014588get_user_input(
14589 typval_T *argvars,
14590 typval_T *rettv,
14591 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 char_u *p = NULL;
14595 int c;
14596 char_u buf[NUMBUFLEN];
14597 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014599 int xp_type = EXPAND_NOTHING;
14600 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014603 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604
14605#ifdef NO_CONSOLE_INPUT
14606 /* While starting up, there is no place to enter text. */
14607 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609#endif
14610
14611 cmd_silent = FALSE; /* Want to see the prompt. */
14612 if (prompt != NULL)
14613 {
14614 /* Only the part of the message after the last NL is considered as
14615 * prompt for the command line */
14616 p = vim_strrchr(prompt, '\n');
14617 if (p == NULL)
14618 p = prompt;
14619 else
14620 {
14621 ++p;
14622 c = *p;
14623 *p = NUL;
14624 msg_start();
14625 msg_clr_eos();
14626 msg_puts_attr(prompt, echo_attr);
14627 msg_didout = FALSE;
14628 msg_starthere();
14629 *p = c;
14630 }
14631 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 if (argvars[1].v_type != VAR_UNKNOWN)
14634 {
14635 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14636 if (defstr != NULL)
14637 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014639 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014640 {
14641 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014642 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014643 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014644
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014645 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014646 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014647
Bram Moolenaar4463f292005-09-25 22:20:24 +000014648 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14649 if (xp_name == NULL)
14650 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014651
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014652 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014653
Bram Moolenaar4463f292005-09-25 22:20:24 +000014654 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14655 &xp_arg) == FAIL)
14656 return;
14657 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014658 }
14659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014661 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014662 int save_ex_normal_busy = ex_normal_busy;
14663 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014665 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14666 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014667 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014668 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014669 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014670 && argvars[1].v_type != VAR_UNKNOWN
14671 && argvars[2].v_type != VAR_UNKNOWN)
14672 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14673 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014674
14675 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 /* since the user typed this, no need to wait for return */
14678 need_wait_return = FALSE;
14679 msg_didout = FALSE;
14680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 cmd_silent = cmd_silent_save;
14682}
14683
14684/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014685 * "input()" function
14686 * Also handles inputsecret() when inputsecret is set.
14687 */
14688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014689f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014690{
14691 get_user_input(argvars, rettv, FALSE);
14692}
14693
14694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 * "inputdialog()" function
14696 */
14697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014698f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699{
14700#if defined(FEAT_GUI_TEXTDIALOG)
14701 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14702 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14703 {
14704 char_u *message;
14705 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 message = get_tv_string_chk(&argvars[0]);
14709 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014710 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014711 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 else
14713 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 if (message != NULL && defstr != NULL
14715 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014716 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014717 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 else
14719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 if (message != NULL && defstr != NULL
14721 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014722 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723 rettv->vval.v_string = vim_strsave(
14724 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014728 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 }
14730 else
14731#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014732 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733}
14734
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014735/*
14736 * "inputlist()" function
14737 */
14738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014739f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014740{
14741 listitem_T *li;
14742 int selected;
14743 int mouse_used;
14744
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014745#ifdef NO_CONSOLE_INPUT
14746 /* While starting up, there is no place to enter text. */
14747 if (no_console_input())
14748 return;
14749#endif
14750 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14751 {
14752 EMSG2(_(e_listarg), "inputlist()");
14753 return;
14754 }
14755
14756 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014757 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014758 lines_left = Rows; /* avoid more prompt */
14759 msg_scroll = TRUE;
14760 msg_clr_eos();
14761
14762 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14763 {
14764 msg_puts(get_tv_string(&li->li_tv));
14765 msg_putchar('\n');
14766 }
14767
14768 /* Ask for choice. */
14769 selected = prompt_for_number(&mouse_used);
14770 if (mouse_used)
14771 selected -= lines_left;
14772
14773 rettv->vval.v_number = selected;
14774}
14775
14776
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14778
14779/*
14780 * "inputrestore()" function
14781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014783f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784{
14785 if (ga_userinput.ga_len > 0)
14786 {
14787 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14789 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014790 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 }
14792 else if (p_verbose > 1)
14793 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014794 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 }
14797}
14798
14799/*
14800 * "inputsave()" function
14801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014805 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 if (ga_grow(&ga_userinput, 1) == OK)
14807 {
14808 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14809 + ga_userinput.ga_len);
14810 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014811 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812 }
14813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014814 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815}
14816
14817/*
14818 * "inputsecret()" function
14819 */
14820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 ++cmdline_star;
14824 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 --cmdline_star;
14827 --inputsecret_flag;
14828}
14829
14830/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 * "insert()" function
14832 */
14833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014834f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014835{
14836 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014837 listitem_T *item;
14838 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840
14841 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014842 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014843 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014844 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845 {
14846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 before = get_tv_number_chk(&argvars[2], &error);
14848 if (error)
14849 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850
Bram Moolenaar758711c2005-02-02 23:11:38 +000014851 if (before == l->lv_len)
14852 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853 else
14854 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014855 item = list_find(l, before);
14856 if (item == NULL)
14857 {
14858 EMSGN(_(e_listidx), before);
14859 l = NULL;
14860 }
14861 }
14862 if (l != NULL)
14863 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014864 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014865 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014866 }
14867 }
14868}
14869
14870/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014871 * "invert(expr)" function
14872 */
14873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014874f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014875{
14876 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14877}
14878
14879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 * "isdirectory()" function
14881 */
14882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014883f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886}
14887
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014888/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014889 * "islocked()" function
14890 */
14891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014893{
14894 lval_T lv;
14895 char_u *end;
14896 dictitem_T *di;
14897
14898 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014899 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14900 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014901 if (end != NULL && lv.ll_name != NULL)
14902 {
14903 if (*end != NUL)
14904 EMSG(_(e_trailing));
14905 else
14906 {
14907 if (lv.ll_tv == NULL)
14908 {
14909 if (check_changedtick(lv.ll_name))
14910 rettv->vval.v_number = 1; /* always locked */
14911 else
14912 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014913 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014914 if (di != NULL)
14915 {
14916 /* Consider a variable locked when:
14917 * 1. the variable itself is locked
14918 * 2. the value of the variable is locked.
14919 * 3. the List or Dict value is locked.
14920 */
14921 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14922 || tv_islocked(&di->di_tv));
14923 }
14924 }
14925 }
14926 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014927 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014928 else if (lv.ll_newkey != NULL)
14929 EMSG2(_(e_dictkey), lv.ll_newkey);
14930 else if (lv.ll_list != NULL)
14931 /* List item. */
14932 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14933 else
14934 /* Dictionary item. */
14935 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14936 }
14937 }
14938
14939 clear_lval(&lv);
14940}
14941
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014942#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14943/*
14944 * "isnan()" function
14945 */
14946 static void
14947f_isnan(typval_T *argvars, typval_T *rettv)
14948{
14949 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14950 && isnan(argvars[0].vval.v_float);
14951}
14952#endif
14953
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014954static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955
14956/*
14957 * Turn a dict into a list:
14958 * "what" == 0: list of keys
14959 * "what" == 1: list of values
14960 * "what" == 2: list of items
14961 */
14962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014963dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014964{
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 list_T *l2;
14966 dictitem_T *di;
14967 hashitem_T *hi;
14968 listitem_T *li;
14969 listitem_T *li2;
14970 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014971 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014972
Bram Moolenaar8c711452005-01-14 21:53:12 +000014973 if (argvars[0].v_type != VAR_DICT)
14974 {
14975 EMSG(_(e_dictreq));
14976 return;
14977 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014978 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014979 return;
14980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014982 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014984 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014989 --todo;
14990 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014991
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014992 li = listitem_alloc();
14993 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014994 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014995 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014996
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014997 if (what == 0)
14998 {
14999 /* keys() */
15000 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015001 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015002 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15003 }
15004 else if (what == 1)
15005 {
15006 /* values() */
15007 copy_tv(&di->di_tv, &li->li_tv);
15008 }
15009 else
15010 {
15011 /* items() */
15012 l2 = list_alloc();
15013 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015014 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 li->li_tv.vval.v_list = l2;
15016 if (l2 == NULL)
15017 break;
15018 ++l2->lv_refcount;
15019
15020 li2 = listitem_alloc();
15021 if (li2 == NULL)
15022 break;
15023 list_append(l2, li2);
15024 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015025 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015026 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15027
15028 li2 = listitem_alloc();
15029 if (li2 == NULL)
15030 break;
15031 list_append(l2, li2);
15032 copy_tv(&di->di_tv, &li2->li_tv);
15033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034 }
15035 }
15036}
15037
15038/*
15039 * "items(dict)" function
15040 */
15041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015042f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015043{
15044 dict_list(argvars, rettv, 2);
15045}
15046
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015047#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015048/*
15049 * Get the job from the argument.
15050 * Returns NULL if the job is invalid.
15051 */
15052 static job_T *
15053get_job_arg(typval_T *tv)
15054{
15055 job_T *job;
15056
15057 if (tv->v_type != VAR_JOB)
15058 {
15059 EMSG2(_(e_invarg2), get_tv_string(tv));
15060 return NULL;
15061 }
15062 job = tv->vval.v_job;
15063
15064 if (job == NULL)
15065 EMSG(_("E916: not a valid job"));
15066 return job;
15067}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015068
15069# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015070/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015071 * "job_getchannel()" function
15072 */
15073 static void
15074f_job_getchannel(typval_T *argvars, typval_T *rettv)
15075{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015076 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015077
Bram Moolenaar65edff82016-02-21 16:40:11 +010015078 if (job != NULL)
15079 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015080 rettv->v_type = VAR_CHANNEL;
15081 rettv->vval.v_channel = job->jv_channel;
15082 if (job->jv_channel != NULL)
15083 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015084 }
15085}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015086# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015087
15088/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015089 * "job_setoptions()" function
15090 */
15091 static void
15092f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15093{
15094 job_T *job = get_job_arg(&argvars[0]);
15095 jobopt_T opt;
15096
15097 if (job == NULL)
15098 return;
15099 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015100 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015101 return;
15102 job_set_options(job, &opt);
15103}
15104
15105/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015106 * "job_start()" function
15107 */
15108 static void
15109f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15110{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015111 job_T *job;
15112 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015113#if defined(UNIX)
15114# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015115 char **argv = NULL;
15116 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015117#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015118 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015119#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015120 jobopt_T opt;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015121 int part;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015122
15123 rettv->v_type = VAR_JOB;
15124 job = job_alloc();
15125 rettv->vval.v_job = job;
15126 if (job == NULL)
15127 return;
15128
15129 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015130
15131 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015132 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015133 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015134 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015135 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015136 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015137 return;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015138
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015139 /* Check that when io is "file" that there is a file name. */
15140 for (part = PART_OUT; part <= PART_IN; ++part)
15141 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
15142 && opt.jo_io[part] == JIO_FILE
15143 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
15144 || *opt.jo_io_name[part] == NUL))
15145 {
15146 EMSG(_("E920: -io file requires -name to be set"));
15147 return;
15148 }
15149
Bram Moolenaar014069a2016-03-03 22:51:40 +010015150 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15151 {
15152 buf_T *buf;
15153
15154 /* check that we can find the buffer before starting the job */
15155 if (!(opt.jo_set & JO_IN_NAME))
15156 {
15157 EMSG(_("E915: in-io buffer requires in-name to be set"));
15158 return;
15159 }
15160 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
15161 if (buf == NULL)
15162 return;
15163 if (buf->b_ml.ml_mfp == NULL)
15164 {
15165 EMSG2(_("E918: buffer must be loaded: %s"),
15166 opt.jo_io_name[PART_IN]);
15167 return;
15168 }
15169 job->jv_in_buf = buf;
15170 }
15171
Bram Moolenaar65edff82016-02-21 16:40:11 +010015172 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015173
Bram Moolenaar835dc632016-02-07 14:27:38 +010015174#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015175 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015176#endif
15177
15178 if (argvars[0].v_type == VAR_STRING)
15179 {
15180 /* Command is a string. */
15181 cmd = argvars[0].vval.v_string;
15182#ifdef USE_ARGV
15183 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15184 return;
15185 argv[argc] = NULL;
15186#endif
15187 }
15188 else if (argvars[0].v_type != VAR_LIST
15189 || argvars[0].vval.v_list == NULL
15190 || argvars[0].vval.v_list->lv_len < 1)
15191 {
15192 EMSG(_(e_invarg));
15193 return;
15194 }
15195 else
15196 {
15197 list_T *l = argvars[0].vval.v_list;
15198 listitem_T *li;
15199 char_u *s;
15200
15201#ifdef USE_ARGV
15202 /* Pass argv[] to mch_call_shell(). */
15203 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15204 if (argv == NULL)
15205 return;
15206#endif
15207 for (li = l->lv_first; li != NULL; li = li->li_next)
15208 {
15209 s = get_tv_string_chk(&li->li_tv);
15210 if (s == NULL)
15211 goto theend;
15212#ifdef USE_ARGV
15213 argv[argc++] = (char *)s;
15214#else
Bram Moolenaara86f14a2016-02-29 21:05:48 +010015215 /* Only escape when needed, double quotes are not always allowed. */
15216 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015217 {
15218 s = vim_strsave_shellescape(s, FALSE, TRUE);
15219 if (s == NULL)
15220 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015221 ga_concat(&ga, s);
15222 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015223 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015224 else
15225 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015226 if (li->li_next != NULL)
15227 ga_append(&ga, ' ');
15228#endif
15229 }
15230#ifdef USE_ARGV
15231 argv[argc] = NULL;
15232#else
15233 cmd = ga.ga_data;
15234#endif
15235 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015236
Bram Moolenaar835dc632016-02-07 14:27:38 +010015237#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015238# ifdef FEAT_CHANNEL
15239 if (ch_log_active())
15240 {
15241 garray_T ga;
15242 int i;
15243
15244 ga_init2(&ga, (int)sizeof(char), 200);
15245 for (i = 0; i < argc; ++i)
15246 {
15247 if (i > 0)
15248 ga_concat(&ga, (char_u *)" ");
15249 ga_concat(&ga, (char_u *)argv[i]);
15250 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015251 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015252 ga_clear(&ga);
15253 }
15254# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015255 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015256#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015257# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015258 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015259# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015260 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015261#endif
15262
Bram Moolenaar014069a2016-03-03 22:51:40 +010015263#ifdef FEAT_CHANNEL
Bram Moolenaar839fd112016-03-06 21:34:03 +010015264 /* If the channel is reading from a buffer, write lines now. */
Bram Moolenaar4e329fc2016-03-07 15:24:03 +010015265 if (job->jv_channel != NULL)
15266 channel_write_in(job->jv_channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015267#endif
15268
Bram Moolenaar835dc632016-02-07 14:27:38 +010015269theend:
15270#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015271 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015272#else
15273 vim_free(ga.ga_data);
15274#endif
15275}
15276
15277/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015278 * Get the status of "job" and invoke the exit callback when needed.
15279 * The returned string is not allocated.
15280 */
15281 static char *
15282job_status(job_T *job)
15283{
15284 char *result;
15285
15286 if (job->jv_status == JOB_ENDED)
15287 /* No need to check, dead is dead. */
15288 result = "dead";
15289 else if (job->jv_status == JOB_FAILED)
15290 result = "fail";
15291 else
15292 {
15293 result = mch_job_status(job);
15294# ifdef FEAT_CHANNEL
15295 if (job->jv_status == JOB_ENDED)
15296 ch_log(job->jv_channel, "Job ended");
15297# endif
15298 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15299 {
15300 typval_T argv[3];
15301 typval_T rettv;
15302 int dummy;
15303
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015304 /* invoke the exit callback; make sure the refcount is > 0 */
15305 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015306 argv[0].v_type = VAR_JOB;
15307 argv[0].vval.v_job = job;
15308 argv[1].v_type = VAR_NUMBER;
15309 argv[1].vval.v_number = job->jv_exitval;
15310 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15311 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15312 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015313 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015314 }
15315 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15316 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015317 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015318 * freed. Careful: caller must not use "job" after this! */
15319 job_free(job);
15320 }
15321 }
15322 return result;
15323}
15324
15325/*
15326 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15327 */
15328 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015329job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015330{
15331 static time_t last_check = 0;
15332 time_t now;
15333 job_T *job;
15334 job_T *next;
15335
15336 /* Only do this once in 10 seconds. */
15337 now = time(NULL);
15338 if (last_check + 10 < now)
15339 {
15340 last_check = now;
15341 for (job = first_job; job != NULL; job = next)
15342 {
15343 next = job->jv_next;
15344 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15345 job_status(job); /* may free "job" */
15346 }
15347 }
15348}
15349
15350/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015351 * "job_status()" function
15352 */
15353 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015354f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015355{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015356 job_T *job = get_job_arg(&argvars[0]);
15357 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015358
Bram Moolenaar65edff82016-02-21 16:40:11 +010015359 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015360 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015361 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015362 rettv->v_type = VAR_STRING;
15363 rettv->vval.v_string = vim_strsave((char_u *)result);
15364 }
15365}
15366
15367/*
15368 * "job_stop()" function
15369 */
15370 static void
15371f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15372{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015373 job_T *job = get_job_arg(&argvars[0]);
15374
15375 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015376 {
15377 char_u *arg;
15378
15379 if (argvars[1].v_type == VAR_UNKNOWN)
15380 arg = (char_u *)"";
15381 else
15382 {
15383 arg = get_tv_string_chk(&argvars[1]);
15384 if (arg == NULL)
15385 {
15386 EMSG(_(e_invarg));
15387 return;
15388 }
15389 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015390# ifdef FEAT_CHANNEL
15391 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15392# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015393 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015394 rettv->vval.v_number = 0;
15395 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015396 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015397 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015398 /* Assume that "hup" does not kill the job. */
15399 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15400 job->jv_channel->ch_job_killed = TRUE;
15401 }
15402 /* We don't try freeing the job, obviously the caller still has a
15403 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015404 }
15405}
15406#endif
15407
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015409 * "join()" function
15410 */
15411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015412f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015413{
15414 garray_T ga;
15415 char_u *sep;
15416
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015417 if (argvars[0].v_type != VAR_LIST)
15418 {
15419 EMSG(_(e_listreq));
15420 return;
15421 }
15422 if (argvars[0].vval.v_list == NULL)
15423 return;
15424 if (argvars[1].v_type == VAR_UNKNOWN)
15425 sep = (char_u *)" ";
15426 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015427 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015428
15429 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015430
15431 if (sep != NULL)
15432 {
15433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015434 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015435 ga_append(&ga, NUL);
15436 rettv->vval.v_string = (char_u *)ga.ga_data;
15437 }
15438 else
15439 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015440}
15441
15442/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015443 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015444 */
15445 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015446f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015447{
15448 js_read_T reader;
15449
15450 reader.js_buf = get_tv_string(&argvars[0]);
15451 reader.js_fill = NULL;
15452 reader.js_used = 0;
15453 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15454 EMSG(_(e_invarg));
15455}
15456
15457/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015458 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015459 */
15460 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015461f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015462{
15463 rettv->v_type = VAR_STRING;
15464 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15465}
15466
15467/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015468 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015469 */
15470 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015471f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015472{
15473 js_read_T reader;
15474
15475 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015476 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015477 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015478 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015479 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015480}
15481
15482/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015483 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015484 */
15485 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015486f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015487{
15488 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015489 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015490}
15491
15492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015493 * "keys()" function
15494 */
15495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015496f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015497{
15498 dict_list(argvars, rettv, 0);
15499}
15500
15501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 * "last_buffer_nr()" function.
15503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015505f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506{
15507 int n = 0;
15508 buf_T *buf;
15509
15510 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15511 if (n < buf->b_fnum)
15512 n = buf->b_fnum;
15513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015515}
15516
15517/*
15518 * "len()" function
15519 */
15520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015521f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015522{
15523 switch (argvars[0].v_type)
15524 {
15525 case VAR_STRING:
15526 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015527 rettv->vval.v_number = (varnumber_T)STRLEN(
15528 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015529 break;
15530 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015532 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015533 case VAR_DICT:
15534 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15535 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015536 case VAR_UNKNOWN:
15537 case VAR_SPECIAL:
15538 case VAR_FLOAT:
15539 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015541 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015542 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015543 break;
15544 }
15545}
15546
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015547static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015548
15549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015550libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015551{
15552#ifdef FEAT_LIBCALL
15553 char_u *string_in;
15554 char_u **string_result;
15555 int nr_result;
15556#endif
15557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015559 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015561
15562 if (check_restricted() || check_secure())
15563 return;
15564
15565#ifdef FEAT_LIBCALL
15566 /* The first two args must be strings, otherwise its meaningless */
15567 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15568 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015569 string_in = NULL;
15570 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015571 string_in = argvars[2].vval.v_string;
15572 if (type == VAR_NUMBER)
15573 string_result = NULL;
15574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015575 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015576 if (mch_libcall(argvars[0].vval.v_string,
15577 argvars[1].vval.v_string,
15578 string_in,
15579 argvars[2].vval.v_number,
15580 string_result,
15581 &nr_result) == OK
15582 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015584 }
15585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586}
15587
15588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015589 * "libcall()" function
15590 */
15591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015592f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593{
15594 libcall_common(argvars, rettv, VAR_STRING);
15595}
15596
15597/*
15598 * "libcallnr()" function
15599 */
15600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015601f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602{
15603 libcall_common(argvars, rettv, VAR_NUMBER);
15604}
15605
15606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 * "line(string)" function
15608 */
15609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015610f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611{
15612 linenr_T lnum = 0;
15613 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015614 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015616 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 if (fp != NULL)
15618 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015619 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620}
15621
15622/*
15623 * "line2byte(lnum)" function
15624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627{
15628#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015629 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630#else
15631 linenr_T lnum;
15632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015637 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15638 if (rettv->vval.v_number >= 0)
15639 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640#endif
15641}
15642
15643/*
15644 * "lispindent(lnum)" function
15645 */
15646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015647f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648{
15649#ifdef FEAT_LISP
15650 pos_T pos;
15651 linenr_T lnum;
15652
15653 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15656 {
15657 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015658 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 curwin->w_cursor = pos;
15660 }
15661 else
15662#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664}
15665
15666/*
15667 * "localtime()" function
15668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015670f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673}
15674
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015675static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679{
15680 char_u *keys;
15681 char_u *which;
15682 char_u buf[NUMBUFLEN];
15683 char_u *keys_buf = NULL;
15684 char_u *rhs;
15685 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015686 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015687 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015688 mapblock_T *mp;
15689 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690
15691 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015692 rettv->v_type = VAR_STRING;
15693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015695 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 if (*keys == NUL)
15697 return;
15698
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015699 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015701 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015703 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015704 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015705 if (argvars[3].v_type != VAR_UNKNOWN)
15706 get_dict = get_tv_number(&argvars[3]);
15707 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 else
15710 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015711 if (which == NULL)
15712 return;
15713
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 mode = get_map_mode(&which, 0);
15715
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015716 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015717 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015719
15720 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015722 /* Return a string. */
15723 if (rhs != NULL)
15724 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
Bram Moolenaarbd743252010-10-20 21:23:33 +020015726 }
15727 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15728 {
15729 /* Return a dictionary. */
15730 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15731 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15732 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
Bram Moolenaarbd743252010-10-20 21:23:33 +020015734 dict_add_nr_str(dict, "lhs", 0L, lhs);
15735 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15736 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15737 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15738 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15739 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15740 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015741 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015742 dict_add_nr_str(dict, "mode", 0L, mapmode);
15743
15744 vim_free(lhs);
15745 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 }
15747}
15748
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015749#ifdef FEAT_FLOAT
15750/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015751 * "log()" function
15752 */
15753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015754f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015755{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015756 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015757
15758 rettv->v_type = VAR_FLOAT;
15759 if (get_float_arg(argvars, &f) == OK)
15760 rettv->vval.v_float = log(f);
15761 else
15762 rettv->vval.v_float = 0.0;
15763}
15764
15765/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015766 * "log10()" function
15767 */
15768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015769f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015770{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015771 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772
15773 rettv->v_type = VAR_FLOAT;
15774 if (get_float_arg(argvars, &f) == OK)
15775 rettv->vval.v_float = log10(f);
15776 else
15777 rettv->vval.v_float = 0.0;
15778}
15779#endif
15780
Bram Moolenaar1dced572012-04-05 16:54:08 +020015781#ifdef FEAT_LUA
15782/*
15783 * "luaeval()" function
15784 */
15785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015786f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015787{
15788 char_u *str;
15789 char_u buf[NUMBUFLEN];
15790
15791 str = get_tv_string_buf(&argvars[0], buf);
15792 do_luaeval(str, argvars + 1, rettv);
15793}
15794#endif
15795
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015797 * "map()" function
15798 */
15799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015800f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015801{
15802 filter_map(argvars, rettv, TRUE);
15803}
15804
15805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807 */
15808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015809f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015811 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812}
15813
15814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015815 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 */
15817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015818f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015820 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821}
15822
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015823static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
15825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015826find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015828 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015829 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015830 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 char_u *pat;
15832 regmatch_T regmatch;
15833 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 char_u *save_cpo;
15836 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015837 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015838 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015839 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015840 list_T *l = NULL;
15841 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015842 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015843 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844
15845 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15846 save_cpo = p_cpo;
15847 p_cpo = (char_u *)"";
15848
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849 rettv->vval.v_number = -1;
15850 if (type == 3)
15851 {
15852 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015853 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015854 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015855 }
15856 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015858 rettv->v_type = VAR_STRING;
15859 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015861
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015862 if (argvars[0].v_type == VAR_LIST)
15863 {
15864 if ((l = argvars[0].vval.v_list) == NULL)
15865 goto theend;
15866 li = l->lv_first;
15867 }
15868 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015869 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015870 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015871 len = (long)STRLEN(str);
15872 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015874 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15875 if (pat == NULL)
15876 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015880 int error = FALSE;
15881
15882 start = get_tv_number_chk(&argvars[2], &error);
15883 if (error)
15884 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015885 if (l != NULL)
15886 {
15887 li = list_find(l, start);
15888 if (li == NULL)
15889 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015890 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015891 }
15892 else
15893 {
15894 if (start < 0)
15895 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015896 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015897 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015898 /* When "count" argument is there ignore matches before "start",
15899 * otherwise skip part of the string. Differs when pattern is "^"
15900 * or "\<". */
15901 if (argvars[3].v_type != VAR_UNKNOWN)
15902 startcol = start;
15903 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015904 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015905 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015906 len -= start;
15907 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015908 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015909
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015910 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015911 nth = get_tv_number_chk(&argvars[3], &error);
15912 if (error)
15913 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 }
15915
15916 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15917 if (regmatch.regprog != NULL)
15918 {
15919 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015920
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015921 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015922 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015923 if (l != NULL)
15924 {
15925 if (li == NULL)
15926 {
15927 match = FALSE;
15928 break;
15929 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015930 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015931 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015932 if (str == NULL)
15933 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015934 }
15935
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015936 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015937
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015938 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015939 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015940 if (l == NULL && !match)
15941 break;
15942
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015943 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015944 if (l != NULL)
15945 {
15946 li = li->li_next;
15947 ++idx;
15948 }
15949 else
15950 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015951#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015952 startcol = (colnr_T)(regmatch.startp[0]
15953 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015954#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015955 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015956#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015957 if (startcol > (colnr_T)len
15958 || str + startcol <= regmatch.startp[0])
15959 {
15960 match = FALSE;
15961 break;
15962 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015963 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015964 }
15965
15966 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015968 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015969 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015970 int i;
15971
15972 /* return list with matched string and submatches */
15973 for (i = 0; i < NSUBEXP; ++i)
15974 {
15975 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015976 {
15977 if (list_append_string(rettv->vval.v_list,
15978 (char_u *)"", 0) == FAIL)
15979 break;
15980 }
15981 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015982 regmatch.startp[i],
15983 (int)(regmatch.endp[i] - regmatch.startp[i]))
15984 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015985 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015986 }
15987 }
15988 else if (type == 2)
15989 {
15990 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015991 if (l != NULL)
15992 copy_tv(&li->li_tv, rettv);
15993 else
15994 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015996 }
15997 else if (l != NULL)
15998 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 else
16000 {
16001 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 (varnumber_T)(regmatch.startp[0] - str);
16004 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016007 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 }
16009 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016010 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 }
16012
16013theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016014 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 p_cpo = save_cpo;
16016}
16017
16018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016019 * "match()" function
16020 */
16021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016022f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023{
16024 find_some_match(argvars, rettv, 1);
16025}
16026
16027/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016028 * "matchadd()" function
16029 */
16030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016031f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016032{
16033#ifdef FEAT_SEARCH_EXTRA
16034 char_u buf[NUMBUFLEN];
16035 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16036 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16037 int prio = 10; /* default priority */
16038 int id = -1;
16039 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016040 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016041
16042 rettv->vval.v_number = -1;
16043
16044 if (grp == NULL || pat == NULL)
16045 return;
16046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016047 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016048 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016049 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016050 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016051 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016052 if (argvars[4].v_type != VAR_UNKNOWN)
16053 {
16054 if (argvars[4].v_type != VAR_DICT)
16055 {
16056 EMSG(_(e_dictreq));
16057 return;
16058 }
16059 if (dict_find(argvars[4].vval.v_dict,
16060 (char_u *)"conceal", -1) != NULL)
16061 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16062 (char_u *)"conceal", FALSE);
16063 }
16064 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016065 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016066 if (error == TRUE)
16067 return;
16068 if (id >= 1 && id <= 3)
16069 {
16070 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16071 return;
16072 }
16073
Bram Moolenaar6561d522015-07-21 15:48:27 +020016074 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16075 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016076#endif
16077}
16078
16079/*
16080 * "matchaddpos()" function
16081 */
16082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016083f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016084{
16085#ifdef FEAT_SEARCH_EXTRA
16086 char_u buf[NUMBUFLEN];
16087 char_u *group;
16088 int prio = 10;
16089 int id = -1;
16090 int error = FALSE;
16091 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016092 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016093
16094 rettv->vval.v_number = -1;
16095
16096 group = get_tv_string_buf_chk(&argvars[0], buf);
16097 if (group == NULL)
16098 return;
16099
16100 if (argvars[1].v_type != VAR_LIST)
16101 {
16102 EMSG2(_(e_listarg), "matchaddpos()");
16103 return;
16104 }
16105 l = argvars[1].vval.v_list;
16106 if (l == NULL)
16107 return;
16108
16109 if (argvars[2].v_type != VAR_UNKNOWN)
16110 {
16111 prio = get_tv_number_chk(&argvars[2], &error);
16112 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016113 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016114 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016115 if (argvars[4].v_type != VAR_UNKNOWN)
16116 {
16117 if (argvars[4].v_type != VAR_DICT)
16118 {
16119 EMSG(_(e_dictreq));
16120 return;
16121 }
16122 if (dict_find(argvars[4].vval.v_dict,
16123 (char_u *)"conceal", -1) != NULL)
16124 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16125 (char_u *)"conceal", FALSE);
16126 }
16127 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016128 }
16129 if (error == TRUE)
16130 return;
16131
16132 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16133 if (id == 1 || id == 2)
16134 {
16135 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16136 return;
16137 }
16138
Bram Moolenaar6561d522015-07-21 15:48:27 +020016139 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16140 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016141#endif
16142}
16143
16144/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016145 * "matcharg()" function
16146 */
16147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016148f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016149{
16150 if (rettv_list_alloc(rettv) == OK)
16151 {
16152#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016153 int id = get_tv_number(&argvars[0]);
16154 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016155
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016156 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016157 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016158 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16159 {
16160 list_append_string(rettv->vval.v_list,
16161 syn_id2name(m->hlg_id), -1);
16162 list_append_string(rettv->vval.v_list, m->pattern, -1);
16163 }
16164 else
16165 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016166 list_append_string(rettv->vval.v_list, NULL, -1);
16167 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016168 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016169 }
16170#endif
16171 }
16172}
16173
16174/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016175 * "matchdelete()" function
16176 */
16177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016178f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016179{
16180#ifdef FEAT_SEARCH_EXTRA
16181 rettv->vval.v_number = match_delete(curwin,
16182 (int)get_tv_number(&argvars[0]), TRUE);
16183#endif
16184}
16185
16186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016187 * "matchend()" function
16188 */
16189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016190f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016191{
16192 find_some_match(argvars, rettv, 0);
16193}
16194
16195/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016196 * "matchlist()" function
16197 */
16198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016199f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016200{
16201 find_some_match(argvars, rettv, 3);
16202}
16203
16204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 * "matchstr()" function
16206 */
16207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016208f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016209{
16210 find_some_match(argvars, rettv, 2);
16211}
16212
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016213static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016214
16215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016216max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016217{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016218 long n = 0;
16219 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016220 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016221
16222 if (argvars[0].v_type == VAR_LIST)
16223 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016224 list_T *l;
16225 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016226
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016227 l = argvars[0].vval.v_list;
16228 if (l != NULL)
16229 {
16230 li = l->lv_first;
16231 if (li != NULL)
16232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016233 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016234 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016235 {
16236 li = li->li_next;
16237 if (li == NULL)
16238 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016239 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016240 if (domax ? i > n : i < n)
16241 n = i;
16242 }
16243 }
16244 }
16245 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016246 else if (argvars[0].v_type == VAR_DICT)
16247 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016248 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016249 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016251 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016252
16253 d = argvars[0].vval.v_dict;
16254 if (d != NULL)
16255 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016256 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016258 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016259 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016261 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016263 if (first)
16264 {
16265 n = i;
16266 first = FALSE;
16267 }
16268 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016269 n = i;
16270 }
16271 }
16272 }
16273 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016274 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016275 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016276 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016277}
16278
16279/*
16280 * "max()" function
16281 */
16282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016284{
16285 max_min(argvars, rettv, TRUE);
16286}
16287
16288/*
16289 * "min()" function
16290 */
16291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016292f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016293{
16294 max_min(argvars, rettv, FALSE);
16295}
16296
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016297static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016298
16299/*
16300 * Create the directory in which "dir" is located, and higher levels when
16301 * needed.
16302 */
16303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016304mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016305{
16306 char_u *p;
16307 char_u *updir;
16308 int r = FAIL;
16309
16310 /* Get end of directory name in "dir".
16311 * We're done when it's "/" or "c:/". */
16312 p = gettail_sep(dir);
16313 if (p <= get_past_head(dir))
16314 return OK;
16315
16316 /* If the directory exists we're done. Otherwise: create it.*/
16317 updir = vim_strnsave(dir, (int)(p - dir));
16318 if (updir == NULL)
16319 return FAIL;
16320 if (mch_isdir(updir))
16321 r = OK;
16322 else if (mkdir_recurse(updir, prot) == OK)
16323 r = vim_mkdir_emsg(updir, prot);
16324 vim_free(updir);
16325 return r;
16326}
16327
16328#ifdef vim_mkdir
16329/*
16330 * "mkdir()" function
16331 */
16332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016333f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016334{
16335 char_u *dir;
16336 char_u buf[NUMBUFLEN];
16337 int prot = 0755;
16338
16339 rettv->vval.v_number = FAIL;
16340 if (check_restricted() || check_secure())
16341 return;
16342
16343 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016344 if (*dir == NUL)
16345 rettv->vval.v_number = FAIL;
16346 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016347 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016348 if (*gettail(dir) == NUL)
16349 /* remove trailing slashes */
16350 *gettail_sep(dir) = NUL;
16351
16352 if (argvars[1].v_type != VAR_UNKNOWN)
16353 {
16354 if (argvars[2].v_type != VAR_UNKNOWN)
16355 prot = get_tv_number_chk(&argvars[2], NULL);
16356 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16357 mkdir_recurse(dir, prot);
16358 }
16359 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016360 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016361}
16362#endif
16363
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 * "mode()" function
16366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016370 char_u buf[3];
16371
16372 buf[1] = NUL;
16373 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 if (VIsual_active)
16376 {
16377 if (VIsual_select)
16378 buf[0] = VIsual_mode + 's' - 'v';
16379 else
16380 buf[0] = VIsual_mode;
16381 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016382 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016383 || State == CONFIRM)
16384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016386 if (State == ASKMORE)
16387 buf[1] = 'm';
16388 else if (State == CONFIRM)
16389 buf[1] = '?';
16390 }
16391 else if (State == EXTERNCMD)
16392 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 else if (State & INSERT)
16394 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016395#ifdef FEAT_VREPLACE
16396 if (State & VREPLACE_FLAG)
16397 {
16398 buf[0] = 'R';
16399 buf[1] = 'v';
16400 }
16401 else
16402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 if (State & REPLACE_FLAG)
16404 buf[0] = 'R';
16405 else
16406 buf[0] = 'i';
16407 }
16408 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016411 if (exmode_active)
16412 buf[1] = 'v';
16413 }
16414 else if (exmode_active)
16415 {
16416 buf[0] = 'c';
16417 buf[1] = 'e';
16418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016421 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016422 if (finish_op)
16423 buf[1] = 'o';
16424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016426 /* Clear out the minor mode when the argument is not a non-zero number or
16427 * non-empty string. */
16428 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016429 buf[1] = NUL;
16430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431 rettv->vval.v_string = vim_strsave(buf);
16432 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016433}
16434
Bram Moolenaar429fa852013-04-15 12:27:36 +020016435#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016436/*
16437 * "mzeval()" function
16438 */
16439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016440f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016441{
16442 char_u *str;
16443 char_u buf[NUMBUFLEN];
16444
16445 str = get_tv_string_buf(&argvars[0], buf);
16446 do_mzeval(str, rettv);
16447}
Bram Moolenaar75676462013-01-30 14:55:42 +010016448
16449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016450mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016451{
16452 typval_T argvars[3];
16453
16454 argvars[0].v_type = VAR_STRING;
16455 argvars[0].vval.v_string = name;
16456 copy_tv(args, &argvars[1]);
16457 argvars[2].v_type = VAR_UNKNOWN;
16458 f_call(argvars, rettv);
16459 clear_tv(&argvars[1]);
16460}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016461#endif
16462
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464 * "nextnonblank()" function
16465 */
16466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016467f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468{
16469 linenr_T lnum;
16470
16471 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016473 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474 {
16475 lnum = 0;
16476 break;
16477 }
16478 if (*skipwhite(ml_get(lnum)) != NUL)
16479 break;
16480 }
16481 rettv->vval.v_number = lnum;
16482}
16483
16484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016485 * "nr2char()" function
16486 */
16487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016488f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016489{
16490 char_u buf[NUMBUFLEN];
16491
16492#ifdef FEAT_MBYTE
16493 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016494 {
16495 int utf8 = 0;
16496
16497 if (argvars[1].v_type != VAR_UNKNOWN)
16498 utf8 = get_tv_number_chk(&argvars[1], NULL);
16499 if (utf8)
16500 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16501 else
16502 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016504 else
16505#endif
16506 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016507 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016508 buf[1] = NUL;
16509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 rettv->v_type = VAR_STRING;
16511 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016512}
16513
16514/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016515 * "or(expr, expr)" function
16516 */
16517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016518f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016519{
16520 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16521 | get_tv_number_chk(&argvars[1], NULL);
16522}
16523
16524/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016525 * "pathshorten()" function
16526 */
16527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016528f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016529{
16530 char_u *p;
16531
16532 rettv->v_type = VAR_STRING;
16533 p = get_tv_string_chk(&argvars[0]);
16534 if (p == NULL)
16535 rettv->vval.v_string = NULL;
16536 else
16537 {
16538 p = vim_strsave(p);
16539 rettv->vval.v_string = p;
16540 if (p != NULL)
16541 shorten_dir(p);
16542 }
16543}
16544
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016545#ifdef FEAT_PERL
16546/*
16547 * "perleval()" function
16548 */
16549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016550f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016551{
16552 char_u *str;
16553 char_u buf[NUMBUFLEN];
16554
16555 str = get_tv_string_buf(&argvars[0], buf);
16556 do_perleval(str, rettv);
16557}
16558#endif
16559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016560#ifdef FEAT_FLOAT
16561/*
16562 * "pow()" function
16563 */
16564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016565f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016566{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016567 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016568
16569 rettv->v_type = VAR_FLOAT;
16570 if (get_float_arg(argvars, &fx) == OK
16571 && get_float_arg(&argvars[1], &fy) == OK)
16572 rettv->vval.v_float = pow(fx, fy);
16573 else
16574 rettv->vval.v_float = 0.0;
16575}
16576#endif
16577
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579 * "prevnonblank()" function
16580 */
16581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016582f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583{
16584 linenr_T lnum;
16585
16586 lnum = get_tv_lnum(argvars);
16587 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16588 lnum = 0;
16589 else
16590 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16591 --lnum;
16592 rettv->vval.v_number = lnum;
16593}
16594
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016595/* This dummy va_list is here because:
16596 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16597 * - locally in the function results in a "used before set" warning
16598 * - using va_start() to initialize it gives "function with fixed args" error */
16599static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016600
Bram Moolenaar8c711452005-01-14 21:53:12 +000016601/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016602 * "printf()" function
16603 */
16604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016605f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016606{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016607 char_u buf[NUMBUFLEN];
16608 int len;
16609 char_u *s;
16610 int saved_did_emsg = did_emsg;
16611 char *fmt;
16612
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016613 rettv->v_type = VAR_STRING;
16614 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016615
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016616 /* Get the required length, allocate the buffer and do it for real. */
16617 did_emsg = FALSE;
16618 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16619 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16620 if (!did_emsg)
16621 {
16622 s = alloc(len + 1);
16623 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016624 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016625 rettv->vval.v_string = s;
16626 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016627 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016628 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016629 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016630}
16631
16632/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016633 * "pumvisible()" function
16634 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016636f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016637{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016638#ifdef FEAT_INS_EXPAND
16639 if (pum_visible())
16640 rettv->vval.v_number = 1;
16641#endif
16642}
16643
Bram Moolenaardb913952012-06-29 12:54:53 +020016644#ifdef FEAT_PYTHON3
16645/*
16646 * "py3eval()" function
16647 */
16648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016649f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016650{
16651 char_u *str;
16652 char_u buf[NUMBUFLEN];
16653
16654 str = get_tv_string_buf(&argvars[0], buf);
16655 do_py3eval(str, rettv);
16656}
16657#endif
16658
16659#ifdef FEAT_PYTHON
16660/*
16661 * "pyeval()" function
16662 */
16663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016664f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016665{
16666 char_u *str;
16667 char_u buf[NUMBUFLEN];
16668
16669 str = get_tv_string_buf(&argvars[0], buf);
16670 do_pyeval(str, rettv);
16671}
16672#endif
16673
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016674/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016675 * "range()" function
16676 */
16677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016678f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016679{
16680 long start;
16681 long end;
16682 long stride = 1;
16683 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016686 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016687 if (argvars[1].v_type == VAR_UNKNOWN)
16688 {
16689 end = start - 1;
16690 start = 0;
16691 }
16692 else
16693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016697 }
16698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016699 if (error)
16700 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016701 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016702 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016703 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016704 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016705 else
16706 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016707 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016708 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016709 if (list_append_number(rettv->vval.v_list,
16710 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016711 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016712 }
16713}
16714
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016715/*
16716 * "readfile()" function
16717 */
16718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016719f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016720{
16721 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016722 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016723 char_u *fname;
16724 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016725 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16726 int io_size = sizeof(buf);
16727 int readlen; /* size of last fread() */
16728 char_u *prev = NULL; /* previously read bytes, if any */
16729 long prevlen = 0; /* length of data in prev */
16730 long prevsize = 0; /* size of prev buffer */
16731 long maxline = MAXLNUM;
16732 long cnt = 0;
16733 char_u *p; /* position in buf */
16734 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016736 if (argvars[1].v_type != VAR_UNKNOWN)
16737 {
16738 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16739 binary = TRUE;
16740 if (argvars[2].v_type != VAR_UNKNOWN)
16741 maxline = get_tv_number(&argvars[2]);
16742 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016743
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016744 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016746
16747 /* Always open the file in binary mode, library functions have a mind of
16748 * their own about CR-LF conversion. */
16749 fname = get_tv_string(&argvars[0]);
16750 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16751 {
16752 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16753 return;
16754 }
16755
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016756 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016758 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016759
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016760 /* This for loop processes what was read, but is also entered at end
16761 * of file so that either:
16762 * - an incomplete line gets written
16763 * - a "binary" file gets an empty line at the end if it ends in a
16764 * newline. */
16765 for (p = buf, start = buf;
16766 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16767 ++p)
16768 {
16769 if (*p == '\n' || readlen <= 0)
16770 {
16771 listitem_T *li;
16772 char_u *s = NULL;
16773 long_u len = p - start;
16774
16775 /* Finished a line. Remove CRs before NL. */
16776 if (readlen > 0 && !binary)
16777 {
16778 while (len > 0 && start[len - 1] == '\r')
16779 --len;
16780 /* removal may cross back to the "prev" string */
16781 if (len == 0)
16782 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16783 --prevlen;
16784 }
16785 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016786 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016787 else
16788 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016789 /* Change "prev" buffer to be the right size. This way
16790 * the bytes are only copied once, and very long lines are
16791 * allocated only once. */
16792 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016793 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016794 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016795 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016796 prev = NULL; /* the list will own the string */
16797 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016798 }
16799 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016800 if (s == NULL)
16801 {
16802 do_outofmem_msg((long_u) prevlen + len + 1);
16803 failed = TRUE;
16804 break;
16805 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016806
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016807 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016808 {
16809 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016810 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016811 break;
16812 }
16813 li->li_tv.v_type = VAR_STRING;
16814 li->li_tv.v_lock = 0;
16815 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016816 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016817
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016818 start = p + 1; /* step over newline */
16819 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016820 break;
16821 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016822 else if (*p == NUL)
16823 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016824#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016825 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16826 * when finding the BF and check the previous two bytes. */
16827 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016828 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016829 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16830 * + 1, these may be in the "prev" string. */
16831 char_u back1 = p >= buf + 1 ? p[-1]
16832 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16833 char_u back2 = p >= buf + 2 ? p[-2]
16834 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16835 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016836
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016837 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016838 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016839 char_u *dest = p - 2;
16840
16841 /* Usually a BOM is at the beginning of a file, and so at
16842 * the beginning of a line; then we can just step over it.
16843 */
16844 if (start == dest)
16845 start = p + 1;
16846 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016847 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016848 /* have to shuffle buf to close gap */
16849 int adjust_prevlen = 0;
16850
16851 if (dest < buf)
16852 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016853 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016854 dest = buf;
16855 }
16856 if (readlen > p - buf + 1)
16857 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16858 readlen -= 3 - adjust_prevlen;
16859 prevlen -= adjust_prevlen;
16860 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016861 }
16862 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016863 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016864#endif
16865 } /* for */
16866
16867 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16868 break;
16869 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016870 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016871 /* There's part of a line in buf, store it in "prev". */
16872 if (p - start + prevlen >= prevsize)
16873 {
16874 /* need bigger "prev" buffer */
16875 char_u *newprev;
16876
16877 /* A common use case is ordinary text files and "prev" gets a
16878 * fragment of a line, so the first allocation is made
16879 * small, to avoid repeatedly 'allocing' large and
16880 * 'reallocing' small. */
16881 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016882 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 else
16884 {
16885 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016886 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016887 prevsize = grow50pc > growmin ? grow50pc : growmin;
16888 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016889 newprev = prev == NULL ? alloc(prevsize)
16890 : vim_realloc(prev, prevsize);
16891 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016892 {
16893 do_outofmem_msg((long_u)prevsize);
16894 failed = TRUE;
16895 break;
16896 }
16897 prev = newprev;
16898 }
16899 /* Add the line part to end of "prev". */
16900 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016901 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016902 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016903 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016904
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016905 /*
16906 * For a negative line count use only the lines at the end of the file,
16907 * free the rest.
16908 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016909 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016910 while (cnt > -maxline)
16911 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016912 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016913 --cnt;
16914 }
16915
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016916 if (failed)
16917 {
16918 list_free(rettv->vval.v_list, TRUE);
16919 /* readfile doc says an empty list is returned on error */
16920 rettv->vval.v_list = list_alloc();
16921 }
16922
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016923 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016924 fclose(fd);
16925}
16926
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016927#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016928static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016929
16930/*
16931 * Convert a List to proftime_T.
16932 * Return FAIL when there is something wrong.
16933 */
16934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016936{
16937 long n1, n2;
16938 int error = FALSE;
16939
16940 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16941 || arg->vval.v_list->lv_len != 2)
16942 return FAIL;
16943 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16944 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16945# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016946 tm->HighPart = n1;
16947 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016948# else
16949 tm->tv_sec = n1;
16950 tm->tv_usec = n2;
16951# endif
16952 return error ? FAIL : OK;
16953}
16954#endif /* FEAT_RELTIME */
16955
16956/*
16957 * "reltime()" function
16958 */
16959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016961{
16962#ifdef FEAT_RELTIME
16963 proftime_T res;
16964 proftime_T start;
16965
16966 if (argvars[0].v_type == VAR_UNKNOWN)
16967 {
16968 /* No arguments: get current time. */
16969 profile_start(&res);
16970 }
16971 else if (argvars[1].v_type == VAR_UNKNOWN)
16972 {
16973 if (list2proftime(&argvars[0], &res) == FAIL)
16974 return;
16975 profile_end(&res);
16976 }
16977 else
16978 {
16979 /* Two arguments: compute the difference. */
16980 if (list2proftime(&argvars[0], &start) == FAIL
16981 || list2proftime(&argvars[1], &res) == FAIL)
16982 return;
16983 profile_sub(&res, &start);
16984 }
16985
16986 if (rettv_list_alloc(rettv) == OK)
16987 {
16988 long n1, n2;
16989
16990# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016991 n1 = res.HighPart;
16992 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016993# else
16994 n1 = res.tv_sec;
16995 n2 = res.tv_usec;
16996# endif
16997 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16998 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16999 }
17000#endif
17001}
17002
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017003#ifdef FEAT_FLOAT
17004/*
17005 * "reltimefloat()" function
17006 */
17007 static void
17008f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17009{
17010# ifdef FEAT_RELTIME
17011 proftime_T tm;
17012# endif
17013
17014 rettv->v_type = VAR_FLOAT;
17015 rettv->vval.v_float = 0;
17016# ifdef FEAT_RELTIME
17017 if (list2proftime(&argvars[0], &tm) == OK)
17018 rettv->vval.v_float = profile_float(&tm);
17019# endif
17020}
17021#endif
17022
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017023/*
17024 * "reltimestr()" function
17025 */
17026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017027f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017028{
17029#ifdef FEAT_RELTIME
17030 proftime_T tm;
17031#endif
17032
17033 rettv->v_type = VAR_STRING;
17034 rettv->vval.v_string = NULL;
17035#ifdef FEAT_RELTIME
17036 if (list2proftime(&argvars[0], &tm) == OK)
17037 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17038#endif
17039}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017040
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017042static void make_connection(void);
17043static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044
17045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017046make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047{
17048 if (X_DISPLAY == NULL
17049# ifdef FEAT_GUI
17050 && !gui.in_use
17051# endif
17052 )
17053 {
17054 x_force_connect = TRUE;
17055 setup_term_clip();
17056 x_force_connect = FALSE;
17057 }
17058}
17059
17060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017061check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062{
17063 make_connection();
17064 if (X_DISPLAY == NULL)
17065 {
17066 EMSG(_("E240: No connection to Vim server"));
17067 return FAIL;
17068 }
17069 return OK;
17070}
17071#endif
17072
17073#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017074static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075
17076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017077remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078{
17079 char_u *server_name;
17080 char_u *keys;
17081 char_u *r = NULL;
17082 char_u buf[NUMBUFLEN];
17083# ifdef WIN32
17084 HWND w;
17085# else
17086 Window w;
17087# endif
17088
17089 if (check_restricted() || check_secure())
17090 return;
17091
17092# ifdef FEAT_X11
17093 if (check_connection() == FAIL)
17094 return;
17095# endif
17096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017097 server_name = get_tv_string_chk(&argvars[0]);
17098 if (server_name == NULL)
17099 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 keys = get_tv_string_buf(&argvars[1], buf);
17101# ifdef WIN32
17102 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17103# else
17104 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17105 < 0)
17106# endif
17107 {
17108 if (r != NULL)
17109 EMSG(r); /* sending worked but evaluation failed */
17110 else
17111 EMSG2(_("E241: Unable to send to %s"), server_name);
17112 return;
17113 }
17114
17115 rettv->vval.v_string = r;
17116
17117 if (argvars[2].v_type != VAR_UNKNOWN)
17118 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017119 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017120 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017121 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017123 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 v.di_tv.v_type = VAR_STRING;
17125 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 idvar = get_tv_string_chk(&argvars[2]);
17127 if (idvar != NULL)
17128 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017129 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 }
17131}
17132#endif
17133
17134/*
17135 * "remote_expr()" function
17136 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017138f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139{
17140 rettv->v_type = VAR_STRING;
17141 rettv->vval.v_string = NULL;
17142#ifdef FEAT_CLIENTSERVER
17143 remote_common(argvars, rettv, TRUE);
17144#endif
17145}
17146
17147/*
17148 * "remote_foreground()" function
17149 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017151f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153#ifdef FEAT_CLIENTSERVER
17154# ifdef WIN32
17155 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 {
17157 char_u *server_name = get_tv_string_chk(&argvars[0]);
17158
17159 if (server_name != NULL)
17160 serverForeground(server_name);
17161 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162# else
17163 /* Send a foreground() expression to the server. */
17164 argvars[1].v_type = VAR_STRING;
17165 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17166 argvars[2].v_type = VAR_UNKNOWN;
17167 remote_common(argvars, rettv, TRUE);
17168 vim_free(argvars[1].vval.v_string);
17169# endif
17170#endif
17171}
17172
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017174f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175{
17176#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 char_u *s = NULL;
17179# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017180 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017183
17184 if (check_restricted() || check_secure())
17185 {
17186 rettv->vval.v_number = -1;
17187 return;
17188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017189 serverid = get_tv_string_chk(&argvars[0]);
17190 if (serverid == NULL)
17191 {
17192 rettv->vval.v_number = -1;
17193 return; /* type error; errmsg already given */
17194 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017195# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017196 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 if (n == 0)
17198 rettv->vval.v_number = -1;
17199 else
17200 {
17201 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17202 rettv->vval.v_number = (s != NULL);
17203 }
17204# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205 if (check_connection() == FAIL)
17206 return;
17207
17208 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017209 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210# endif
17211
17212 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 char_u *retvar;
17215
Bram Moolenaar33570922005-01-25 22:26:29 +000017216 v.di_tv.v_type = VAR_STRING;
17217 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017218 retvar = get_tv_string_chk(&argvars[1]);
17219 if (retvar != NULL)
17220 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017221 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017222 }
17223#else
17224 rettv->vval.v_number = -1;
17225#endif
17226}
17227
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017229f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230{
17231 char_u *r = NULL;
17232
17233#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 char_u *serverid = get_tv_string_chk(&argvars[0]);
17235
17236 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237 {
17238# ifdef WIN32
17239 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017240 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017242 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243 if (n != 0)
17244 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17245 if (r == NULL)
17246# else
17247 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017248 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249# endif
17250 EMSG(_("E277: Unable to read a server reply"));
17251 }
17252#endif
17253 rettv->v_type = VAR_STRING;
17254 rettv->vval.v_string = r;
17255}
17256
17257/*
17258 * "remote_send()" function
17259 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017261f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262{
17263 rettv->v_type = VAR_STRING;
17264 rettv->vval.v_string = NULL;
17265#ifdef FEAT_CLIENTSERVER
17266 remote_common(argvars, rettv, FALSE);
17267#endif
17268}
17269
17270/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017271 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017272 */
17273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017274f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275{
Bram Moolenaar33570922005-01-25 22:26:29 +000017276 list_T *l;
17277 listitem_T *item, *item2;
17278 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017280 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017281 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017282 dict_T *d;
17283 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017284 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017285
Bram Moolenaar8c711452005-01-14 21:53:12 +000017286 if (argvars[0].v_type == VAR_DICT)
17287 {
17288 if (argvars[2].v_type != VAR_UNKNOWN)
17289 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017290 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017291 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 key = get_tv_string_chk(&argvars[1]);
17294 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 di = dict_find(d, key, -1);
17297 if (di == NULL)
17298 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017299 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17300 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017301 {
17302 *rettv = di->di_tv;
17303 init_tv(&di->di_tv);
17304 dictitem_remove(d, di);
17305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017306 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017307 }
17308 }
17309 else if (argvars[0].v_type != VAR_LIST)
17310 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017311 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017312 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017314 int error = FALSE;
17315
17316 idx = get_tv_number_chk(&argvars[1], &error);
17317 if (error)
17318 ; /* type error: do nothing, errmsg already given */
17319 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320 EMSGN(_(e_listidx), idx);
17321 else
17322 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 if (argvars[2].v_type == VAR_UNKNOWN)
17324 {
17325 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017326 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017327 *rettv = item->li_tv;
17328 vim_free(item);
17329 }
17330 else
17331 {
17332 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017333 end = get_tv_number_chk(&argvars[2], &error);
17334 if (error)
17335 ; /* type error: do nothing */
17336 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017337 EMSGN(_(e_listidx), end);
17338 else
17339 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017340 int cnt = 0;
17341
17342 for (li = item; li != NULL; li = li->li_next)
17343 {
17344 ++cnt;
17345 if (li == item2)
17346 break;
17347 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017348 if (li == NULL) /* didn't find "item2" after "item" */
17349 EMSG(_(e_invrange));
17350 else
17351 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017352 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017353 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017354 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017355 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017356 l->lv_first = item;
17357 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017358 item->li_prev = NULL;
17359 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017360 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017361 }
17362 }
17363 }
17364 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017365 }
17366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367}
17368
17369/*
17370 * "rename({from}, {to})" function
17371 */
17372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017373f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374{
17375 char_u buf[NUMBUFLEN];
17376
17377 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017380 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17381 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382}
17383
17384/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017385 * "repeat()" function
17386 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017388f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017389{
17390 char_u *p;
17391 int n;
17392 int slen;
17393 int len;
17394 char_u *r;
17395 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017396
17397 n = get_tv_number(&argvars[1]);
17398 if (argvars[0].v_type == VAR_LIST)
17399 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017400 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017401 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017402 if (list_extend(rettv->vval.v_list,
17403 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017404 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017405 }
17406 else
17407 {
17408 p = get_tv_string(&argvars[0]);
17409 rettv->v_type = VAR_STRING;
17410 rettv->vval.v_string = NULL;
17411
17412 slen = (int)STRLEN(p);
17413 len = slen * n;
17414 if (len <= 0)
17415 return;
17416
17417 r = alloc(len + 1);
17418 if (r != NULL)
17419 {
17420 for (i = 0; i < n; i++)
17421 mch_memmove(r + i * slen, p, (size_t)slen);
17422 r[len] = NUL;
17423 }
17424
17425 rettv->vval.v_string = r;
17426 }
17427}
17428
17429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 * "resolve()" function
17431 */
17432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017433f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434{
17435 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017436#ifdef HAVE_READLINK
17437 char_u *buf = NULL;
17438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017440 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441#ifdef FEAT_SHORTCUT
17442 {
17443 char_u *v = NULL;
17444
17445 v = mch_resolve_shortcut(p);
17446 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017449 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 }
17451#else
17452# ifdef HAVE_READLINK
17453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 char_u *cpy;
17455 int len;
17456 char_u *remain = NULL;
17457 char_u *q;
17458 int is_relative_to_current = FALSE;
17459 int has_trailing_pathsep = FALSE;
17460 int limit = 100;
17461
17462 p = vim_strsave(p);
17463
17464 if (p[0] == '.' && (vim_ispathsep(p[1])
17465 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17466 is_relative_to_current = TRUE;
17467
17468 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017469 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017472 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
17475 q = getnextcomp(p);
17476 if (*q != NUL)
17477 {
17478 /* Separate the first path component in "p", and keep the
17479 * remainder (beginning with the path separator). */
17480 remain = vim_strsave(q - 1);
17481 q[-1] = NUL;
17482 }
17483
Bram Moolenaard9462e32011-04-11 21:35:11 +020017484 buf = alloc(MAXPATHL + 1);
17485 if (buf == NULL)
17486 goto fail;
17487
Bram Moolenaar071d4272004-06-13 20:20:40 +000017488 for (;;)
17489 {
17490 for (;;)
17491 {
17492 len = readlink((char *)p, (char *)buf, MAXPATHL);
17493 if (len <= 0)
17494 break;
17495 buf[len] = NUL;
17496
17497 if (limit-- == 0)
17498 {
17499 vim_free(p);
17500 vim_free(remain);
17501 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 goto fail;
17504 }
17505
17506 /* Ensure that the result will have a trailing path separator
17507 * if the argument has one. */
17508 if (remain == NULL && has_trailing_pathsep)
17509 add_pathsep(buf);
17510
17511 /* Separate the first path component in the link value and
17512 * concatenate the remainders. */
17513 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17514 if (*q != NUL)
17515 {
17516 if (remain == NULL)
17517 remain = vim_strsave(q - 1);
17518 else
17519 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017520 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 if (cpy != NULL)
17522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 vim_free(remain);
17524 remain = cpy;
17525 }
17526 }
17527 q[-1] = NUL;
17528 }
17529
17530 q = gettail(p);
17531 if (q > p && *q == NUL)
17532 {
17533 /* Ignore trailing path separator. */
17534 q[-1] = NUL;
17535 q = gettail(p);
17536 }
17537 if (q > p && !mch_isFullName(buf))
17538 {
17539 /* symlink is relative to directory of argument */
17540 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17541 if (cpy != NULL)
17542 {
17543 STRCPY(cpy, p);
17544 STRCPY(gettail(cpy), buf);
17545 vim_free(p);
17546 p = cpy;
17547 }
17548 }
17549 else
17550 {
17551 vim_free(p);
17552 p = vim_strsave(buf);
17553 }
17554 }
17555
17556 if (remain == NULL)
17557 break;
17558
17559 /* Append the first path component of "remain" to "p". */
17560 q = getnextcomp(remain + 1);
17561 len = q - remain - (*q != NUL);
17562 cpy = vim_strnsave(p, STRLEN(p) + len);
17563 if (cpy != NULL)
17564 {
17565 STRNCAT(cpy, remain, len);
17566 vim_free(p);
17567 p = cpy;
17568 }
17569 /* Shorten "remain". */
17570 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017571 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 else
17573 {
17574 vim_free(remain);
17575 remain = NULL;
17576 }
17577 }
17578
17579 /* If the result is a relative path name, make it explicitly relative to
17580 * the current directory if and only if the argument had this form. */
17581 if (!vim_ispathsep(*p))
17582 {
17583 if (is_relative_to_current
17584 && *p != NUL
17585 && !(p[0] == '.'
17586 && (p[1] == NUL
17587 || vim_ispathsep(p[1])
17588 || (p[1] == '.'
17589 && (p[2] == NUL
17590 || vim_ispathsep(p[2]))))))
17591 {
17592 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017593 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 if (cpy != NULL)
17595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 vim_free(p);
17597 p = cpy;
17598 }
17599 }
17600 else if (!is_relative_to_current)
17601 {
17602 /* Strip leading "./". */
17603 q = p;
17604 while (q[0] == '.' && vim_ispathsep(q[1]))
17605 q += 2;
17606 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017607 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 }
17609 }
17610
17611 /* Ensure that the result will have no trailing path separator
17612 * if the argument had none. But keep "/" or "//". */
17613 if (!has_trailing_pathsep)
17614 {
17615 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017616 if (after_pathsep(p, q))
17617 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 }
17619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 }
17622# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624# endif
17625#endif
17626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628
17629#ifdef HAVE_READLINK
17630fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017631 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017633 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634}
17635
17636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638 */
17639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017640f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641{
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 list_T *l;
17643 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644
Bram Moolenaar0d660222005-01-07 21:51:51 +000017645 if (argvars[0].v_type != VAR_LIST)
17646 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017647 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017648 && !tv_check_lock(l->lv_lock,
17649 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017650 {
17651 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017652 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017653 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017654 while (li != NULL)
17655 {
17656 ni = li->li_prev;
17657 list_append(l, li);
17658 li = ni;
17659 }
17660 rettv->vval.v_list = l;
17661 rettv->v_type = VAR_LIST;
17662 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017663 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665}
17666
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017667#define SP_NOMOVE 0x01 /* don't move cursor */
17668#define SP_REPEAT 0x02 /* repeat to find outer pair */
17669#define SP_RETCOUNT 0x04 /* return matchcount */
17670#define SP_SETPCMARK 0x08 /* set previous context mark */
17671#define SP_START 0x10 /* accept match at start position */
17672#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17673#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017674#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017675
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017676static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677
17678/*
17679 * Get flags for a search function.
17680 * Possibly sets "p_ws".
17681 * Returns BACKWARD, FORWARD or zero (for an error).
17682 */
17683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017684get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685{
17686 int dir = FORWARD;
17687 char_u *flags;
17688 char_u nbuf[NUMBUFLEN];
17689 int mask;
17690
17691 if (varp->v_type != VAR_UNKNOWN)
17692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017693 flags = get_tv_string_buf_chk(varp, nbuf);
17694 if (flags == NULL)
17695 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017696 while (*flags != NUL)
17697 {
17698 switch (*flags)
17699 {
17700 case 'b': dir = BACKWARD; break;
17701 case 'w': p_ws = TRUE; break;
17702 case 'W': p_ws = FALSE; break;
17703 default: mask = 0;
17704 if (flagsp != NULL)
17705 switch (*flags)
17706 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017707 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017708 case 'e': mask = SP_END; break;
17709 case 'm': mask = SP_RETCOUNT; break;
17710 case 'n': mask = SP_NOMOVE; break;
17711 case 'p': mask = SP_SUBPAT; break;
17712 case 'r': mask = SP_REPEAT; break;
17713 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017714 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017715 }
17716 if (mask == 0)
17717 {
17718 EMSG2(_(e_invarg2), flags);
17719 dir = 0;
17720 }
17721 else
17722 *flagsp |= mask;
17723 }
17724 if (dir == 0)
17725 break;
17726 ++flags;
17727 }
17728 }
17729 return dir;
17730}
17731
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017733 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017735 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017736search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017738 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 char_u *pat;
17740 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017741 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 int save_p_ws = p_ws;
17743 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017744 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017745 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017746 proftime_T tm;
17747#ifdef FEAT_RELTIME
17748 long time_limit = 0;
17749#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017750 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017751 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017754 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017755 if (dir == 0)
17756 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017757 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017758 if (flags & SP_START)
17759 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017760 if (flags & SP_END)
17761 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017762 if (flags & SP_COLUMN)
17763 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017764
Bram Moolenaar76929292008-01-06 19:07:36 +000017765 /* Optional arguments: line number to stop searching and timeout. */
17766 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017767 {
17768 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17769 if (lnum_stop < 0)
17770 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017771#ifdef FEAT_RELTIME
17772 if (argvars[3].v_type != VAR_UNKNOWN)
17773 {
17774 time_limit = get_tv_number_chk(&argvars[3], NULL);
17775 if (time_limit < 0)
17776 goto theend;
17777 }
17778#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017779 }
17780
Bram Moolenaar76929292008-01-06 19:07:36 +000017781#ifdef FEAT_RELTIME
17782 /* Set the time limit, if there is one. */
17783 profile_setlimit(time_limit, &tm);
17784#endif
17785
Bram Moolenaar231334e2005-07-25 20:46:57 +000017786 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017787 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017788 * Check to make sure only those flags are set.
17789 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17790 * flags cannot be set. Check for that condition also.
17791 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017792 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017793 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017795 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017796 goto theend;
17797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017799 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017800 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017801 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017802 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017804 if (flags & SP_SUBPAT)
17805 retval = subpatnum;
17806 else
17807 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017808 if (flags & SP_SETPCMARK)
17809 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017811 if (match_pos != NULL)
17812 {
17813 /* Store the match cursor position */
17814 match_pos->lnum = pos.lnum;
17815 match_pos->col = pos.col + 1;
17816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 /* "/$" will put the cursor after the end of the line, may need to
17818 * correct that here */
17819 check_cursor();
17820 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017821
17822 /* If 'n' flag is used: restore cursor position. */
17823 if (flags & SP_NOMOVE)
17824 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017825 else
17826 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017827theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017829
17830 return retval;
17831}
17832
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017833#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017834
17835/*
17836 * round() is not in C90, use ceil() or floor() instead.
17837 */
17838 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017839vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017840{
17841 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17842}
17843
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017844/*
17845 * "round({float})" function
17846 */
17847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017848f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017849{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017850 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017851
17852 rettv->v_type = VAR_FLOAT;
17853 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017854 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017855 else
17856 rettv->vval.v_float = 0.0;
17857}
17858#endif
17859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017860/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017861 * "screenattr()" function
17862 */
17863 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017864f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017865{
17866 int row;
17867 int col;
17868 int c;
17869
17870 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17871 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17872 if (row < 0 || row >= screen_Rows
17873 || col < 0 || col >= screen_Columns)
17874 c = -1;
17875 else
17876 c = ScreenAttrs[LineOffset[row] + col];
17877 rettv->vval.v_number = c;
17878}
17879
17880/*
17881 * "screenchar()" function
17882 */
17883 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017884f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017885{
17886 int row;
17887 int col;
17888 int off;
17889 int c;
17890
17891 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17892 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17893 if (row < 0 || row >= screen_Rows
17894 || col < 0 || col >= screen_Columns)
17895 c = -1;
17896 else
17897 {
17898 off = LineOffset[row] + col;
17899#ifdef FEAT_MBYTE
17900 if (enc_utf8 && ScreenLinesUC[off] != 0)
17901 c = ScreenLinesUC[off];
17902 else
17903#endif
17904 c = ScreenLines[off];
17905 }
17906 rettv->vval.v_number = c;
17907}
17908
17909/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017910 * "screencol()" function
17911 *
17912 * First column is 1 to be consistent with virtcol().
17913 */
17914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017915f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017916{
17917 rettv->vval.v_number = screen_screencol() + 1;
17918}
17919
17920/*
17921 * "screenrow()" function
17922 */
17923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017924f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017925{
17926 rettv->vval.v_number = screen_screenrow() + 1;
17927}
17928
17929/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017930 * "search()" function
17931 */
17932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017933f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017934{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017935 int flags = 0;
17936
17937 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938}
17939
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017941 * "searchdecl()" function
17942 */
17943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017944f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017945{
17946 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017947 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017948 int error = FALSE;
17949 char_u *name;
17950
17951 rettv->vval.v_number = 1; /* default: FAIL */
17952
17953 name = get_tv_string_chk(&argvars[0]);
17954 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017955 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017956 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017957 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17958 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17959 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017960 if (!error && name != NULL)
17961 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017962 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017963}
17964
17965/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017966 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017969searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970{
17971 char_u *spat, *mpat, *epat;
17972 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 int dir;
17975 int flags = 0;
17976 char_u nbuf1[NUMBUFLEN];
17977 char_u nbuf2[NUMBUFLEN];
17978 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017979 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017980 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017981 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017984 spat = get_tv_string_chk(&argvars[0]);
17985 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17986 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17987 if (spat == NULL || mpat == NULL || epat == NULL)
17988 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 /* Handle the optional fourth argument: flags */
17991 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017992 if (dir == 0)
17993 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017994
17995 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017996 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17997 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017998 if ((flags & (SP_END | SP_SUBPAT)) != 0
17999 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018000 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018001 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018002 goto theend;
18003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018005 /* Using 'r' implies 'W', otherwise it doesn't work. */
18006 if (flags & SP_REPEAT)
18007 p_ws = FALSE;
18008
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018010 if (argvars[3].v_type == VAR_UNKNOWN
18011 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 skip = (char_u *)"";
18013 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018015 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018016 if (argvars[5].v_type != VAR_UNKNOWN)
18017 {
18018 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
18019 if (lnum_stop < 0)
18020 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018021#ifdef FEAT_RELTIME
18022 if (argvars[6].v_type != VAR_UNKNOWN)
18023 {
18024 time_limit = get_tv_number_chk(&argvars[6], NULL);
18025 if (time_limit < 0)
18026 goto theend;
18027 }
18028#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018029 }
18030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 if (skip == NULL)
18032 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018034 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018035 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018036
18037theend:
18038 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018039
18040 return retval;
18041}
18042
18043/*
18044 * "searchpair()" function
18045 */
18046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018047f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018048{
18049 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18050}
18051
18052/*
18053 * "searchpairpos()" function
18054 */
18055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018056f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018057{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018058 pos_T match_pos;
18059 int lnum = 0;
18060 int col = 0;
18061
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018062 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018063 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018064
18065 if (searchpair_cmn(argvars, &match_pos) > 0)
18066 {
18067 lnum = match_pos.lnum;
18068 col = match_pos.col;
18069 }
18070
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018071 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18072 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018073}
18074
18075/*
18076 * Search for a start/middle/end thing.
18077 * Used by searchpair(), see its documentation for the details.
18078 * Returns 0 or -1 for no match,
18079 */
18080 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081do_searchpair(
18082 char_u *spat, /* start pattern */
18083 char_u *mpat, /* middle pattern */
18084 char_u *epat, /* end pattern */
18085 int dir, /* BACKWARD or FORWARD */
18086 char_u *skip, /* skip expression */
18087 int flags, /* SP_SETPCMARK and other SP_ values */
18088 pos_T *match_pos,
18089 linenr_T lnum_stop, /* stop at this line if not zero */
18090 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018091{
18092 char_u *save_cpo;
18093 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18094 long retval = 0;
18095 pos_T pos;
18096 pos_T firstpos;
18097 pos_T foundpos;
18098 pos_T save_cursor;
18099 pos_T save_pos;
18100 int n;
18101 int r;
18102 int nest = 1;
18103 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018104 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018105 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018106
18107 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18108 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018109 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018110
Bram Moolenaar76929292008-01-06 19:07:36 +000018111#ifdef FEAT_RELTIME
18112 /* Set the time limit, if there is one. */
18113 profile_setlimit(time_limit, &tm);
18114#endif
18115
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018116 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18117 * start/middle/end (pat3, for the top pair). */
18118 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18119 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18120 if (pat2 == NULL || pat3 == NULL)
18121 goto theend;
18122 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18123 if (*mpat == NUL)
18124 STRCPY(pat3, pat2);
18125 else
18126 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18127 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018128 if (flags & SP_START)
18129 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018130
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131 save_cursor = curwin->w_cursor;
18132 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018133 clearpos(&firstpos);
18134 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 pat = pat3;
18136 for (;;)
18137 {
18138 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018139 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18141 /* didn't find it or found the first match again: FAIL */
18142 break;
18143
18144 if (firstpos.lnum == 0)
18145 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018146 if (equalpos(pos, foundpos))
18147 {
18148 /* Found the same position again. Can happen with a pattern that
18149 * has "\zs" at the end and searching backwards. Advance one
18150 * character and try again. */
18151 if (dir == BACKWARD)
18152 decl(&pos);
18153 else
18154 incl(&pos);
18155 }
18156 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018158 /* clear the start flag to avoid getting stuck here */
18159 options &= ~SEARCH_START;
18160
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 /* If the skip pattern matches, ignore this match. */
18162 if (*skip != NUL)
18163 {
18164 save_pos = curwin->w_cursor;
18165 curwin->w_cursor = pos;
18166 r = eval_to_bool(skip, &err, NULL, FALSE);
18167 curwin->w_cursor = save_pos;
18168 if (err)
18169 {
18170 /* Evaluating {skip} caused an error, break here. */
18171 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018172 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 break;
18174 }
18175 if (r)
18176 continue;
18177 }
18178
18179 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18180 {
18181 /* Found end when searching backwards or start when searching
18182 * forward: nested pair. */
18183 ++nest;
18184 pat = pat2; /* nested, don't search for middle */
18185 }
18186 else
18187 {
18188 /* Found end when searching forward or start when searching
18189 * backward: end of (nested) pair; or found middle in outer pair. */
18190 if (--nest == 1)
18191 pat = pat3; /* outer level, search for middle */
18192 }
18193
18194 if (nest == 0)
18195 {
18196 /* Found the match: return matchcount or line number. */
18197 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018198 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018200 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018201 if (flags & SP_SETPCMARK)
18202 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 curwin->w_cursor = pos;
18204 if (!(flags & SP_REPEAT))
18205 break;
18206 nest = 1; /* search for next unmatched */
18207 }
18208 }
18209
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018210 if (match_pos != NULL)
18211 {
18212 /* Store the match cursor position */
18213 match_pos->lnum = curwin->w_cursor.lnum;
18214 match_pos->col = curwin->w_cursor.col + 1;
18215 }
18216
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018218 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 curwin->w_cursor = save_cursor;
18220
18221theend:
18222 vim_free(pat2);
18223 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018224 if (p_cpo == empty_option)
18225 p_cpo = save_cpo;
18226 else
18227 /* Darn, evaluating the {skip} expression changed the value. */
18228 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018229
18230 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231}
18232
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018233/*
18234 * "searchpos()" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018238{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018239 pos_T match_pos;
18240 int lnum = 0;
18241 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018242 int n;
18243 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018245 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018246 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018247
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018248 n = search_cmn(argvars, &match_pos, &flags);
18249 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018250 {
18251 lnum = match_pos.lnum;
18252 col = match_pos.col;
18253 }
18254
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018255 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18256 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018257 if (flags & SP_SUBPAT)
18258 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018259}
18260
Bram Moolenaar0d660222005-01-07 21:51:51 +000018261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018262f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018264#ifdef FEAT_CLIENTSERVER
18265 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018266 char_u *server = get_tv_string_chk(&argvars[0]);
18267 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268
Bram Moolenaar0d660222005-01-07 21:51:51 +000018269 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 if (server == NULL || reply == NULL)
18271 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018272 if (check_restricted() || check_secure())
18273 return;
18274# ifdef FEAT_X11
18275 if (check_connection() == FAIL)
18276 return;
18277# endif
18278
18279 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 EMSG(_("E258: Unable to send to client"));
18282 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018284 rettv->vval.v_number = 0;
18285#else
18286 rettv->vval.v_number = -1;
18287#endif
18288}
18289
Bram Moolenaar0d660222005-01-07 21:51:51 +000018290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018291f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292{
18293 char_u *r = NULL;
18294
18295#ifdef FEAT_CLIENTSERVER
18296# ifdef WIN32
18297 r = serverGetVimNames();
18298# else
18299 make_connection();
18300 if (X_DISPLAY != NULL)
18301 r = serverGetVimNames(X_DISPLAY);
18302# endif
18303#endif
18304 rettv->v_type = VAR_STRING;
18305 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306}
18307
18308/*
18309 * "setbufvar()" function
18310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018312f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313{
18314 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018317 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 char_u nbuf[NUMBUFLEN];
18319
18320 if (check_restricted() || check_secure())
18321 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18323 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018324 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 varp = &argvars[2];
18326
18327 if (buf != NULL && varname != NULL && varp != NULL)
18328 {
18329 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331
18332 if (*varname == '&')
18333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018334 long numval;
18335 char_u *strval;
18336 int error = FALSE;
18337
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018339 numval = get_tv_number_chk(varp, &error);
18340 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018341 if (!error && strval != NULL)
18342 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343 }
18344 else
18345 {
18346 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18347 if (bufvarname != NULL)
18348 {
18349 STRCPY(bufvarname, "b:");
18350 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018351 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 vim_free(bufvarname);
18353 }
18354 }
18355
18356 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359}
18360
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018362f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018363{
18364 dict_T *d;
18365 dictitem_T *di;
18366 char_u *csearch;
18367
18368 if (argvars[0].v_type != VAR_DICT)
18369 {
18370 EMSG(_(e_dictreq));
18371 return;
18372 }
18373
18374 if ((d = argvars[0].vval.v_dict) != NULL)
18375 {
18376 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18377 if (csearch != NULL)
18378 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018379#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018380 if (enc_utf8)
18381 {
18382 int pcc[MAX_MCO];
18383 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018384
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018385 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18386 }
18387 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018388#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018389 set_last_csearch(PTR2CHAR(csearch),
18390 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018391 }
18392
18393 di = dict_find(d, (char_u *)"forward", -1);
18394 if (di != NULL)
18395 set_csearch_direction(get_tv_number(&di->di_tv)
18396 ? FORWARD : BACKWARD);
18397
18398 di = dict_find(d, (char_u *)"until", -1);
18399 if (di != NULL)
18400 set_csearch_until(!!get_tv_number(&di->di_tv));
18401 }
18402}
18403
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404/*
18405 * "setcmdpos()" function
18406 */
18407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018408f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018410 int pos = (int)get_tv_number(&argvars[0]) - 1;
18411
18412 if (pos >= 0)
18413 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414}
18415
18416/*
18417 * "setline()" function
18418 */
18419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018420f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421{
18422 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018423 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018424 list_T *l = NULL;
18425 listitem_T *li = NULL;
18426 long added = 0;
18427 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018429 lnum = get_tv_lnum(&argvars[0]);
18430 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018432 l = argvars[1].vval.v_list;
18433 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018435 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018436 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018437
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018438 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018439 for (;;)
18440 {
18441 if (l != NULL)
18442 {
18443 /* list argument, get next string */
18444 if (li == NULL)
18445 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018446 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018447 li = li->li_next;
18448 }
18449
18450 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018451 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018452 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018453
18454 /* When coming here from Insert mode, sync undo, so that this can be
18455 * undone separately from what was previously inserted. */
18456 if (u_sync_once == 2)
18457 {
18458 u_sync_once = 1; /* notify that u_sync() was called */
18459 u_sync(TRUE);
18460 }
18461
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018462 if (lnum <= curbuf->b_ml.ml_line_count)
18463 {
18464 /* existing line, replace it */
18465 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18466 {
18467 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018468 if (lnum == curwin->w_cursor.lnum)
18469 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018470 rettv->vval.v_number = 0; /* OK */
18471 }
18472 }
18473 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18474 {
18475 /* lnum is one past the last line, append the line */
18476 ++added;
18477 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18478 rettv->vval.v_number = 0; /* OK */
18479 }
18480
18481 if (l == NULL) /* only one string argument */
18482 break;
18483 ++lnum;
18484 }
18485
18486 if (added > 0)
18487 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488}
18489
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018490static 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 +000018491
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018493 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018494 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018496set_qf_ll_list(
18497 win_T *wp UNUSED,
18498 typval_T *list_arg UNUSED,
18499 typval_T *action_arg UNUSED,
18500 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018501{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018502#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018503 char_u *act;
18504 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018505#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018506
Bram Moolenaar2641f772005-03-25 21:58:17 +000018507 rettv->vval.v_number = -1;
18508
18509#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018510 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018511 EMSG(_(e_listreq));
18512 else
18513 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018514 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018515
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018516 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018517 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018518 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018519 if (act == NULL)
18520 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018521 if (*act == 'a' || *act == 'r')
18522 action = *act;
18523 }
18524
Bram Moolenaar81484f42012-12-05 15:16:47 +010018525 if (l != NULL && set_errorlist(wp, l, action,
18526 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018527 rettv->vval.v_number = 0;
18528 }
18529#endif
18530}
18531
18532/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018533 * "setloclist()" function
18534 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018536f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018537{
18538 win_T *win;
18539
18540 rettv->vval.v_number = -1;
18541
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018542 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018543 if (win != NULL)
18544 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18545}
18546
18547/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018548 * "setmatches()" function
18549 */
18550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018551f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018552{
18553#ifdef FEAT_SEARCH_EXTRA
18554 list_T *l;
18555 listitem_T *li;
18556 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018557 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018558
18559 rettv->vval.v_number = -1;
18560 if (argvars[0].v_type != VAR_LIST)
18561 {
18562 EMSG(_(e_listreq));
18563 return;
18564 }
18565 if ((l = argvars[0].vval.v_list) != NULL)
18566 {
18567
18568 /* To some extent make sure that we are dealing with a list from
18569 * "getmatches()". */
18570 li = l->lv_first;
18571 while (li != NULL)
18572 {
18573 if (li->li_tv.v_type != VAR_DICT
18574 || (d = li->li_tv.vval.v_dict) == NULL)
18575 {
18576 EMSG(_(e_invarg));
18577 return;
18578 }
18579 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018580 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18581 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018582 && dict_find(d, (char_u *)"priority", -1) != NULL
18583 && dict_find(d, (char_u *)"id", -1) != NULL))
18584 {
18585 EMSG(_(e_invarg));
18586 return;
18587 }
18588 li = li->li_next;
18589 }
18590
18591 clear_matches(curwin);
18592 li = l->lv_first;
18593 while (li != NULL)
18594 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018595 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018596 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018597 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018598 char_u *group;
18599 int priority;
18600 int id;
18601 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018602
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018603 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018604 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18605 {
18606 if (s == NULL)
18607 {
18608 s = list_alloc();
18609 if (s == NULL)
18610 return;
18611 }
18612
18613 /* match from matchaddpos() */
18614 for (i = 1; i < 9; i++)
18615 {
18616 sprintf((char *)buf, (char *)"pos%d", i);
18617 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18618 {
18619 if (di->di_tv.v_type != VAR_LIST)
18620 return;
18621
18622 list_append_tv(s, &di->di_tv);
18623 s->lv_refcount++;
18624 }
18625 else
18626 break;
18627 }
18628 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018629
18630 group = get_dict_string(d, (char_u *)"group", FALSE);
18631 priority = (int)get_dict_number(d, (char_u *)"priority");
18632 id = (int)get_dict_number(d, (char_u *)"id");
18633 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18634 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18635 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018636 if (i == 0)
18637 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018638 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018639 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018640 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018641 }
18642 else
18643 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018644 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018645 list_unref(s);
18646 s = NULL;
18647 }
18648
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018649 li = li->li_next;
18650 }
18651 rettv->vval.v_number = 0;
18652 }
18653#endif
18654}
18655
18656/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018657 * "setpos()" function
18658 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018660f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018661{
18662 pos_T pos;
18663 int fnum;
18664 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018665 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018666
Bram Moolenaar08250432008-02-13 11:42:46 +000018667 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018668 name = get_tv_string_chk(argvars);
18669 if (name != NULL)
18670 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018671 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018672 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018673 if (--pos.col < 0)
18674 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018675 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018676 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018677 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018678 if (fnum == curbuf->b_fnum)
18679 {
18680 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018681 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018682 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018683 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018684 curwin->w_set_curswant = FALSE;
18685 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018686 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018687 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018688 }
18689 else
18690 EMSG(_(e_invarg));
18691 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018692 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18693 {
18694 /* set mark */
18695 if (setmark_pos(name[1], &pos, fnum) == OK)
18696 rettv->vval.v_number = 0;
18697 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018698 else
18699 EMSG(_(e_invarg));
18700 }
18701 }
18702}
18703
18704/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018705 * "setqflist()" function
18706 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018708f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018709{
18710 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18711}
18712
18713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 * "setreg()" function
18715 */
18716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018717f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718{
18719 int regname;
18720 char_u *strregname;
18721 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018722 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 int append;
18724 char_u yank_type;
18725 long block_len;
18726
18727 block_len = -1;
18728 yank_type = MAUTO;
18729 append = FALSE;
18730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018731 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018732 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018734 if (strregname == NULL)
18735 return; /* type error; errmsg already given */
18736 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 if (regname == 0 || regname == '@')
18738 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018740 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018742 stropt = get_tv_string_chk(&argvars[2]);
18743 if (stropt == NULL)
18744 return; /* type error */
18745 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 switch (*stropt)
18747 {
18748 case 'a': case 'A': /* append */
18749 append = TRUE;
18750 break;
18751 case 'v': case 'c': /* character-wise selection */
18752 yank_type = MCHAR;
18753 break;
18754 case 'V': case 'l': /* line-wise selection */
18755 yank_type = MLINE;
18756 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 case 'b': case Ctrl_V: /* block-wise selection */
18758 yank_type = MBLOCK;
18759 if (VIM_ISDIGIT(stropt[1]))
18760 {
18761 ++stropt;
18762 block_len = getdigits(&stropt) - 1;
18763 --stropt;
18764 }
18765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 }
18767 }
18768
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018769 if (argvars[1].v_type == VAR_LIST)
18770 {
18771 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018772 char_u **allocval;
18773 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018774 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018775 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018776 int len = argvars[1].vval.v_list->lv_len;
18777 listitem_T *li;
18778
Bram Moolenaar7d647822014-04-05 21:28:56 +020018779 /* First half: use for pointers to result lines; second half: use for
18780 * pointers to allocated copies. */
18781 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018782 if (lstval == NULL)
18783 return;
18784 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018785 allocval = lstval + len + 2;
18786 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018787
18788 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18789 li = li->li_next)
18790 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018791 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018792 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018793 goto free_lstval;
18794 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018795 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018796 /* Need to make a copy, next get_tv_string_buf_chk() will
18797 * overwrite the string. */
18798 strval = vim_strsave(buf);
18799 if (strval == NULL)
18800 goto free_lstval;
18801 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018802 }
18803 *curval++ = strval;
18804 }
18805 *curval++ = NULL;
18806
18807 write_reg_contents_lst(regname, lstval, -1,
18808 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018809free_lstval:
18810 while (curallocval > allocval)
18811 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018812 vim_free(lstval);
18813 }
18814 else
18815 {
18816 strval = get_tv_string_chk(&argvars[1]);
18817 if (strval == NULL)
18818 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018819 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823}
18824
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018825/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018826 * "settabvar()" function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018830{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018831#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018832 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018833 tabpage_T *tp;
18834#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018835 char_u *varname, *tabvarname;
18836 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018837
18838 rettv->vval.v_number = 0;
18839
18840 if (check_restricted() || check_secure())
18841 return;
18842
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018843#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018844 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018845#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018846 varname = get_tv_string_chk(&argvars[1]);
18847 varp = &argvars[2];
18848
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018849 if (varname != NULL && varp != NULL
18850#ifdef FEAT_WINDOWS
18851 && tp != NULL
18852#endif
18853 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018854 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018855#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018856 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018857 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018858#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018859
18860 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18861 if (tabvarname != NULL)
18862 {
18863 STRCPY(tabvarname, "t:");
18864 STRCPY(tabvarname + 2, varname);
18865 set_var(tabvarname, varp, TRUE);
18866 vim_free(tabvarname);
18867 }
18868
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018869#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018870 /* Restore current tabpage */
18871 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018872 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018873#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018874 }
18875}
18876
18877/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018878 * "settabwinvar()" function
18879 */
18880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018881f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018882{
18883 setwinvar(argvars, rettv, 1);
18884}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885
18886/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018887 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018890f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018892 setwinvar(argvars, rettv, 0);
18893}
18894
18895/*
18896 * "setwinvar()" and "settabwinvar()" functions
18897 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018898
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018900setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018901{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 win_T *win;
18903#ifdef FEAT_WINDOWS
18904 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018905 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018906 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907#endif
18908 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018909 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018911 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912
18913 if (check_restricted() || check_secure())
18914 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018915
18916#ifdef FEAT_WINDOWS
18917 if (off == 1)
18918 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18919 else
18920 tp = curtab;
18921#endif
18922 win = find_win_by_nr(&argvars[off], tp);
18923 varname = get_tv_string_chk(&argvars[off + 1]);
18924 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925
18926 if (win != NULL && varname != NULL && varp != NULL)
18927 {
18928#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018929 need_switch_win = !(tp == curtab && win == curwin);
18930 if (!need_switch_win
18931 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018934 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018936 long numval;
18937 char_u *strval;
18938 int error = FALSE;
18939
18940 ++varname;
18941 numval = get_tv_number_chk(varp, &error);
18942 strval = get_tv_string_buf_chk(varp, nbuf);
18943 if (!error && strval != NULL)
18944 set_option_value(varname, numval, strval, OPT_LOCAL);
18945 }
18946 else
18947 {
18948 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18949 if (winvarname != NULL)
18950 {
18951 STRCPY(winvarname, "w:");
18952 STRCPY(winvarname + 2, varname);
18953 set_var(winvarname, varp, TRUE);
18954 vim_free(winvarname);
18955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956 }
18957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018959 if (need_switch_win)
18960 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961#endif
18962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963}
18964
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018965#ifdef FEAT_CRYPT
18966/*
18967 * "sha256({string})" function
18968 */
18969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018970f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018971{
18972 char_u *p;
18973
18974 p = get_tv_string(&argvars[0]);
18975 rettv->vval.v_string = vim_strsave(
18976 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18977 rettv->v_type = VAR_STRING;
18978}
18979#endif /* FEAT_CRYPT */
18980
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018982 * "shellescape({string})" function
18983 */
18984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018985f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018986{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018987 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018988 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018989 rettv->v_type = VAR_STRING;
18990}
18991
18992/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018993 * shiftwidth() function
18994 */
18995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018996f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018997{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018998 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018999}
19000
19001/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 */
19004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019005f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008
Bram Moolenaar0d660222005-01-07 21:51:51 +000019009 p = get_tv_string(&argvars[0]);
19010 rettv->vval.v_string = vim_strsave(p);
19011 simplify_filename(rettv->vval.v_string); /* simplify in place */
19012 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013}
19014
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019015#ifdef FEAT_FLOAT
19016/*
19017 * "sin()" function
19018 */
19019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019020f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019021{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019022 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019023
19024 rettv->v_type = VAR_FLOAT;
19025 if (get_float_arg(argvars, &f) == OK)
19026 rettv->vval.v_float = sin(f);
19027 else
19028 rettv->vval.v_float = 0.0;
19029}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019030
19031/*
19032 * "sinh()" function
19033 */
19034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019035f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019036{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019037 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019038
19039 rettv->v_type = VAR_FLOAT;
19040 if (get_float_arg(argvars, &f) == OK)
19041 rettv->vval.v_float = sinh(f);
19042 else
19043 rettv->vval.v_float = 0.0;
19044}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019045#endif
19046
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047static int
19048#ifdef __BORLANDC__
19049 _RTLENTRYF
19050#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019051 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052static int
19053#ifdef __BORLANDC__
19054 _RTLENTRYF
19055#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019056 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019058/* struct used in the array that's given to qsort() */
19059typedef struct
19060{
19061 listitem_T *item;
19062 int idx;
19063} sortItem_T;
19064
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065/* struct storing information about current sort */
19066typedef struct
19067{
19068 int item_compare_ic;
19069 int item_compare_numeric;
19070 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019071#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019073#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019074 char_u *item_compare_func;
19075 dict_T *item_compare_selfdict;
19076 int item_compare_func_err;
19077 int item_compare_keep_zero;
19078} sortinfo_T;
19079static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019080static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019081#define ITEM_COMPARE_FAIL 999
19082
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019084 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 static int
19087#ifdef __BORLANDC__
19088_RTLENTRYF
19089#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019090item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019092 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019093 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019094 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019095 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096 int res;
19097 char_u numbuf1[NUMBUFLEN];
19098 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019100 si1 = (sortItem_T *)s1;
19101 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019102 tv1 = &si1->item->li_tv;
19103 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019104
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019106 {
19107 long v1 = get_tv_number(tv1);
19108 long v2 = get_tv_number(tv2);
19109
19110 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19111 }
19112
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019113#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019115 {
19116 float_T v1 = get_tv_float(tv1);
19117 float_T v2 = get_tv_float(tv2);
19118
19119 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19120 }
19121#endif
19122
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019123 /* tv2string() puts quotes around a string and allocates memory. Don't do
19124 * that for string variables. Use a single quote when comparing with a
19125 * non-string to do what the docs promise. */
19126 if (tv1->v_type == VAR_STRING)
19127 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019128 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019129 p1 = (char_u *)"'";
19130 else
19131 p1 = tv1->vval.v_string;
19132 }
19133 else
19134 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19135 if (tv2->v_type == VAR_STRING)
19136 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019138 p2 = (char_u *)"'";
19139 else
19140 p2 = tv2->vval.v_string;
19141 }
19142 else
19143 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019144 if (p1 == NULL)
19145 p1 = (char_u *)"";
19146 if (p2 == NULL)
19147 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019149 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019151 res = STRICMP(p1, p2);
19152 else
19153 res = STRCMP(p1, p2);
19154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019156 {
19157 double n1, n2;
19158 n1 = strtod((char *)p1, (char **)&p1);
19159 n2 = strtod((char *)p2, (char **)&p2);
19160 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19161 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019162
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019163 /* When the result would be zero, compare the item indexes. Makes the
19164 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019166 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019167
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 vim_free(tofree1);
19169 vim_free(tofree2);
19170 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019174#ifdef __BORLANDC__
19175_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019177item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019179 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019182 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019183 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019185 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019187 return 0;
19188
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019189 si1 = (sortItem_T *)s1;
19190 si2 = (sortItem_T *)s2;
19191
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019192 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019193 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019194 copy_tv(&si1->item->li_tv, &argv[0]);
19195 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196
19197 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019199 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019200 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202 clear_tv(&argv[0]);
19203 clear_tv(&argv[1]);
19204
19205 if (res == FAIL)
19206 res = ITEM_COMPARE_FAIL;
19207 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19209 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019210 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019212
19213 /* When the result would be zero, compare the pointers themselves. Makes
19214 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019216 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019217
Bram Moolenaar0d660222005-01-07 21:51:51 +000019218 return res;
19219}
19220
19221/*
19222 * "sort({list})" function
19223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019225do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226{
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 list_T *l;
19228 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019229 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019230 sortinfo_T *old_sortinfo;
19231 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019232 long len;
19233 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
Bram Moolenaar0b962472016-02-22 22:51:33 +010019235 /* Pointer to current info struct used in compare function. Save and
19236 * restore the current one for nested calls. */
19237 old_sortinfo = sortinfo;
19238 sortinfo = &info;
19239
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019241 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 else
19243 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019244 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019245 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019246 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19247 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 rettv->vval.v_list = l;
19250 rettv->v_type = VAR_LIST;
19251 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252
Bram Moolenaar0d660222005-01-07 21:51:51 +000019253 len = list_len(l);
19254 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256
Bram Moolenaar0b962472016-02-22 22:51:33 +010019257 info.item_compare_ic = FALSE;
19258 info.item_compare_numeric = FALSE;
19259 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019260#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019261 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019262#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019263 info.item_compare_func = NULL;
19264 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265 if (argvars[1].v_type != VAR_UNKNOWN)
19266 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019267 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019268 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019269 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019270 else
19271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019272 int error = FALSE;
19273
19274 i = get_tv_number_chk(&argvars[1], &error);
19275 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019276 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019278 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019279 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019280 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019281 else if (i != 0)
19282 {
19283 EMSG(_(e_invarg));
19284 goto theend;
19285 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019287 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019288 if (*info.item_compare_func == NUL)
19289 {
19290 /* empty string means default sort */
19291 info.item_compare_func = NULL;
19292 }
19293 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019294 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019295 info.item_compare_func = NULL;
19296 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019297 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019298 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019299 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019300 info.item_compare_func = NULL;
19301 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019302 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019303#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019305 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019306 info.item_compare_func = NULL;
19307 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019308 }
19309#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019310 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019311 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 info.item_compare_func = NULL;
19313 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019314 }
19315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019317
19318 if (argvars[2].v_type != VAR_UNKNOWN)
19319 {
19320 /* optional third argument: {dict} */
19321 if (argvars[2].v_type != VAR_DICT)
19322 {
19323 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019324 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019325 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019326 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019327 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329
Bram Moolenaar0d660222005-01-07 21:51:51 +000019330 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019331 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019332 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019333 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334
Bram Moolenaar327aa022014-03-25 18:24:23 +010019335 i = 0;
19336 if (sort)
19337 {
19338 /* sort(): ptrs will be the list to sort */
19339 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019340 {
19341 ptrs[i].item = li;
19342 ptrs[i].idx = i;
19343 ++i;
19344 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019345
Bram Moolenaar0b962472016-02-22 22:51:33 +010019346 info.item_compare_func_err = FALSE;
19347 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019348 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019349 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019350 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019351 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019352 EMSG(_("E702: Sort compare function failed"));
19353 else
19354 {
19355 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019356 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019357 info.item_compare_func == NULL
19358 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019359
Bram Moolenaar0b962472016-02-22 22:51:33 +010019360 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019361 {
19362 /* Clear the List and append the items in sorted order. */
19363 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19364 l->lv_len = 0;
19365 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019366 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019367 }
19368 }
19369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019370 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019372 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019373
19374 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 info.item_compare_func_err = FALSE;
19376 info.item_compare_keep_zero = TRUE;
19377 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019378 ? item_compare2 : item_compare;
19379
19380 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19381 li = li->li_next)
19382 {
19383 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19384 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019385 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019386 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019387 {
19388 EMSG(_("E882: Uniq compare function failed"));
19389 break;
19390 }
19391 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019392
Bram Moolenaar0b962472016-02-22 22:51:33 +010019393 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019394 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019395 while (--i >= 0)
19396 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019397 li = ptrs[i].item->li_next;
19398 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019399 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019400 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019401 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019402 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019403 list_fix_watch(l, li);
19404 listitem_free(li);
19405 l->lv_len--;
19406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408 }
19409
19410 vim_free(ptrs);
19411 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019412theend:
19413 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414}
19415
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019416/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019417 * "sort({list})" function
19418 */
19419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019420f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019421{
19422 do_sort_uniq(argvars, rettv, TRUE);
19423}
19424
19425/*
19426 * "uniq({list})" function
19427 */
19428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019429f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019430{
19431 do_sort_uniq(argvars, rettv, FALSE);
19432}
19433
19434/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019435 * "soundfold({word})" function
19436 */
19437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019438f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019439{
19440 char_u *s;
19441
19442 rettv->v_type = VAR_STRING;
19443 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019444#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019445 rettv->vval.v_string = eval_soundfold(s);
19446#else
19447 rettv->vval.v_string = vim_strsave(s);
19448#endif
19449}
19450
19451/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019452 * "spellbadword()" function
19453 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019455f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019456{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019457 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019458 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019459 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019461 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019462 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019463
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019464#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019465 if (argvars[0].v_type == VAR_UNKNOWN)
19466 {
19467 /* Find the start and length of the badly spelled word. */
19468 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19469 if (len != 0)
19470 word = ml_get_cursor();
19471 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019472 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019473 {
19474 char_u *str = get_tv_string_chk(&argvars[0]);
19475 int capcol = -1;
19476
19477 if (str != NULL)
19478 {
19479 /* Check the argument for spelling. */
19480 while (*str != NUL)
19481 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019482 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019483 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019484 {
19485 word = str;
19486 break;
19487 }
19488 str += len;
19489 }
19490 }
19491 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019492#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019494 list_append_string(rettv->vval.v_list, word, len);
19495 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019496 attr == HLF_SPB ? "bad" :
19497 attr == HLF_SPR ? "rare" :
19498 attr == HLF_SPL ? "local" :
19499 attr == HLF_SPC ? "caps" :
19500 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019501}
19502
19503/*
19504 * "spellsuggest()" function
19505 */
19506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019507f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019508{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019509#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019510 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019511 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019512 int maxcount;
19513 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019514 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019515 listitem_T *li;
19516 int need_capital = FALSE;
19517#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019519 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019520 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019521
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019522#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019523 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019524 {
19525 str = get_tv_string(&argvars[0]);
19526 if (argvars[1].v_type != VAR_UNKNOWN)
19527 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019528 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019529 if (maxcount <= 0)
19530 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019531 if (argvars[2].v_type != VAR_UNKNOWN)
19532 {
19533 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19534 if (typeerr)
19535 return;
19536 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019537 }
19538 else
19539 maxcount = 25;
19540
Bram Moolenaar4770d092006-01-12 23:22:24 +000019541 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019542
19543 for (i = 0; i < ga.ga_len; ++i)
19544 {
19545 str = ((char_u **)ga.ga_data)[i];
19546
19547 li = listitem_alloc();
19548 if (li == NULL)
19549 vim_free(str);
19550 else
19551 {
19552 li->li_tv.v_type = VAR_STRING;
19553 li->li_tv.v_lock = 0;
19554 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019555 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019556 }
19557 }
19558 ga_clear(&ga);
19559 }
19560#endif
19561}
19562
Bram Moolenaar0d660222005-01-07 21:51:51 +000019563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019564f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565{
19566 char_u *str;
19567 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019568 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019569 regmatch_T regmatch;
19570 char_u patbuf[NUMBUFLEN];
19571 char_u *save_cpo;
19572 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019573 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019574 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019575 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019576
19577 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19578 save_cpo = p_cpo;
19579 p_cpo = (char_u *)"";
19580
19581 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019582 if (argvars[1].v_type != VAR_UNKNOWN)
19583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019584 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19585 if (pat == NULL)
19586 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019588 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019589 }
19590 if (pat == NULL || *pat == NUL)
19591 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019592
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019593 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019595 if (typeerr)
19596 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597
Bram Moolenaar0d660222005-01-07 21:51:51 +000019598 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19599 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019601 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019602 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019603 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019604 if (*str == NUL)
19605 match = FALSE; /* empty item at the end */
19606 else
19607 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608 if (match)
19609 end = regmatch.startp[0];
19610 else
19611 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019612 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19613 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019614 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019615 if (list_append_string(rettv->vval.v_list, str,
19616 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019617 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019618 }
19619 if (!match)
19620 break;
19621 /* Advance to just after the match. */
19622 if (regmatch.endp[0] > str)
19623 col = 0;
19624 else
19625 {
19626 /* Don't get stuck at the same match. */
19627#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019628 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019629#else
19630 col = 1;
19631#endif
19632 }
19633 str = regmatch.endp[0];
19634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635
Bram Moolenaar473de612013-06-08 18:19:48 +020019636 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638
Bram Moolenaar0d660222005-01-07 21:51:51 +000019639 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640}
19641
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019642#ifdef FEAT_FLOAT
19643/*
19644 * "sqrt()" function
19645 */
19646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019647f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019648{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019649 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019650
19651 rettv->v_type = VAR_FLOAT;
19652 if (get_float_arg(argvars, &f) == OK)
19653 rettv->vval.v_float = sqrt(f);
19654 else
19655 rettv->vval.v_float = 0.0;
19656}
19657
19658/*
19659 * "str2float()" function
19660 */
19661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019662f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019663{
19664 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19665
19666 if (*p == '+')
19667 p = skipwhite(p + 1);
19668 (void)string2float(p, &rettv->vval.v_float);
19669 rettv->v_type = VAR_FLOAT;
19670}
19671#endif
19672
Bram Moolenaar2c932302006-03-18 21:42:09 +000019673/*
19674 * "str2nr()" function
19675 */
19676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019677f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019678{
19679 int base = 10;
19680 char_u *p;
19681 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019682 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019683
19684 if (argvars[1].v_type != VAR_UNKNOWN)
19685 {
19686 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019687 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019688 {
19689 EMSG(_(e_invarg));
19690 return;
19691 }
19692 }
19693
19694 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019695 if (*p == '+')
19696 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019697 switch (base)
19698 {
19699 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19700 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19701 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19702 default: what = 0;
19703 }
19704 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019705 rettv->vval.v_number = n;
19706}
19707
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708#ifdef HAVE_STRFTIME
19709/*
19710 * "strftime({format}[, {time}])" function
19711 */
19712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019713f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714{
19715 char_u result_buf[256];
19716 struct tm *curtime;
19717 time_t seconds;
19718 char_u *p;
19719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724 seconds = time(NULL);
19725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 curtime = localtime(&seconds);
19728 /* MSVC returns NULL for an invalid value of seconds. */
19729 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 else
19732 {
19733# ifdef FEAT_MBYTE
19734 vimconv_T conv;
19735 char_u *enc;
19736
19737 conv.vc_type = CONV_NONE;
19738 enc = enc_locale();
19739 convert_setup(&conv, p_enc, enc);
19740 if (conv.vc_type != CONV_NONE)
19741 p = string_convert(&conv, p, NULL);
19742# endif
19743 if (p != NULL)
19744 (void)strftime((char *)result_buf, sizeof(result_buf),
19745 (char *)p, curtime);
19746 else
19747 result_buf[0] = NUL;
19748
19749# ifdef FEAT_MBYTE
19750 if (conv.vc_type != CONV_NONE)
19751 vim_free(p);
19752 convert_setup(&conv, enc, p_enc);
19753 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019754 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 else
19756# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019757 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758
19759# ifdef FEAT_MBYTE
19760 /* Release conversion descriptors */
19761 convert_setup(&conv, NULL, NULL);
19762 vim_free(enc);
19763# endif
19764 }
19765}
19766#endif
19767
19768/*
19769 * "stridx()" function
19770 */
19771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
19774 char_u buf[NUMBUFLEN];
19775 char_u *needle;
19776 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019779 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019781 needle = get_tv_string_chk(&argvars[1]);
19782 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019784 if (needle == NULL || haystack == NULL)
19785 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 if (argvars[2].v_type != VAR_UNKNOWN)
19788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019789 int error = FALSE;
19790
19791 start_idx = get_tv_number_chk(&argvars[2], &error);
19792 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019794 if (start_idx >= 0)
19795 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 }
19797
19798 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19799 if (pos != NULL)
19800 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801}
19802
19803/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 * "string()" function
19805 */
19806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019807f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808{
19809 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019810 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019813 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019814 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019815 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019816 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817}
19818
19819/*
19820 * "strlen()" function
19821 */
19822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019823f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 rettv->vval.v_number = (varnumber_T)(STRLEN(
19826 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827}
19828
19829/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019830 * "strchars()" function
19831 */
19832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019833f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019834{
19835 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019836 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019837#ifdef FEAT_MBYTE
19838 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019839 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019840#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019841
19842 if (argvars[1].v_type != VAR_UNKNOWN)
19843 skipcc = get_tv_number_chk(&argvars[1], NULL);
19844 if (skipcc < 0 || skipcc > 1)
19845 EMSG(_(e_invarg));
19846 else
19847 {
19848#ifdef FEAT_MBYTE
19849 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19850 while (*s != NUL)
19851 {
19852 func_mb_ptr2char_adv(&s);
19853 ++len;
19854 }
19855 rettv->vval.v_number = len;
19856#else
19857 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19858#endif
19859 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019860}
19861
19862/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019863 * "strdisplaywidth()" function
19864 */
19865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019866f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019867{
19868 char_u *s = get_tv_string(&argvars[0]);
19869 int col = 0;
19870
19871 if (argvars[1].v_type != VAR_UNKNOWN)
19872 col = get_tv_number(&argvars[1]);
19873
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019874 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019875}
19876
19877/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019878 * "strwidth()" function
19879 */
19880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019881f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019882{
19883 char_u *s = get_tv_string(&argvars[0]);
19884
19885 rettv->vval.v_number = (varnumber_T)(
19886#ifdef FEAT_MBYTE
19887 mb_string2cells(s, -1)
19888#else
19889 STRLEN(s)
19890#endif
19891 );
19892}
19893
19894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 * "strpart()" function
19896 */
19897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019898f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899{
19900 char_u *p;
19901 int n;
19902 int len;
19903 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 slen = (int)STRLEN(p);
19908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019909 n = get_tv_number_chk(&argvars[1], &error);
19910 if (error)
19911 len = 0;
19912 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019913 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 else
19915 len = slen - n; /* default len: all bytes that are available. */
19916
19917 /*
19918 * Only return the overlap between the specified part and the actual
19919 * string.
19920 */
19921 if (n < 0)
19922 {
19923 len += n;
19924 n = 0;
19925 }
19926 else if (n > slen)
19927 n = slen;
19928 if (len < 0)
19929 len = 0;
19930 else if (n + len > slen)
19931 len = slen - n;
19932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935}
19936
19937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019938 * "strridx()" function
19939 */
19940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019941f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019942{
19943 char_u buf[NUMBUFLEN];
19944 char_u *needle;
19945 char_u *haystack;
19946 char_u *rest;
19947 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019948 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019950 needle = get_tv_string_chk(&argvars[1]);
19951 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019952
19953 rettv->vval.v_number = -1;
19954 if (needle == NULL || haystack == NULL)
19955 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019956
19957 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019958 if (argvars[2].v_type != VAR_UNKNOWN)
19959 {
19960 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019962 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019963 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019964 }
19965 else
19966 end_idx = haystack_len;
19967
Bram Moolenaar0d660222005-01-07 21:51:51 +000019968 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019969 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019970 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019971 lastmatch = haystack + end_idx;
19972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019973 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019974 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019975 for (rest = haystack; *rest != '\0'; ++rest)
19976 {
19977 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019978 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019979 break;
19980 lastmatch = rest;
19981 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019983
19984 if (lastmatch == NULL)
19985 rettv->vval.v_number = -1;
19986 else
19987 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19988}
19989
19990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 * "strtrans()" function
19992 */
19993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019994f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 rettv->v_type = VAR_STRING;
19997 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020001 * "submatch()" function
20002 */
20003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020004f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020005{
Bram Moolenaar41571762014-04-02 19:00:58 +020020006 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020007 int no;
20008 int retList = 0;
20009
20010 no = (int)get_tv_number_chk(&argvars[0], &error);
20011 if (error)
20012 return;
20013 error = FALSE;
20014 if (argvars[1].v_type != VAR_UNKNOWN)
20015 retList = get_tv_number_chk(&argvars[1], &error);
20016 if (error)
20017 return;
20018
20019 if (retList == 0)
20020 {
20021 rettv->v_type = VAR_STRING;
20022 rettv->vval.v_string = reg_submatch(no);
20023 }
20024 else
20025 {
20026 rettv->v_type = VAR_LIST;
20027 rettv->vval.v_list = reg_submatch_list(no);
20028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020029}
20030
20031/*
20032 * "substitute()" function
20033 */
20034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020035f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020036{
20037 char_u patbuf[NUMBUFLEN];
20038 char_u subbuf[NUMBUFLEN];
20039 char_u flagsbuf[NUMBUFLEN];
20040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020041 char_u *str = get_tv_string_chk(&argvars[0]);
20042 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20043 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20044 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20045
Bram Moolenaar0d660222005-01-07 21:51:51 +000020046 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20048 rettv->vval.v_string = NULL;
20049 else
20050 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020051}
20052
20053/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020054 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020057f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058{
20059 int id = 0;
20060#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020061 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 long col;
20063 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020064 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020066 lnum = get_tv_lnum(argvars); /* -1 on type error */
20067 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20068 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020071 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020072 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073#endif
20074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076}
20077
20078/*
20079 * "synIDattr(id, what [, mode])" function
20080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020082f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083{
20084 char_u *p = NULL;
20085#ifdef FEAT_SYN_HL
20086 int id;
20087 char_u *what;
20088 char_u *mode;
20089 char_u modebuf[NUMBUFLEN];
20090 int modec;
20091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 id = get_tv_number(&argvars[0]);
20093 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020098 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 modec = 0; /* replace invalid with current */
20100 }
20101 else
20102 {
20103#ifdef FEAT_GUI
20104 if (gui.in_use)
20105 modec = 'g';
20106 else
20107#endif
20108 if (t_colors > 1)
20109 modec = 'c';
20110 else
20111 modec = 't';
20112 }
20113
20114
20115 switch (TOLOWER_ASC(what[0]))
20116 {
20117 case 'b':
20118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20119 p = highlight_color(id, what, modec);
20120 else /* bold */
20121 p = highlight_has_attr(id, HL_BOLD, modec);
20122 break;
20123
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020124 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 p = highlight_color(id, what, modec);
20126 break;
20127
20128 case 'i':
20129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20130 p = highlight_has_attr(id, HL_INVERSE, modec);
20131 else /* italic */
20132 p = highlight_has_attr(id, HL_ITALIC, modec);
20133 break;
20134
20135 case 'n': /* name */
20136 p = get_highlight_name(NULL, id - 1);
20137 break;
20138
20139 case 'r': /* reverse */
20140 p = highlight_has_attr(id, HL_INVERSE, modec);
20141 break;
20142
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020143 case 's':
20144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20145 p = highlight_color(id, what, modec);
20146 else /* standout */
20147 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 break;
20149
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020150 case 'u':
20151 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20152 /* underline */
20153 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20154 else
20155 /* undercurl */
20156 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 break;
20158 }
20159
20160 if (p != NULL)
20161 p = vim_strsave(p);
20162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 rettv->v_type = VAR_STRING;
20164 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165}
20166
20167/*
20168 * "synIDtrans(id)" function
20169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020171f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172{
20173 int id;
20174
20175#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177
20178 if (id > 0)
20179 id = syn_get_final_id(id);
20180 else
20181#endif
20182 id = 0;
20183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185}
20186
20187/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020188 * "synconcealed(lnum, col)" function
20189 */
20190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020191f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020192{
20193#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20194 long lnum;
20195 long col;
20196 int syntax_flags = 0;
20197 int cchar;
20198 int matchid = 0;
20199 char_u str[NUMBUFLEN];
20200#endif
20201
20202 rettv->v_type = VAR_LIST;
20203 rettv->vval.v_list = NULL;
20204
20205#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20206 lnum = get_tv_lnum(argvars); /* -1 on type error */
20207 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20208
20209 vim_memset(str, NUL, sizeof(str));
20210
20211 if (rettv_list_alloc(rettv) != FAIL)
20212 {
20213 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20214 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20215 && curwin->w_p_cole > 0)
20216 {
20217 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20218 syntax_flags = get_syntax_info(&matchid);
20219
20220 /* get the conceal character */
20221 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20222 {
20223 cchar = syn_get_sub_char();
20224 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20225 cchar = lcs_conceal;
20226 if (cchar != NUL)
20227 {
20228# ifdef FEAT_MBYTE
20229 if (has_mbyte)
20230 (*mb_char2bytes)(cchar, str);
20231 else
20232# endif
20233 str[0] = cchar;
20234 }
20235 }
20236 }
20237
20238 list_append_number(rettv->vval.v_list,
20239 (syntax_flags & HL_CONCEAL) != 0);
20240 /* -1 to auto-determine strlen */
20241 list_append_string(rettv->vval.v_list, str, -1);
20242 list_append_number(rettv->vval.v_list, matchid);
20243 }
20244#endif
20245}
20246
20247/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020248 * "synstack(lnum, col)" function
20249 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020252{
20253#ifdef FEAT_SYN_HL
20254 long lnum;
20255 long col;
20256 int i;
20257 int id;
20258#endif
20259
20260 rettv->v_type = VAR_LIST;
20261 rettv->vval.v_list = NULL;
20262
20263#ifdef FEAT_SYN_HL
20264 lnum = get_tv_lnum(argvars); /* -1 on type error */
20265 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20266
20267 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020268 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020269 && rettv_list_alloc(rettv) != FAIL)
20270 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020271 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020272 for (i = 0; ; ++i)
20273 {
20274 id = syn_get_stack_item(i);
20275 if (id < 0)
20276 break;
20277 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20278 break;
20279 }
20280 }
20281#endif
20282}
20283
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020285get_cmd_output_as_rettv(
20286 typval_T *argvars,
20287 typval_T *rettv,
20288 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020290 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020292 char_u *infile = NULL;
20293 char_u buf[NUMBUFLEN];
20294 int err = FALSE;
20295 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020296 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020297 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 rettv->v_type = VAR_STRING;
20300 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020301 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020303
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020305 {
20306 /*
20307 * Write the string to a temp file, to be used for input of the shell
20308 * command.
20309 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020310 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020311 {
20312 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020313 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020314 }
20315
20316 fd = mch_fopen((char *)infile, WRITEBIN);
20317 if (fd == NULL)
20318 {
20319 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020320 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020321 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020322 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020323 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020324 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20325 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020326 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020327 else
20328 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020329 size_t len;
20330
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020331 p = get_tv_string_buf_chk(&argvars[1], buf);
20332 if (p == NULL)
20333 {
20334 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020335 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020336 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020337 len = STRLEN(p);
20338 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020339 err = TRUE;
20340 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020341 if (fclose(fd) != 0)
20342 err = TRUE;
20343 if (err)
20344 {
20345 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020347 }
20348 }
20349
Bram Moolenaar52a72462014-08-29 15:53:52 +020020350 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20351 * echoes typeahead, that messes up the display. */
20352 if (!msg_silent)
20353 flags += SHELL_COOKED;
20354
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020355 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020357 int len;
20358 listitem_T *li;
20359 char_u *s = NULL;
20360 char_u *start;
20361 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020362 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
Bram Moolenaar52a72462014-08-29 15:53:52 +020020364 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020365 if (res == NULL)
20366 goto errret;
20367
20368 list = list_alloc();
20369 if (list == NULL)
20370 goto errret;
20371
20372 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020375 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020376 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020377 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020379 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020380 if (s == NULL)
20381 goto errret;
20382
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020383 for (p = s; start < end; ++p, ++start)
20384 *p = *start == NUL ? NL : *start;
20385 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386
20387 li = listitem_alloc();
20388 if (li == NULL)
20389 {
20390 vim_free(s);
20391 goto errret;
20392 }
20393 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020394 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395 li->li_tv.vval.v_string = s;
20396 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020398
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020399 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020400 rettv->v_type = VAR_LIST;
20401 rettv->vval.v_list = list;
20402 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020404 else
20405 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020406 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020407#ifdef USE_CR
20408 /* translate <CR> into <NL> */
20409 if (res != NULL)
20410 {
20411 char_u *s;
20412
20413 for (s = res; *s; ++s)
20414 {
20415 if (*s == CAR)
20416 *s = NL;
20417 }
20418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419#else
20420# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020421 /* translate <CR><NL> into <NL> */
20422 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020424 char_u *s, *d;
20425
20426 d = res;
20427 for (s = res; *s; ++s)
20428 {
20429 if (s[0] == CAR && s[1] == NL)
20430 ++s;
20431 *d++ = *s;
20432 }
20433 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435# endif
20436#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437 rettv->vval.v_string = res;
20438 res = NULL;
20439 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020440
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020441errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020442 if (infile != NULL)
20443 {
20444 mch_remove(infile);
20445 vim_free(infile);
20446 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 if (res != NULL)
20448 vim_free(res);
20449 if (list != NULL)
20450 list_free(list, TRUE);
20451}
20452
20453/*
20454 * "system()" function
20455 */
20456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020457f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020458{
20459 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20460}
20461
20462/*
20463 * "systemlist()" function
20464 */
20465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467{
20468 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
20471/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020472 * "tabpagebuflist()" function
20473 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020475f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020476{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020477#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020478 tabpage_T *tp;
20479 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020480
20481 if (argvars[0].v_type == VAR_UNKNOWN)
20482 wp = firstwin;
20483 else
20484 {
20485 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20486 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020487 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020488 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020489 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020490 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020491 for (; wp != NULL; wp = wp->w_next)
20492 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020493 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020494 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020495 }
20496#endif
20497}
20498
20499
20500/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020501 * "tabpagenr()" function
20502 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020504f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020505{
20506 int nr = 1;
20507#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020508 char_u *arg;
20509
20510 if (argvars[0].v_type != VAR_UNKNOWN)
20511 {
20512 arg = get_tv_string_chk(&argvars[0]);
20513 nr = 0;
20514 if (arg != NULL)
20515 {
20516 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020517 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020518 else
20519 EMSG2(_(e_invexpr2), arg);
20520 }
20521 }
20522 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020523 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020524#endif
20525 rettv->vval.v_number = nr;
20526}
20527
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020528
20529#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020530static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020531
20532/*
20533 * Common code for tabpagewinnr() and winnr().
20534 */
20535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020537{
20538 win_T *twin;
20539 int nr = 1;
20540 win_T *wp;
20541 char_u *arg;
20542
20543 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20544 if (argvar->v_type != VAR_UNKNOWN)
20545 {
20546 arg = get_tv_string_chk(argvar);
20547 if (arg == NULL)
20548 nr = 0; /* type error; errmsg already given */
20549 else if (STRCMP(arg, "$") == 0)
20550 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20551 else if (STRCMP(arg, "#") == 0)
20552 {
20553 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20554 if (twin == NULL)
20555 nr = 0;
20556 }
20557 else
20558 {
20559 EMSG2(_(e_invexpr2), arg);
20560 nr = 0;
20561 }
20562 }
20563
20564 if (nr > 0)
20565 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20566 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020567 {
20568 if (wp == NULL)
20569 {
20570 /* didn't find it in this tabpage */
20571 nr = 0;
20572 break;
20573 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020575 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020576 return nr;
20577}
20578#endif
20579
20580/*
20581 * "tabpagewinnr()" function
20582 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020585{
20586 int nr = 1;
20587#ifdef FEAT_WINDOWS
20588 tabpage_T *tp;
20589
20590 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20591 if (tp == NULL)
20592 nr = 0;
20593 else
20594 nr = get_winnr(tp, &argvars[1]);
20595#endif
20596 rettv->vval.v_number = nr;
20597}
20598
20599
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020600/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020601 * "tagfiles()" function
20602 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020605{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020606 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020607 tagname_T tn;
20608 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020611 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020612 fname = alloc(MAXPATHL);
20613 if (fname == NULL)
20614 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020616 for (first = TRUE; ; first = FALSE)
20617 if (get_tagfname(&tn, first, fname) == FAIL
20618 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020619 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020620 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020621 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020622}
20623
20624/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020625 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020626 */
20627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020629{
20630 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020631
20632 tag_pattern = get_tv_string(&argvars[0]);
20633
20634 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020635 if (*tag_pattern == NUL)
20636 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020638 if (rettv_list_alloc(rettv) == OK)
20639 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020640}
20641
20642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 * "tempname()" function
20644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647{
20648 static int x = 'A';
20649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020650 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020651 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652
20653 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20654 * names. Skip 'I' and 'O', they are used for shell redirection. */
20655 do
20656 {
20657 if (x == 'Z')
20658 x = '0';
20659 else if (x == '9')
20660 x = 'A';
20661 else
20662 {
20663#ifdef EBCDIC
20664 if (x == 'I')
20665 x = 'J';
20666 else if (x == 'R')
20667 x = 'S';
20668 else
20669#endif
20670 ++x;
20671 }
20672 } while (x == 'I' || x == 'O');
20673}
20674
20675/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020676 * "test(list)" function: Just checking the walls...
20677 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020679f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020680{
20681 /* Used for unit testing. Change the code below to your liking. */
20682#if 0
20683 listitem_T *li;
20684 list_T *l;
20685 char_u *bad, *good;
20686
20687 if (argvars[0].v_type != VAR_LIST)
20688 return;
20689 l = argvars[0].vval.v_list;
20690 if (l == NULL)
20691 return;
20692 li = l->lv_first;
20693 if (li == NULL)
20694 return;
20695 bad = get_tv_string(&li->li_tv);
20696 li = li->li_next;
20697 if (li == NULL)
20698 return;
20699 good = get_tv_string(&li->li_tv);
20700 rettv->vval.v_number = test_edit_score(bad, good);
20701#endif
20702}
20703
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020704#ifdef FEAT_FLOAT
20705/*
20706 * "tan()" function
20707 */
20708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020709f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020710{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020711 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020712
20713 rettv->v_type = VAR_FLOAT;
20714 if (get_float_arg(argvars, &f) == OK)
20715 rettv->vval.v_float = tan(f);
20716 else
20717 rettv->vval.v_float = 0.0;
20718}
20719
20720/*
20721 * "tanh()" function
20722 */
20723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020724f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020725{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020726 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020727
20728 rettv->v_type = VAR_FLOAT;
20729 if (get_float_arg(argvars, &f) == OK)
20730 rettv->vval.v_float = tanh(f);
20731 else
20732 rettv->vval.v_float = 0.0;
20733}
20734#endif
20735
Bram Moolenaard52d9742005-08-21 22:20:28 +000020736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 * "tolower(string)" function
20738 */
20739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020740f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741{
20742 char_u *p;
20743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 p = vim_strsave(get_tv_string(&argvars[0]));
20745 rettv->v_type = VAR_STRING;
20746 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747
20748 if (p != NULL)
20749 while (*p != NUL)
20750 {
20751#ifdef FEAT_MBYTE
20752 int l;
20753
20754 if (enc_utf8)
20755 {
20756 int c, lc;
20757
20758 c = utf_ptr2char(p);
20759 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020760 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 /* TODO: reallocate string when byte count changes. */
20762 if (utf_char2len(lc) == l)
20763 utf_char2bytes(lc, p);
20764 p += l;
20765 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020766 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 p += l; /* skip multi-byte character */
20768 else
20769#endif
20770 {
20771 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20772 ++p;
20773 }
20774 }
20775}
20776
20777/*
20778 * "toupper(string)" function
20779 */
20780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020783 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020784 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785}
20786
20787/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020788 * "tr(string, fromstr, tostr)" function
20789 */
20790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020791f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020792{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020793 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020794 char_u *fromstr;
20795 char_u *tostr;
20796 char_u *p;
20797#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020798 int inlen;
20799 int fromlen;
20800 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020801 int idx;
20802 char_u *cpstr;
20803 int cplen;
20804 int first = TRUE;
20805#endif
20806 char_u buf[NUMBUFLEN];
20807 char_u buf2[NUMBUFLEN];
20808 garray_T ga;
20809
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020810 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020811 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20812 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020813
20814 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020815 rettv->v_type = VAR_STRING;
20816 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020817 if (fromstr == NULL || tostr == NULL)
20818 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020819 ga_init2(&ga, (int)sizeof(char), 80);
20820
20821#ifdef FEAT_MBYTE
20822 if (!has_mbyte)
20823#endif
20824 /* not multi-byte: fromstr and tostr must be the same length */
20825 if (STRLEN(fromstr) != STRLEN(tostr))
20826 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020827#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020828error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020829#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020830 EMSG2(_(e_invarg2), fromstr);
20831 ga_clear(&ga);
20832 return;
20833 }
20834
20835 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020836 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020837 {
20838#ifdef FEAT_MBYTE
20839 if (has_mbyte)
20840 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020841 inlen = (*mb_ptr2len)(in_str);
20842 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020843 cplen = inlen;
20844 idx = 0;
20845 for (p = fromstr; *p != NUL; p += fromlen)
20846 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020847 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020848 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020849 {
20850 for (p = tostr; *p != NUL; p += tolen)
20851 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020852 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020853 if (idx-- == 0)
20854 {
20855 cplen = tolen;
20856 cpstr = p;
20857 break;
20858 }
20859 }
20860 if (*p == NUL) /* tostr is shorter than fromstr */
20861 goto error;
20862 break;
20863 }
20864 ++idx;
20865 }
20866
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020867 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020868 {
20869 /* Check that fromstr and tostr have the same number of
20870 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020871 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020872 first = FALSE;
20873 for (p = tostr; *p != NUL; p += tolen)
20874 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020875 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020876 --idx;
20877 }
20878 if (idx != 0)
20879 goto error;
20880 }
20881
Bram Moolenaarcde88542015-08-11 19:14:00 +020020882 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020883 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020884 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020885
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020886 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 }
20888 else
20889#endif
20890 {
20891 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020892 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020893 if (p != NULL)
20894 ga_append(&ga, tostr[p - fromstr]);
20895 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020896 ga_append(&ga, *in_str);
20897 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020898 }
20899 }
20900
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020901 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020902 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020903 ga_append(&ga, NUL);
20904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020905 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020906}
20907
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020908#ifdef FEAT_FLOAT
20909/*
20910 * "trunc({float})" function
20911 */
20912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020913f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020914{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020915 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020916
20917 rettv->v_type = VAR_FLOAT;
20918 if (get_float_arg(argvars, &f) == OK)
20919 /* trunc() is not in C90, use floor() or ceil() instead. */
20920 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20921 else
20922 rettv->vval.v_float = 0.0;
20923}
20924#endif
20925
Bram Moolenaar8299df92004-07-10 09:47:34 +000020926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927 * "type(expr)" function
20928 */
20929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020930f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020932 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020933
20934 switch (argvars[0].v_type)
20935 {
20936 case VAR_NUMBER: n = 0; break;
20937 case VAR_STRING: n = 1; break;
20938 case VAR_FUNC: n = 2; break;
20939 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020940 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020941 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020942 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020943 if (argvars[0].vval.v_number == VVAL_FALSE
20944 || argvars[0].vval.v_number == VVAL_TRUE)
20945 n = 6;
20946 else
20947 n = 7;
20948 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020949 case VAR_JOB: n = 8; break;
20950 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020951 case VAR_UNKNOWN:
20952 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20953 n = -1;
20954 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020955 }
20956 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957}
20958
20959/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020960 * "undofile(name)" function
20961 */
20962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020963f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020964{
20965 rettv->v_type = VAR_STRING;
20966#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020967 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020968 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020969
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020970 if (*fname == NUL)
20971 {
20972 /* If there is no file name there will be no undo file. */
20973 rettv->vval.v_string = NULL;
20974 }
20975 else
20976 {
20977 char_u *ffname = FullName_save(fname, FALSE);
20978
20979 if (ffname != NULL)
20980 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20981 vim_free(ffname);
20982 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020983 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020984#else
20985 rettv->vval.v_string = NULL;
20986#endif
20987}
20988
20989/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020990 * "undotree()" function
20991 */
20992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020993f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020994{
20995 if (rettv_dict_alloc(rettv) == OK)
20996 {
20997 dict_T *dict = rettv->vval.v_dict;
20998 list_T *list;
20999
Bram Moolenaar730cde92010-06-27 05:18:54 +020021000 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021001 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021002 dict_add_nr_str(dict, "save_last",
21003 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021004 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21005 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021006 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021007
21008 list = list_alloc();
21009 if (list != NULL)
21010 {
21011 u_eval_tree(curbuf->b_u_oldhead, list);
21012 dict_add_list(dict, "entries", list);
21013 }
21014 }
21015}
21016
21017/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021018 * "values(dict)" function
21019 */
21020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021021f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021022{
21023 dict_list(argvars, rettv, 1);
21024}
21025
21026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 * "virtcol(string)" function
21028 */
21029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021030f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031{
21032 colnr_T vcol = 0;
21033 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021034 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021036 fp = var2fpos(&argvars[0], FALSE, &fnum);
21037 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21038 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 {
21040 getvvcol(curwin, fp, NULL, NULL, &vcol);
21041 ++vcol;
21042 }
21043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045}
21046
21047/*
21048 * "visualmode()" function
21049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021051f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 char_u str[2];
21054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 str[0] = curbuf->b_visual_mode_eval;
21057 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021058 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059
21060 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021061 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063}
21064
21065/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021066 * "wildmenumode()" function
21067 */
21068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021069f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021070{
21071#ifdef FEAT_WILDMENU
21072 if (wild_menu_showing)
21073 rettv->vval.v_number = 1;
21074#endif
21075}
21076
21077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 * "winbufnr(nr)" function
21079 */
21080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082{
21083 win_T *wp;
21084
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021085 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090}
21091
21092/*
21093 * "wincol()" function
21094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021096f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097{
21098 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021099 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100}
21101
21102/*
21103 * "winheight(nr)" function
21104 */
21105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021106f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107{
21108 win_T *wp;
21109
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021110 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021112 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115}
21116
21117/*
21118 * "winline()" function
21119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021121f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122{
21123 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021124 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125}
21126
21127/*
21128 * "winnr()" function
21129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021131f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132{
21133 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021134
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021136 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139}
21140
21141/*
21142 * "winrestcmd()" function
21143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021145f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146{
21147#ifdef FEAT_WINDOWS
21148 win_T *wp;
21149 int winnr = 1;
21150 garray_T ga;
21151 char_u buf[50];
21152
21153 ga_init2(&ga, (int)sizeof(char), 70);
21154 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21155 {
21156 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21157 ga_concat(&ga, buf);
21158# ifdef FEAT_VERTSPLIT
21159 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21160 ga_concat(&ga, buf);
21161# endif
21162 ++winnr;
21163 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021164 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171}
21172
21173/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021174 * "winrestview()" function
21175 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021178{
21179 dict_T *dict;
21180
21181 if (argvars[0].v_type != VAR_DICT
21182 || (dict = argvars[0].vval.v_dict) == NULL)
21183 EMSG(_(e_invarg));
21184 else
21185 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021186 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21187 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21188 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21189 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021190#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021191 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21192 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021193#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021194 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21195 {
21196 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21197 curwin->w_set_curswant = FALSE;
21198 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021199
Bram Moolenaar82c25852014-05-28 16:47:16 +020021200 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21201 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021202#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021203 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21204 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021205#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021206 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21207 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21208 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21209 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021210
21211 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021212 win_new_height(curwin, curwin->w_height);
21213# ifdef FEAT_VERTSPLIT
21214 win_new_width(curwin, W_WIDTH(curwin));
21215# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021216 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021217
Bram Moolenaarb851a962014-10-31 15:45:52 +010021218 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021219 curwin->w_topline = 1;
21220 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21221 curwin->w_topline = curbuf->b_ml.ml_line_count;
21222#ifdef FEAT_DIFF
21223 check_topfill(curwin, TRUE);
21224#endif
21225 }
21226}
21227
21228/*
21229 * "winsaveview()" function
21230 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021233{
21234 dict_T *dict;
21235
Bram Moolenaara800b422010-06-27 01:15:55 +020021236 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021237 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021238 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021239
21240 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21241 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21242#ifdef FEAT_VIRTUALEDIT
21243 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21244#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021245 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021246 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21247
21248 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21249#ifdef FEAT_DIFF
21250 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21251#endif
21252 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21253 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21254}
21255
21256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 * "winwidth(nr)" function
21258 */
21259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021260f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261{
21262 win_T *wp;
21263
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021264 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 else
21268#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021269 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021271 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272#endif
21273}
21274
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021276 * "wordcount()" function
21277 */
21278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021279f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021280{
21281 if (rettv_dict_alloc(rettv) == FAIL)
21282 return;
21283 cursor_pos_info(rettv->vval.v_dict);
21284}
21285
21286/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021287 * Write list of strings to file
21288 */
21289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021290write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021291{
21292 listitem_T *li;
21293 int c;
21294 int ret = OK;
21295 char_u *s;
21296
21297 for (li = list->lv_first; li != NULL; li = li->li_next)
21298 {
21299 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21300 {
21301 if (*s == '\n')
21302 c = putc(NUL, fd);
21303 else
21304 c = putc(*s, fd);
21305 if (c == EOF)
21306 {
21307 ret = FAIL;
21308 break;
21309 }
21310 }
21311 if (!binary || li->li_next != NULL)
21312 if (putc('\n', fd) == EOF)
21313 {
21314 ret = FAIL;
21315 break;
21316 }
21317 if (ret == FAIL)
21318 {
21319 EMSG(_(e_write));
21320 break;
21321 }
21322 }
21323 return ret;
21324}
21325
21326/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021327 * "writefile()" function
21328 */
21329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021330f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021331{
21332 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021333 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021334 char_u *fname;
21335 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021336 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021337
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021338 if (check_restricted() || check_secure())
21339 return;
21340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021341 if (argvars[0].v_type != VAR_LIST)
21342 {
21343 EMSG2(_(e_listarg), "writefile()");
21344 return;
21345 }
21346 if (argvars[0].vval.v_list == NULL)
21347 return;
21348
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021349 if (argvars[2].v_type != VAR_UNKNOWN)
21350 {
21351 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21352 binary = TRUE;
21353 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21354 append = TRUE;
21355 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356
21357 /* Always open the file in binary mode, library functions have a mind of
21358 * their own about CR-LF conversion. */
21359 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021360 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21361 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021362 {
21363 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21364 ret = -1;
21365 }
21366 else
21367 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021368 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21369 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021370 fclose(fd);
21371 }
21372
21373 rettv->vval.v_number = ret;
21374}
21375
21376/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021377 * "xor(expr, expr)" function
21378 */
21379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021381{
21382 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21383 ^ get_tv_number_chk(&argvars[1], NULL);
21384}
21385
21386
21387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021389 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 */
21391 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392var2fpos(
21393 typval_T *varp,
21394 int dollar_lnum, /* TRUE when $ is last line */
21395 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021397 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021399 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400
Bram Moolenaara5525202006-03-02 22:52:09 +000021401 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021402 if (varp->v_type == VAR_LIST)
21403 {
21404 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021405 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021406 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021407 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021408
21409 l = varp->vval.v_list;
21410 if (l == NULL)
21411 return NULL;
21412
21413 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021414 pos.lnum = list_find_nr(l, 0L, &error);
21415 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021416 return NULL; /* invalid line number */
21417
21418 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021419 pos.col = list_find_nr(l, 1L, &error);
21420 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021421 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021422 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021423
21424 /* We accept "$" for the column number: last column. */
21425 li = list_find(l, 1L);
21426 if (li != NULL && li->li_tv.v_type == VAR_STRING
21427 && li->li_tv.vval.v_string != NULL
21428 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21429 pos.col = len + 1;
21430
Bram Moolenaara5525202006-03-02 22:52:09 +000021431 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021432 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021433 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021434 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021435
Bram Moolenaara5525202006-03-02 22:52:09 +000021436#ifdef FEAT_VIRTUALEDIT
21437 /* Get the virtual offset. Defaults to zero. */
21438 pos.coladd = list_find_nr(l, 2L, &error);
21439 if (error)
21440 pos.coladd = 0;
21441#endif
21442
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021443 return &pos;
21444 }
21445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021446 name = get_tv_string_chk(varp);
21447 if (name == NULL)
21448 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021449 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021451 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21452 {
21453 if (VIsual_active)
21454 return &VIsual;
21455 return &curwin->w_cursor;
21456 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021457 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021459 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21461 return NULL;
21462 return pp;
21463 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021464
21465#ifdef FEAT_VIRTUALEDIT
21466 pos.coladd = 0;
21467#endif
21468
Bram Moolenaar477933c2007-07-17 14:32:23 +000021469 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021470 {
21471 pos.col = 0;
21472 if (name[1] == '0') /* "w0": first visible line */
21473 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021474 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021475 pos.lnum = curwin->w_topline;
21476 return &pos;
21477 }
21478 else if (name[1] == '$') /* "w$": last visible line */
21479 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021480 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021481 pos.lnum = curwin->w_botline - 1;
21482 return &pos;
21483 }
21484 }
21485 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021487 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 {
21489 pos.lnum = curbuf->b_ml.ml_line_count;
21490 pos.col = 0;
21491 }
21492 else
21493 {
21494 pos.lnum = curwin->w_cursor.lnum;
21495 pos.col = (colnr_T)STRLEN(ml_get_curline());
21496 }
21497 return &pos;
21498 }
21499 return NULL;
21500}
21501
21502/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021503 * Convert list in "arg" into a position and optional file number.
21504 * When "fnump" is NULL there is no file number, only 3 items.
21505 * Note that the column is passed on as-is, the caller may want to decrement
21506 * it to use 1 for the first column.
21507 * Return FAIL when conversion is not possible, doesn't check the position for
21508 * validity.
21509 */
21510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021511list2fpos(
21512 typval_T *arg,
21513 pos_T *posp,
21514 int *fnump,
21515 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021516{
21517 list_T *l = arg->vval.v_list;
21518 long i = 0;
21519 long n;
21520
Bram Moolenaar493c1782014-05-28 14:34:46 +020021521 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21522 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021523 if (arg->v_type != VAR_LIST
21524 || l == NULL
21525 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021526 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021527 return FAIL;
21528
21529 if (fnump != NULL)
21530 {
21531 n = list_find_nr(l, i++, NULL); /* fnum */
21532 if (n < 0)
21533 return FAIL;
21534 if (n == 0)
21535 n = curbuf->b_fnum; /* current buffer */
21536 *fnump = n;
21537 }
21538
21539 n = list_find_nr(l, i++, NULL); /* lnum */
21540 if (n < 0)
21541 return FAIL;
21542 posp->lnum = n;
21543
21544 n = list_find_nr(l, i++, NULL); /* col */
21545 if (n < 0)
21546 return FAIL;
21547 posp->col = n;
21548
21549#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021550 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021551 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021552 posp->coladd = 0;
21553 else
21554 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021555#endif
21556
Bram Moolenaar493c1782014-05-28 14:34:46 +020021557 if (curswantp != NULL)
21558 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21559
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021560 return OK;
21561}
21562
21563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 * Get the length of an environment variable name.
21565 * Advance "arg" to the first character after the name.
21566 * Return 0 for error.
21567 */
21568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021569get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570{
21571 char_u *p;
21572 int len;
21573
21574 for (p = *arg; vim_isIDc(*p); ++p)
21575 ;
21576 if (p == *arg) /* no name found */
21577 return 0;
21578
21579 len = (int)(p - *arg);
21580 *arg = p;
21581 return len;
21582}
21583
21584/*
21585 * Get the length of the name of a function or internal variable.
21586 * "arg" is advanced to the first non-white character after the name.
21587 * Return 0 if something is wrong.
21588 */
21589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021590get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591{
21592 char_u *p;
21593 int len;
21594
21595 /* Find the end of the name. */
21596 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021597 {
21598 if (*p == ':')
21599 {
21600 /* "s:" is start of "s:var", but "n:" is not and can be used in
21601 * slice "[n:]". Also "xx:" is not a namespace. */
21602 len = (int)(p - *arg);
21603 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21604 || len > 1)
21605 break;
21606 }
21607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 if (p == *arg) /* no name found */
21609 return 0;
21610
21611 len = (int)(p - *arg);
21612 *arg = skipwhite(p);
21613
21614 return len;
21615}
21616
21617/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021618 * Get the length of the name of a variable or function.
21619 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021621 * Return -1 if curly braces expansion failed.
21622 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 * If the name contains 'magic' {}'s, expand them and return the
21624 * expanded name in an allocated string via 'alias' - caller must free.
21625 */
21626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021627get_name_len(
21628 char_u **arg,
21629 char_u **alias,
21630 int evaluate,
21631 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
21633 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 char_u *p;
21635 char_u *expr_start;
21636 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637
21638 *alias = NULL; /* default to no alias */
21639
21640 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21641 && (*arg)[2] == (int)KE_SNR)
21642 {
21643 /* hard coded <SNR>, already translated */
21644 *arg += 3;
21645 return get_id_len(arg) + 3;
21646 }
21647 len = eval_fname_script(*arg);
21648 if (len > 0)
21649 {
21650 /* literal "<SID>", "s:" or "<SNR>" */
21651 *arg += len;
21652 }
21653
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021655 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021657 p = find_name_end(*arg, &expr_start, &expr_end,
21658 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 if (expr_start != NULL)
21660 {
21661 char_u *temp_string;
21662
21663 if (!evaluate)
21664 {
21665 len += (int)(p - *arg);
21666 *arg = skipwhite(p);
21667 return len;
21668 }
21669
21670 /*
21671 * Include any <SID> etc in the expanded string:
21672 * Thus the -len here.
21673 */
21674 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21675 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 *alias = temp_string;
21678 *arg = skipwhite(p);
21679 return (int)STRLEN(temp_string);
21680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681
21682 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021683 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 EMSG2(_(e_invexpr2), *arg);
21685
21686 return len;
21687}
21688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021689/*
21690 * Find the end of a variable or function name, taking care of magic braces.
21691 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21692 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021693 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021694 * Return a pointer to just after the name. Equal to "arg" if there is no
21695 * valid name.
21696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021698find_name_end(
21699 char_u *arg,
21700 char_u **expr_start,
21701 char_u **expr_end,
21702 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021704 int mb_nest = 0;
21705 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021707 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021709 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021711 *expr_start = NULL;
21712 *expr_end = NULL;
21713 }
21714
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021715 /* Quick check for valid starting character. */
21716 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21717 return arg;
21718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021719 for (p = arg; *p != NUL
21720 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021721 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021722 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021723 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021724 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021726 if (*p == '\'')
21727 {
21728 /* skip over 'string' to avoid counting [ and ] inside it. */
21729 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21730 ;
21731 if (*p == NUL)
21732 break;
21733 }
21734 else if (*p == '"')
21735 {
21736 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21737 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21738 if (*p == '\\' && p[1] != NUL)
21739 ++p;
21740 if (*p == NUL)
21741 break;
21742 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021743 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21744 {
21745 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021746 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021747 len = (int)(p - arg);
21748 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021749 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021750 break;
21751 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021755 if (*p == '[')
21756 ++br_nest;
21757 else if (*p == ']')
21758 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021763 if (*p == '{')
21764 {
21765 mb_nest++;
21766 if (expr_start != NULL && *expr_start == NULL)
21767 *expr_start = p;
21768 }
21769 else if (*p == '}')
21770 {
21771 mb_nest--;
21772 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21773 *expr_end = p;
21774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 }
21777
21778 return p;
21779}
21780
21781/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021782 * Expands out the 'magic' {}'s in a variable/function name.
21783 * Note that this can call itself recursively, to deal with
21784 * constructs like foo{bar}{baz}{bam}
21785 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21786 * "in_start" ^
21787 * "expr_start" ^
21788 * "expr_end" ^
21789 * "in_end" ^
21790 *
21791 * Returns a new allocated string, which the caller must free.
21792 * Returns NULL for failure.
21793 */
21794 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021795make_expanded_name(
21796 char_u *in_start,
21797 char_u *expr_start,
21798 char_u *expr_end,
21799 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021800{
21801 char_u c1;
21802 char_u *retval = NULL;
21803 char_u *temp_result;
21804 char_u *nextcmd = NULL;
21805
21806 if (expr_end == NULL || in_end == NULL)
21807 return NULL;
21808 *expr_start = NUL;
21809 *expr_end = NUL;
21810 c1 = *in_end;
21811 *in_end = NUL;
21812
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021813 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021814 if (temp_result != NULL && nextcmd == NULL)
21815 {
21816 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21817 + (in_end - expr_end) + 1));
21818 if (retval != NULL)
21819 {
21820 STRCPY(retval, in_start);
21821 STRCAT(retval, temp_result);
21822 STRCAT(retval, expr_end + 1);
21823 }
21824 }
21825 vim_free(temp_result);
21826
21827 *in_end = c1; /* put char back for error messages */
21828 *expr_start = '{';
21829 *expr_end = '}';
21830
21831 if (retval != NULL)
21832 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021833 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021834 if (expr_start != NULL)
21835 {
21836 /* Further expansion! */
21837 temp_result = make_expanded_name(retval, expr_start,
21838 expr_end, temp_result);
21839 vim_free(retval);
21840 retval = temp_result;
21841 }
21842 }
21843
21844 return retval;
21845}
21846
21847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021849 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 */
21851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021852eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021854 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21855}
21856
21857/*
21858 * Return TRUE if character "c" can be used as the first character in a
21859 * variable or function name (excluding '{' and '}').
21860 */
21861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021862eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021863{
21864 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865}
21866
21867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 * Set number v: variable to "val".
21869 */
21870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021871set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021873 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874}
21875
21876/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021877 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 */
21879 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021880get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021882 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883}
21884
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021885/*
21886 * Get string v: variable value. Uses a static buffer, can only be used once.
21887 */
21888 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021889get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021890{
21891 return get_tv_string(&vimvars[idx].vv_tv);
21892}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021893
Bram Moolenaar071d4272004-06-13 20:20:40 +000021894/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021895 * Get List v: variable value. Caller must take care of reference count when
21896 * needed.
21897 */
21898 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021899get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021900{
21901 return vimvars[idx].vv_list;
21902}
21903
21904/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021905 * Set v:char to character "c".
21906 */
21907 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021908set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021909{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021910 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021911
21912#ifdef FEAT_MBYTE
21913 if (has_mbyte)
21914 buf[(*mb_char2bytes)(c, buf)] = NUL;
21915 else
21916#endif
21917 {
21918 buf[0] = c;
21919 buf[1] = NUL;
21920 }
21921 set_vim_var_string(VV_CHAR, buf, -1);
21922}
21923
21924/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021925 * Set v:count to "count" and v:count1 to "count1".
21926 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 */
21928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021929set_vcount(
21930 long count,
21931 long count1,
21932 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021934 if (set_prevcount)
21935 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 vimvars[VV_COUNT].vv_nr = count;
21937 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
21941 * Set string v: variable to a copy of "val".
21942 */
21943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021944set_vim_var_string(
21945 int idx,
21946 char_u *val,
21947 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948{
Bram Moolenaara542c682016-01-31 16:28:04 +010021949 clear_tv(&vimvars[idx].vv_di.di_tv);
21950 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021952 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021954 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021956 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957}
21958
21959/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021960 * Set List v: variable to "val".
21961 */
21962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021963set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021964{
Bram Moolenaara542c682016-01-31 16:28:04 +010021965 clear_tv(&vimvars[idx].vv_di.di_tv);
21966 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021967 vimvars[idx].vv_list = val;
21968 if (val != NULL)
21969 ++val->lv_refcount;
21970}
21971
21972/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021973 * Set Dictionary v: variable to "val".
21974 */
21975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021976set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021977{
21978 int todo;
21979 hashitem_T *hi;
21980
Bram Moolenaara542c682016-01-31 16:28:04 +010021981 clear_tv(&vimvars[idx].vv_di.di_tv);
21982 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021983 vimvars[idx].vv_dict = val;
21984 if (val != NULL)
21985 {
21986 ++val->dv_refcount;
21987
21988 /* Set readonly */
21989 todo = (int)val->dv_hashtab.ht_used;
21990 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21991 {
21992 if (HASHITEM_EMPTY(hi))
21993 continue;
21994 --todo;
21995 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21996 }
21997 }
21998}
21999
22000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 * Set v:register if needed.
22002 */
22003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022004set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005{
22006 char_u regname;
22007
22008 if (c == 0 || c == ' ')
22009 regname = '"';
22010 else
22011 regname = c;
22012 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022013 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 set_vim_var_string(VV_REG, &regname, 1);
22015}
22016
22017/*
22018 * Get or set v:exception. If "oldval" == NULL, return the current value.
22019 * Otherwise, restore the value to "oldval" and return NULL.
22020 * Must always be called in pairs to save and restore v:exception! Does not
22021 * take care of memory allocations.
22022 */
22023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025{
22026 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028
Bram Moolenaare9a41262005-01-15 22:18:47 +000022029 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022030 return NULL;
22031}
22032
22033/*
22034 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22035 * Otherwise, restore the value to "oldval" and return NULL.
22036 * Must always be called in pairs to save and restore v:throwpoint! Does not
22037 * take care of memory allocations.
22038 */
22039 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022040v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041{
22042 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022043 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044
Bram Moolenaare9a41262005-01-15 22:18:47 +000022045 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 return NULL;
22047}
22048
22049#if defined(FEAT_AUTOCMD) || defined(PROTO)
22050/*
22051 * Set v:cmdarg.
22052 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22053 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22054 * Must always be called in pairs!
22055 */
22056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022057set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058{
22059 char_u *oldval;
22060 char_u *newval;
22061 unsigned len;
22062
Bram Moolenaare9a41262005-01-15 22:18:47 +000022063 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022064 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022066 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022067 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022068 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069 }
22070
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022071 if (eap->force_bin == FORCE_BIN)
22072 len = 6;
22073 else if (eap->force_bin == FORCE_NOBIN)
22074 len = 8;
22075 else
22076 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022077
22078 if (eap->read_edit)
22079 len += 7;
22080
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022081 if (eap->force_ff != 0)
22082 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22083# ifdef FEAT_MBYTE
22084 if (eap->force_enc != 0)
22085 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022086 if (eap->bad_char != 0)
22087 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022088# endif
22089
22090 newval = alloc(len + 1);
22091 if (newval == NULL)
22092 return NULL;
22093
22094 if (eap->force_bin == FORCE_BIN)
22095 sprintf((char *)newval, " ++bin");
22096 else if (eap->force_bin == FORCE_NOBIN)
22097 sprintf((char *)newval, " ++nobin");
22098 else
22099 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022100
22101 if (eap->read_edit)
22102 STRCAT(newval, " ++edit");
22103
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022104 if (eap->force_ff != 0)
22105 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22106 eap->cmd + eap->force_ff);
22107# ifdef FEAT_MBYTE
22108 if (eap->force_enc != 0)
22109 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22110 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022111 if (eap->bad_char == BAD_KEEP)
22112 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22113 else if (eap->bad_char == BAD_DROP)
22114 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22115 else if (eap->bad_char != 0)
22116 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022117# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022118 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022119 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120}
22121#endif
22122
22123/*
22124 * Get the value of internal variable "name".
22125 * Return OK or FAIL.
22126 */
22127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022128get_var_tv(
22129 char_u *name,
22130 int len, /* length of "name" */
22131 typval_T *rettv, /* NULL when only checking existence */
22132 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22133 int verbose, /* may give error message */
22134 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135{
22136 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 typval_T *tv = NULL;
22138 typval_T atv;
22139 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141
22142 /* truncate the name, so that we can use strcmp() */
22143 cc = name[len];
22144 name[len] = NUL;
22145
22146 /*
22147 * Check for "b:changedtick".
22148 */
22149 if (STRCMP(name, "b:changedtick") == 0)
22150 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022151 atv.v_type = VAR_NUMBER;
22152 atv.vval.v_number = curbuf->b_changedtick;
22153 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 }
22155
22156 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 * Check for user-defined variables.
22158 */
22159 else
22160 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022161 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022165 if (dip != NULL)
22166 *dip = v;
22167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 }
22169
Bram Moolenaare9a41262005-01-15 22:18:47 +000022170 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022172 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 ret = FAIL;
22175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022176 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022177 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178
22179 name[len] = cc;
22180
22181 return ret;
22182}
22183
22184/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022185 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22186 * Also handle function call with Funcref variable: func(expr)
22187 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22188 */
22189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190handle_subscript(
22191 char_u **arg,
22192 typval_T *rettv,
22193 int evaluate, /* do more than finding the end */
22194 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022195{
22196 int ret = OK;
22197 dict_T *selfdict = NULL;
22198 char_u *s;
22199 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022200 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022201
22202 while (ret == OK
22203 && (**arg == '['
22204 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022205 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022206 && !vim_iswhite(*(*arg - 1)))
22207 {
22208 if (**arg == '(')
22209 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022210 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022211 if (evaluate)
22212 {
22213 functv = *rettv;
22214 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022215
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022216 /* Invoke the function. Recursive! */
22217 s = functv.vval.v_string;
22218 }
22219 else
22220 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022221 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022222 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22223 &len, evaluate, selfdict);
22224
22225 /* Clear the funcref afterwards, so that deleting it while
22226 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022227 if (evaluate)
22228 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022229
22230 /* Stop the expression evaluation when immediately aborting on
22231 * error, or when an interrupt occurred or an exception was thrown
22232 * but not caught. */
22233 if (aborting())
22234 {
22235 if (ret == OK)
22236 clear_tv(rettv);
22237 ret = FAIL;
22238 }
22239 dict_unref(selfdict);
22240 selfdict = NULL;
22241 }
22242 else /* **arg == '[' || **arg == '.' */
22243 {
22244 dict_unref(selfdict);
22245 if (rettv->v_type == VAR_DICT)
22246 {
22247 selfdict = rettv->vval.v_dict;
22248 if (selfdict != NULL)
22249 ++selfdict->dv_refcount;
22250 }
22251 else
22252 selfdict = NULL;
22253 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22254 {
22255 clear_tv(rettv);
22256 ret = FAIL;
22257 }
22258 }
22259 }
22260 dict_unref(selfdict);
22261 return ret;
22262}
22263
22264/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022265 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022266 * value).
22267 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022268 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022269alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022270{
Bram Moolenaar33570922005-01-25 22:26:29 +000022271 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272}
22273
22274/*
22275 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * The string "s" must have been allocated, it is consumed.
22277 * Return NULL for out of memory, the variable otherwise.
22278 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022280alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281{
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022284 rettv = alloc_tv();
22285 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022287 rettv->v_type = VAR_STRING;
22288 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 else
22291 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022292 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293}
22294
22295/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022296 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022299free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300{
22301 if (varp != NULL)
22302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022303 switch (varp->v_type)
22304 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022305 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022306 func_unref(varp->vval.v_string);
22307 /*FALLTHROUGH*/
22308 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 vim_free(varp->vval.v_string);
22310 break;
22311 case VAR_LIST:
22312 list_unref(varp->vval.v_list);
22313 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022314 case VAR_DICT:
22315 dict_unref(varp->vval.v_dict);
22316 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022317 case VAR_JOB:
22318#ifdef FEAT_JOB
22319 job_unref(varp->vval.v_job);
22320 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022321#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022322 case VAR_CHANNEL:
22323#ifdef FEAT_CHANNEL
22324 channel_unref(varp->vval.v_channel);
22325 break;
22326#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022327 case VAR_NUMBER:
22328 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022329 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022330 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022331 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333 vim_free(varp);
22334 }
22335}
22336
22337/*
22338 * Free the memory for a variable value and set the value to NULL or 0.
22339 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022341clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342{
22343 if (varp != NULL)
22344 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022345 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022347 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 func_unref(varp->vval.v_string);
22349 /*FALLTHROUGH*/
22350 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351 vim_free(varp->vval.v_string);
22352 varp->vval.v_string = NULL;
22353 break;
22354 case VAR_LIST:
22355 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022356 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022358 case VAR_DICT:
22359 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022360 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022361 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022362 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022363 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022364 varp->vval.v_number = 0;
22365 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022366 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022367#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022368 varp->vval.v_float = 0.0;
22369 break;
22370#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022371 case VAR_JOB:
22372#ifdef FEAT_JOB
22373 job_unref(varp->vval.v_job);
22374 varp->vval.v_job = NULL;
22375#endif
22376 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022377 case VAR_CHANNEL:
22378#ifdef FEAT_CHANNEL
22379 channel_unref(varp->vval.v_channel);
22380 varp->vval.v_channel = NULL;
22381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022382 case VAR_UNKNOWN:
22383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022385 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 }
22387}
22388
22389/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022390 * Set the value of a variable to NULL without freeing items.
22391 */
22392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022393init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022394{
22395 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022396 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022397}
22398
22399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 * Get the number value of a variable.
22401 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022402 * For incompatible types, return 0.
22403 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22404 * caller of incompatible types: it sets *denote to TRUE if "denote"
22405 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 */
22407 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022408get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022410 int error = FALSE;
22411
22412 return get_tv_number_chk(varp, &error); /* return 0L on error */
22413}
22414
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022415 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022416get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022417{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022418 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022420 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022423 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022424 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022425#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022426 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022427 break;
22428#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022429 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022430 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 break;
22432 case VAR_STRING:
22433 if (varp->vval.v_string != NULL)
22434 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022435 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022436 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022437 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022438 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022439 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022440 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022441 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022442 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022443 case VAR_SPECIAL:
22444 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22445 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022446 case VAR_JOB:
22447#ifdef FEAT_JOB
22448 EMSG(_("E910: Using a Job as a Number"));
22449 break;
22450#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022451 case VAR_CHANNEL:
22452#ifdef FEAT_CHANNEL
22453 EMSG(_("E913: Using a Channel as a Number"));
22454 break;
22455#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022456 case VAR_UNKNOWN:
22457 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022460 if (denote == NULL) /* useful for values that must be unsigned */
22461 n = -1;
22462 else
22463 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022464 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465}
22466
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022467#ifdef FEAT_FLOAT
22468 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022469get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022470{
22471 switch (varp->v_type)
22472 {
22473 case VAR_NUMBER:
22474 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022475 case VAR_FLOAT:
22476 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022477 case VAR_FUNC:
22478 EMSG(_("E891: Using a Funcref as a Float"));
22479 break;
22480 case VAR_STRING:
22481 EMSG(_("E892: Using a String as a Float"));
22482 break;
22483 case VAR_LIST:
22484 EMSG(_("E893: Using a List as a Float"));
22485 break;
22486 case VAR_DICT:
22487 EMSG(_("E894: Using a Dictionary as a Float"));
22488 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022489 case VAR_SPECIAL:
22490 EMSG(_("E907: Using a special value as a Float"));
22491 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022492 case VAR_JOB:
22493# ifdef FEAT_JOB
22494 EMSG(_("E911: Using a Job as a Float"));
22495 break;
22496# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022497 case VAR_CHANNEL:
22498# ifdef FEAT_CHANNEL
22499 EMSG(_("E914: Using a Channel as a Float"));
22500 break;
22501# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022502 case VAR_UNKNOWN:
22503 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022504 break;
22505 }
22506 return 0;
22507}
22508#endif
22509
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022511 * Get the lnum from the first argument.
22512 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022513 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514 */
22515 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022516get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517{
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 linenr_T lnum;
22520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022521 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 if (lnum == 0) /* no valid number, try using line() */
22523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022524 rettv.v_type = VAR_NUMBER;
22525 f_line(argvars, &rettv);
22526 lnum = rettv.vval.v_number;
22527 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 }
22529 return lnum;
22530}
22531
22532/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022533 * Get the lnum from the first argument.
22534 * Also accepts "$", then "buf" is used.
22535 * Returns 0 on error.
22536 */
22537 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022538get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022539{
22540 if (argvars[0].v_type == VAR_STRING
22541 && argvars[0].vval.v_string != NULL
22542 && argvars[0].vval.v_string[0] == '$'
22543 && buf != NULL)
22544 return buf->b_ml.ml_line_count;
22545 return get_tv_number_chk(&argvars[0], NULL);
22546}
22547
22548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 * Get the string value of a variable.
22550 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022551 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22552 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 * If the String variable has never been set, return an empty string.
22554 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022555 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22556 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 */
22558 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022559get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560{
22561 static char_u mybuf[NUMBUFLEN];
22562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022563 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564}
22565
22566 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022569 char_u *res = get_tv_string_buf_chk(varp, buf);
22570
22571 return res != NULL ? res : (char_u *)"";
22572}
22573
Bram Moolenaar7d647822014-04-05 21:28:56 +020022574/*
22575 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22576 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022577 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022578get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022579{
22580 static char_u mybuf[NUMBUFLEN];
22581
22582 return get_tv_string_buf_chk(varp, mybuf);
22583}
22584
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022585 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022586get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022587{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022590 case VAR_NUMBER:
22591 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22592 return buf;
22593 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022594 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022595 break;
22596 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022597 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022598 break;
22599 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022600 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022601 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022602 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022603#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022604 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022605 break;
22606#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022607 case VAR_STRING:
22608 if (varp->vval.v_string != NULL)
22609 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022610 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022611 case VAR_SPECIAL:
22612 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22613 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022614 case VAR_JOB:
22615#ifdef FEAT_JOB
22616 {
22617 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022618 char *status;
22619
22620 if (job == NULL)
22621 return (char_u *)"no process";
22622 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022623 : job->jv_status == JOB_ENDED ? "dead"
22624 : "run";
22625# ifdef UNIX
22626 vim_snprintf((char *)buf, NUMBUFLEN,
22627 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022628# elif defined(WIN32)
22629 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022630 "process %ld %s",
22631 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022632 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022633# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022634 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022635 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22636# endif
22637 return buf;
22638 }
22639#endif
22640 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022641 case VAR_CHANNEL:
22642#ifdef FEAT_CHANNEL
22643 {
22644 channel_T *channel = varp->vval.v_channel;
22645 char *status = channel_status(channel);
22646
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022647 if (channel == NULL)
22648 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22649 else
22650 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022651 "channel %d %s", channel->ch_id, status);
22652 return buf;
22653 }
22654#endif
22655 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022656 case VAR_UNKNOWN:
22657 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022658 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022660 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661}
22662
22663/*
22664 * Find variable "name" in the list of variables.
22665 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022667 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022668 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022671find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022673 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675
Bram Moolenaara7043832005-01-21 11:56:39 +000022676 ht = find_var_ht(name, &varname);
22677 if (htp != NULL)
22678 *htp = ht;
22679 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022681 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682}
22683
22684/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022685 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022686 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022689find_var_in_ht(
22690 hashtab_T *ht,
22691 int htname,
22692 char_u *varname,
22693 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022694{
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 hashitem_T *hi;
22696
22697 if (*varname == NUL)
22698 {
22699 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022700 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022701 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022702 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 case 'g': return &globvars_var;
22704 case 'v': return &vimvars_var;
22705 case 'b': return &curbuf->b_bufvar;
22706 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022707#ifdef FEAT_WINDOWS
22708 case 't': return &curtab->tp_winvar;
22709#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022710 case 'l': return current_funccal == NULL
22711 ? NULL : &current_funccal->l_vars_var;
22712 case 'a': return current_funccal == NULL
22713 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 }
22715 return NULL;
22716 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022717
22718 hi = hash_find(ht, varname);
22719 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022720 {
22721 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022722 * worked find the variable again. Don't auto-load a script if it was
22723 * loaded already, otherwise it would be loaded every time when
22724 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022725 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022726 {
22727 /* Note: script_autoload() may make "hi" invalid. It must either
22728 * be obtained again or not used. */
22729 if (!script_autoload(varname, FALSE) || aborting())
22730 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022731 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022732 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022733 if (HASHITEM_EMPTY(hi))
22734 return NULL;
22735 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022736 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022737}
22738
22739/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022741 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022742 * Set "varname" to the start of name without ':'.
22743 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022745find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022747 hashitem_T *hi;
22748
Bram Moolenaar73627d02015-08-11 15:46:09 +020022749 if (name[0] == NUL)
22750 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 if (name[1] != ':')
22752 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022753 /* The name must not start with a colon or #. */
22754 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 return NULL;
22756 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022757
22758 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022759 hi = hash_find(&compat_hashtab, name);
22760 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022761 return &compat_hashtab;
22762
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022765 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 }
22767 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022768 if (*name == 'g') /* global variable */
22769 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022770 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22771 */
22772 if (vim_strchr(name + 2, ':') != NULL
22773 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022774 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022776 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022778 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022779#ifdef FEAT_WINDOWS
22780 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022781 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022782#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 if (*name == 'v') /* v: variable */
22784 return &vimvarht;
22785 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022786 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022788 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 if (*name == 's' /* script variable */
22790 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22791 return &SCRIPT_VARS(current_SID);
22792 return NULL;
22793}
22794
22795/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022796 * Get function call environment based on bactrace debug level
22797 */
22798 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022800{
22801 int i;
22802 funccall_T *funccal;
22803 funccall_T *temp_funccal;
22804
22805 funccal = current_funccal;
22806 if (debug_backtrace_level > 0)
22807 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022808 for (i = 0; i < debug_backtrace_level; i++)
22809 {
22810 temp_funccal = funccal->caller;
22811 if (temp_funccal)
22812 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022813 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022814 /* backtrace level overflow. reset to max */
22815 debug_backtrace_level = i;
22816 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022817 }
22818 return funccal;
22819}
22820
22821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022823 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 * Returns NULL when it doesn't exist.
22825 */
22826 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022827get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828{
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022831 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 if (v == NULL)
22833 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835}
22836
22837/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 * sourcing this script and when executing functions defined in the script.
22840 */
22841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022842new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843{
Bram Moolenaara7043832005-01-21 11:56:39 +000022844 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 hashtab_T *ht;
22846 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022847
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22849 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022850 /* Re-allocating ga_data means that an ht_array pointing to
22851 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022853 for (i = 1; i <= ga_scripts.ga_len; ++i)
22854 {
22855 ht = &SCRIPT_VARS(i);
22856 if (ht->ht_mask == HT_INIT_SIZE - 1)
22857 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022858 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022860 }
22861
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 while (ga_scripts.ga_len < id)
22863 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022864 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022865 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022866 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 }
22869 }
22870}
22871
22872/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22874 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 */
22876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022877init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878{
Bram Moolenaar33570922005-01-25 22:26:29 +000022879 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022880 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022881 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022882 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022883 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022884 dict_var->di_tv.vval.v_dict = dict;
22885 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022886 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022887 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22888 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889}
22890
22891/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022892 * Unreference a dictionary initialized by init_var_dict().
22893 */
22894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022895unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022896{
22897 /* Now the dict needs to be freed if no one else is using it, go back to
22898 * normal reference counting. */
22899 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22900 dict_unref(dict);
22901}
22902
22903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 * Frees all allocated variables and the value they contain.
22906 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 */
22908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022909vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022910{
22911 vars_clear_ext(ht, TRUE);
22912}
22913
22914/*
22915 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22916 */
22917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022918vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919{
Bram Moolenaara7043832005-01-21 11:56:39 +000022920 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 hashitem_T *hi;
22922 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923
Bram Moolenaar33570922005-01-25 22:26:29 +000022924 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022925 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022926 for (hi = ht->ht_array; todo > 0; ++hi)
22927 {
22928 if (!HASHITEM_EMPTY(hi))
22929 {
22930 --todo;
22931
Bram Moolenaar33570922005-01-25 22:26:29 +000022932 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022933 * ht_array might change then. hash_clear() takes care of it
22934 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 v = HI2DI(hi);
22936 if (free_val)
22937 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022938 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022940 }
22941 }
22942 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022943 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944}
22945
Bram Moolenaara7043832005-01-21 11:56:39 +000022946/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022947 * Delete a variable from hashtab "ht" at item "hi".
22948 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022951delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952{
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022954
22955 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 clear_tv(&di->di_tv);
22957 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958}
22959
22960/*
22961 * List the value of one internal variable.
22962 */
22963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022964list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022966 char_u *tofree;
22967 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022968 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022969
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022970 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022971 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022972 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022973 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974}
22975
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022977list_one_var_a(
22978 char_u *prefix,
22979 char_u *name,
22980 int type,
22981 char_u *string,
22982 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983{
Bram Moolenaar31859182007-08-14 20:41:13 +000022984 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22985 msg_start();
22986 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 if (name != NULL) /* "a:" vars don't have a name stored */
22988 msg_puts(name);
22989 msg_putchar(' ');
22990 msg_advance(22);
22991 if (type == VAR_NUMBER)
22992 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022993 else if (type == VAR_FUNC)
22994 msg_putchar('*');
22995 else if (type == VAR_LIST)
22996 {
22997 msg_putchar('[');
22998 if (*string == '[')
22999 ++string;
23000 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023001 else if (type == VAR_DICT)
23002 {
23003 msg_putchar('{');
23004 if (*string == '{')
23005 ++string;
23006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 else
23008 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023009
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023011
23012 if (type == VAR_FUNC)
23013 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023014 if (*first)
23015 {
23016 msg_clr_eos();
23017 *first = FALSE;
23018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019}
23020
23021/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023022 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 * If the variable already exists, the value is updated.
23024 * Otherwise the variable is created.
23025 */
23026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023027set_var(
23028 char_u *name,
23029 typval_T *tv,
23030 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031{
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023034 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023036 ht = find_var_ht(name, &varname);
23037 if (ht == NULL || *varname == NUL)
23038 {
23039 EMSG2(_(e_illvar), name);
23040 return;
23041 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023042 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023043
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023044 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
23045 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023046
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023049 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023050 if (var_check_ro(v->di_flags, name, FALSE)
23051 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023052 return;
23053 if (v->di_tv.v_type != tv->v_type
23054 && !((v->di_tv.v_type == VAR_STRING
23055 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023056 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023057 || tv->v_type == VAR_NUMBER))
23058#ifdef FEAT_FLOAT
23059 && !((v->di_tv.v_type == VAR_NUMBER
23060 || v->di_tv.v_type == VAR_FLOAT)
23061 && (tv->v_type == VAR_NUMBER
23062 || tv->v_type == VAR_FLOAT))
23063#endif
23064 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023065 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000023066 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023067 return;
23068 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023069
23070 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023071 * Handle setting internal v: variables separately where needed to
23072 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023073 */
23074 if (ht == &vimvarht)
23075 {
23076 if (v->di_tv.v_type == VAR_STRING)
23077 {
23078 vim_free(v->di_tv.vval.v_string);
23079 if (copy || tv->v_type != VAR_STRING)
23080 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23081 else
23082 {
23083 /* Take over the string to avoid an extra alloc/free. */
23084 v->di_tv.vval.v_string = tv->vval.v_string;
23085 tv->vval.v_string = NULL;
23086 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023087 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023088 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023089 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023090 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023091 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023092 if (STRCMP(varname, "searchforward") == 0)
23093 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023094#ifdef FEAT_SEARCH_EXTRA
23095 else if (STRCMP(varname, "hlsearch") == 0)
23096 {
23097 no_hlsearch = !v->di_tv.vval.v_number;
23098 redraw_all_later(SOME_VALID);
23099 }
23100#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023101 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023102 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023103 else if (v->di_tv.v_type != tv->v_type)
23104 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 }
23106
23107 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 }
23109 else /* add a new variable */
23110 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023111 /* Can't add "v:" variable. */
23112 if (ht == &vimvarht)
23113 {
23114 EMSG2(_(e_illvar), name);
23115 return;
23116 }
23117
Bram Moolenaar92124a32005-06-17 22:03:40 +000023118 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023119 if (!valid_varname(varname))
23120 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023121
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023122 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23123 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023124 if (v == NULL)
23125 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023129 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130 return;
23131 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023132 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023134
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023135 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023136 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023137 else
23138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023140 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023141 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143}
23144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023145/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023146 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 * Also give an error message.
23148 */
23149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023150var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023151{
23152 if (flags & DI_FLAGS_RO)
23153 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023154 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023155 return TRUE;
23156 }
23157 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23158 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023159 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 return TRUE;
23161 }
23162 return FALSE;
23163}
23164
23165/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023166 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23167 * Also give an error message.
23168 */
23169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023170var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023171{
23172 if (flags & DI_FLAGS_FIX)
23173 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023174 EMSG2(_("E795: Cannot delete variable %s"),
23175 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023176 return TRUE;
23177 }
23178 return FALSE;
23179}
23180
23181/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023182 * Check if a funcref is assigned to a valid variable name.
23183 * Return TRUE and give an error if not.
23184 */
23185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023186var_check_func_name(
23187 char_u *name, /* points to start of variable name */
23188 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023189{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023190 /* Allow for w: b: s: and t:. */
23191 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023192 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23193 ? name[2] : name[0]))
23194 {
23195 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23196 name);
23197 return TRUE;
23198 }
23199 /* Don't allow hiding a function. When "v" is not NULL we might be
23200 * assigning another function to the same var, the type is checked
23201 * below. */
23202 if (new_var && function_exists(name))
23203 {
23204 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23205 name);
23206 return TRUE;
23207 }
23208 return FALSE;
23209}
23210
23211/*
23212 * Check if a variable name is valid.
23213 * Return FALSE and give an error if not.
23214 */
23215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023216valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023217{
23218 char_u *p;
23219
23220 for (p = varname; *p != NUL; ++p)
23221 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23222 && *p != AUTOLOAD_CHAR)
23223 {
23224 EMSG2(_(e_illvar), varname);
23225 return FALSE;
23226 }
23227 return TRUE;
23228}
23229
23230/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023231 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023232 * Also give an error message, using "name" or _("name") when use_gettext is
23233 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023234 */
23235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023236tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023237{
23238 if (lock & VAR_LOCKED)
23239 {
23240 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023241 name == NULL ? (char_u *)_("Unknown")
23242 : use_gettext ? (char_u *)_(name)
23243 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023244 return TRUE;
23245 }
23246 if (lock & VAR_FIXED)
23247 {
23248 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023249 name == NULL ? (char_u *)_("Unknown")
23250 : use_gettext ? (char_u *)_(name)
23251 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023252 return TRUE;
23253 }
23254 return FALSE;
23255}
23256
23257/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023259 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023260 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023261 * It is OK for "from" and "to" to point to the same item. This is used to
23262 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023263 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023265copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023267 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023268 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023269 switch (from->v_type)
23270 {
23271 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023272 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023273 to->vval.v_number = from->vval.v_number;
23274 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023275 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023276#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023277 to->vval.v_float = from->vval.v_float;
23278 break;
23279#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023280 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023281#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023282 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023283 if (to->vval.v_job != NULL)
23284 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023285 break;
23286#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023287 case VAR_CHANNEL:
23288#ifdef FEAT_CHANNEL
23289 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023290 if (to->vval.v_channel != NULL)
23291 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023292 break;
23293#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023294 case VAR_STRING:
23295 case VAR_FUNC:
23296 if (from->vval.v_string == NULL)
23297 to->vval.v_string = NULL;
23298 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023300 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023301 if (from->v_type == VAR_FUNC)
23302 func_ref(to->vval.v_string);
23303 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023304 break;
23305 case VAR_LIST:
23306 if (from->vval.v_list == NULL)
23307 to->vval.v_list = NULL;
23308 else
23309 {
23310 to->vval.v_list = from->vval.v_list;
23311 ++to->vval.v_list->lv_refcount;
23312 }
23313 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023314 case VAR_DICT:
23315 if (from->vval.v_dict == NULL)
23316 to->vval.v_dict = NULL;
23317 else
23318 {
23319 to->vval.v_dict = from->vval.v_dict;
23320 ++to->vval.v_dict->dv_refcount;
23321 }
23322 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023323 case VAR_UNKNOWN:
23324 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023325 break;
23326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327}
23328
23329/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023330 * Make a copy of an item.
23331 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023332 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23333 * reference to an already copied list/dict can be used.
23334 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023335 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023337item_copy(
23338 typval_T *from,
23339 typval_T *to,
23340 int deep,
23341 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023342{
23343 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023344 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023345
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023347 {
23348 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023349 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023350 }
23351 ++recurse;
23352
23353 switch (from->v_type)
23354 {
23355 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023356 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023357 case VAR_STRING:
23358 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023359 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023360 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023361 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023362 copy_tv(from, to);
23363 break;
23364 case VAR_LIST:
23365 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023366 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023367 if (from->vval.v_list == NULL)
23368 to->vval.v_list = NULL;
23369 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23370 {
23371 /* use the copy made earlier */
23372 to->vval.v_list = from->vval.v_list->lv_copylist;
23373 ++to->vval.v_list->lv_refcount;
23374 }
23375 else
23376 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23377 if (to->vval.v_list == NULL)
23378 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023379 break;
23380 case VAR_DICT:
23381 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023382 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023383 if (from->vval.v_dict == NULL)
23384 to->vval.v_dict = NULL;
23385 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23386 {
23387 /* use the copy made earlier */
23388 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23389 ++to->vval.v_dict->dv_refcount;
23390 }
23391 else
23392 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23393 if (to->vval.v_dict == NULL)
23394 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023395 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023396 case VAR_UNKNOWN:
23397 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023398 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023399 }
23400 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023401 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023402}
23403
23404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 * ":echo expr1 ..." print each argument separated with a space, add a
23406 * newline at the end.
23407 * ":echon expr1 ..." print each argument plain.
23408 */
23409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023410ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411{
23412 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023413 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023414 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 char_u *p;
23416 int needclr = TRUE;
23417 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419
23420 if (eap->skip)
23421 ++emsg_skip;
23422 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23423 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023424 /* If eval1() causes an error message the text from the command may
23425 * still need to be cleared. E.g., "echo 22,44". */
23426 need_clr_eos = needclr;
23427
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023429 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 {
23431 /*
23432 * Report the invalid expression unless the expression evaluation
23433 * has been cancelled due to an aborting error, an interrupt, or an
23434 * exception.
23435 */
23436 if (!aborting())
23437 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023438 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 break;
23440 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023441 need_clr_eos = FALSE;
23442
Bram Moolenaar071d4272004-06-13 20:20:40 +000023443 if (!eap->skip)
23444 {
23445 if (atstart)
23446 {
23447 atstart = FALSE;
23448 /* Call msg_start() after eval1(), evaluating the expression
23449 * may cause a message to appear. */
23450 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023451 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023452 /* Mark the saved text as finishing the line, so that what
23453 * follows is displayed on a new line when scrolling back
23454 * at the more prompt. */
23455 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023456 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 }
23459 else if (eap->cmdidx == CMD_echo)
23460 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023461 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023462 if (p != NULL)
23463 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023465 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023467 if (*p != TAB && needclr)
23468 {
23469 /* remove any text still there from the command */
23470 msg_clr_eos();
23471 needclr = FALSE;
23472 }
23473 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 }
23475 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023476 {
23477#ifdef FEAT_MBYTE
23478 if (has_mbyte)
23479 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023480 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023481
23482 (void)msg_outtrans_len_attr(p, i, echo_attr);
23483 p += i - 1;
23484 }
23485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023487 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023492 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 arg = skipwhite(arg);
23494 }
23495 eap->nextcmd = check_nextcmd(arg);
23496
23497 if (eap->skip)
23498 --emsg_skip;
23499 else
23500 {
23501 /* remove text that may still be there from the command */
23502 if (needclr)
23503 msg_clr_eos();
23504 if (eap->cmdidx == CMD_echo)
23505 msg_end();
23506 }
23507}
23508
23509/*
23510 * ":echohl {name}".
23511 */
23512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023513ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514{
23515 int id;
23516
23517 id = syn_name2id(eap->arg);
23518 if (id == 0)
23519 echo_attr = 0;
23520 else
23521 echo_attr = syn_id2attr(id);
23522}
23523
23524/*
23525 * ":execute expr1 ..." execute the result of an expression.
23526 * ":echomsg expr1 ..." Print a message
23527 * ":echoerr expr1 ..." Print an error
23528 * Each gets spaces around each argument and a newline at the end for
23529 * echo commands
23530 */
23531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023532ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533{
23534 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023535 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 int ret = OK;
23537 char_u *p;
23538 garray_T ga;
23539 int len;
23540 int save_did_emsg;
23541
23542 ga_init2(&ga, 1, 80);
23543
23544 if (eap->skip)
23545 ++emsg_skip;
23546 while (*arg != NUL && *arg != '|' && *arg != '\n')
23547 {
23548 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023549 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 {
23551 /*
23552 * Report the invalid expression unless the expression evaluation
23553 * has been cancelled due to an aborting error, an interrupt, or an
23554 * exception.
23555 */
23556 if (!aborting())
23557 EMSG2(_(e_invexpr2), p);
23558 ret = FAIL;
23559 break;
23560 }
23561
23562 if (!eap->skip)
23563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023564 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 len = (int)STRLEN(p);
23566 if (ga_grow(&ga, len + 2) == FAIL)
23567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023568 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 ret = FAIL;
23570 break;
23571 }
23572 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 ga.ga_len += len;
23576 }
23577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023578 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 arg = skipwhite(arg);
23580 }
23581
23582 if (ret != FAIL && ga.ga_data != NULL)
23583 {
23584 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023587 out_flush();
23588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 else if (eap->cmdidx == CMD_echoerr)
23590 {
23591 /* We don't want to abort following commands, restore did_emsg. */
23592 save_did_emsg = did_emsg;
23593 EMSG((char_u *)ga.ga_data);
23594 if (!force_abort)
23595 did_emsg = save_did_emsg;
23596 }
23597 else if (eap->cmdidx == CMD_execute)
23598 do_cmdline((char_u *)ga.ga_data,
23599 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23600 }
23601
23602 ga_clear(&ga);
23603
23604 if (eap->skip)
23605 --emsg_skip;
23606
23607 eap->nextcmd = check_nextcmd(arg);
23608}
23609
23610/*
23611 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23612 * "arg" points to the "&" or '+' when called, to "option" when returning.
23613 * Returns NULL when no option name found. Otherwise pointer to the char
23614 * after the option name.
23615 */
23616 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023617find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618{
23619 char_u *p = *arg;
23620
23621 ++p;
23622 if (*p == 'g' && p[1] == ':')
23623 {
23624 *opt_flags = OPT_GLOBAL;
23625 p += 2;
23626 }
23627 else if (*p == 'l' && p[1] == ':')
23628 {
23629 *opt_flags = OPT_LOCAL;
23630 p += 2;
23631 }
23632 else
23633 *opt_flags = 0;
23634
23635 if (!ASCII_ISALPHA(*p))
23636 return NULL;
23637 *arg = p;
23638
23639 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23640 p += 4; /* termcap option */
23641 else
23642 while (ASCII_ISALPHA(*p))
23643 ++p;
23644 return p;
23645}
23646
23647/*
23648 * ":function"
23649 */
23650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023651ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652{
23653 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023654 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 int j;
23656 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023658 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 char_u *name = NULL;
23660 char_u *p;
23661 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023662 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 garray_T newargs;
23664 garray_T newlines;
23665 int varargs = FALSE;
23666 int mustend = FALSE;
23667 int flags = 0;
23668 ufunc_T *fp;
23669 int indent;
23670 int nesting;
23671 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023672 dictitem_T *v;
23673 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023674 static int func_nr = 0; /* number for nameless function */
23675 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023676 hashtab_T *ht;
23677 int todo;
23678 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023679 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680
23681 /*
23682 * ":function" without argument: list functions.
23683 */
23684 if (ends_excmd(*eap->arg))
23685 {
23686 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023687 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023688 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023689 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023690 {
23691 if (!HASHITEM_EMPTY(hi))
23692 {
23693 --todo;
23694 fp = HI2UF(hi);
23695 if (!isdigit(*fp->uf_name))
23696 list_func_head(fp, FALSE);
23697 }
23698 }
23699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 eap->nextcmd = check_nextcmd(eap->arg);
23701 return;
23702 }
23703
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023704 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023705 * ":function /pat": list functions matching pattern.
23706 */
23707 if (*eap->arg == '/')
23708 {
23709 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23710 if (!eap->skip)
23711 {
23712 regmatch_T regmatch;
23713
23714 c = *p;
23715 *p = NUL;
23716 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23717 *p = c;
23718 if (regmatch.regprog != NULL)
23719 {
23720 regmatch.rm_ic = p_ic;
23721
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023722 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023723 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23724 {
23725 if (!HASHITEM_EMPTY(hi))
23726 {
23727 --todo;
23728 fp = HI2UF(hi);
23729 if (!isdigit(*fp->uf_name)
23730 && vim_regexec(&regmatch, fp->uf_name, 0))
23731 list_func_head(fp, FALSE);
23732 }
23733 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023734 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023735 }
23736 }
23737 if (*p == '/')
23738 ++p;
23739 eap->nextcmd = check_nextcmd(p);
23740 return;
23741 }
23742
23743 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023744 * Get the function name. There are these situations:
23745 * func normal function name
23746 * "name" == func, "fudi.fd_dict" == NULL
23747 * dict.func new dictionary entry
23748 * "name" == NULL, "fudi.fd_dict" set,
23749 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23750 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023751 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023752 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23753 * dict.func existing dict entry that's not a Funcref
23754 * "name" == NULL, "fudi.fd_dict" set,
23755 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023756 * s:func script-local function name
23757 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023760 name = trans_function_name(&p, eap->skip, 0, &fudi);
23761 paren = (vim_strchr(p, '(') != NULL);
23762 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 {
23764 /*
23765 * Return on an invalid expression in braces, unless the expression
23766 * evaluation has been cancelled due to an aborting error, an
23767 * interrupt, or an exception.
23768 */
23769 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023770 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023771 if (!eap->skip && fudi.fd_newkey != NULL)
23772 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023773 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 else
23777 eap->skip = TRUE;
23778 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023779
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 /* An error in a function call during evaluation of an expression in magic
23781 * braces should not cause the function not to be defined. */
23782 saved_did_emsg = did_emsg;
23783 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784
23785 /*
23786 * ":function func" with only function name: list function.
23787 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023788 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 {
23790 if (!ends_excmd(*skipwhite(p)))
23791 {
23792 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023793 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 }
23795 eap->nextcmd = check_nextcmd(p);
23796 if (eap->nextcmd != NULL)
23797 *p = NUL;
23798 if (!eap->skip && !got_int)
23799 {
23800 fp = find_func(name);
23801 if (fp != NULL)
23802 {
23803 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023804 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023806 if (FUNCLINE(fp, j) == NULL)
23807 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 msg_putchar('\n');
23809 msg_outnum((long)(j + 1));
23810 if (j < 9)
23811 msg_putchar(' ');
23812 if (j < 99)
23813 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023814 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 out_flush(); /* show a line at a time */
23816 ui_breakcheck();
23817 }
23818 if (!got_int)
23819 {
23820 msg_putchar('\n');
23821 msg_puts((char_u *)" endfunction");
23822 }
23823 }
23824 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023825 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023827 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023828 }
23829
23830 /*
23831 * ":function name(arg1, arg2)" Define function.
23832 */
23833 p = skipwhite(p);
23834 if (*p != '(')
23835 {
23836 if (!eap->skip)
23837 {
23838 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 }
23841 /* attempt to continue by skipping some text */
23842 if (vim_strchr(p, '(') != NULL)
23843 p = vim_strchr(p, '(');
23844 }
23845 p = skipwhite(p + 1);
23846
23847 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23848 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23849
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023850 if (!eap->skip)
23851 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023852 /* Check the name of the function. Unless it's a dictionary function
23853 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023854 if (name != NULL)
23855 arg = name;
23856 else
23857 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023858 if (arg != NULL && (fudi.fd_di == NULL
23859 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023860 {
23861 if (*arg == K_SPECIAL)
23862 j = 3;
23863 else
23864 j = 0;
23865 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23866 : eval_isnamec(arg[j])))
23867 ++j;
23868 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023869 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023870 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023871 /* Disallow using the g: dict. */
23872 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23873 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023874 }
23875
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 /*
23877 * Isolate the arguments: "arg1, arg2, ...)"
23878 */
23879 while (*p != ')')
23880 {
23881 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23882 {
23883 varargs = TRUE;
23884 p += 3;
23885 mustend = TRUE;
23886 }
23887 else
23888 {
23889 arg = p;
23890 while (ASCII_ISALNUM(*p) || *p == '_')
23891 ++p;
23892 if (arg == p || isdigit(*arg)
23893 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23894 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23895 {
23896 if (!eap->skip)
23897 EMSG2(_("E125: Illegal argument: %s"), arg);
23898 break;
23899 }
23900 if (ga_grow(&newargs, 1) == FAIL)
23901 goto erret;
23902 c = *p;
23903 *p = NUL;
23904 arg = vim_strsave(arg);
23905 if (arg == NULL)
23906 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023907
23908 /* Check for duplicate argument name. */
23909 for (i = 0; i < newargs.ga_len; ++i)
23910 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23911 {
23912 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023913 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023914 goto erret;
23915 }
23916
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23918 *p = c;
23919 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 if (*p == ',')
23921 ++p;
23922 else
23923 mustend = TRUE;
23924 }
23925 p = skipwhite(p);
23926 if (mustend && *p != ')')
23927 {
23928 if (!eap->skip)
23929 EMSG2(_(e_invarg2), eap->arg);
23930 break;
23931 }
23932 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023933 if (*p != ')')
23934 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 ++p; /* skip the ')' */
23936
Bram Moolenaare9a41262005-01-15 22:18:47 +000023937 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 for (;;)
23939 {
23940 p = skipwhite(p);
23941 if (STRNCMP(p, "range", 5) == 0)
23942 {
23943 flags |= FC_RANGE;
23944 p += 5;
23945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023946 else if (STRNCMP(p, "dict", 4) == 0)
23947 {
23948 flags |= FC_DICT;
23949 p += 4;
23950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 else if (STRNCMP(p, "abort", 5) == 0)
23952 {
23953 flags |= FC_ABORT;
23954 p += 5;
23955 }
23956 else
23957 break;
23958 }
23959
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023960 /* When there is a line break use what follows for the function body.
23961 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23962 if (*p == '\n')
23963 line_arg = p + 1;
23964 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 EMSG(_(e_trailing));
23966
23967 /*
23968 * Read the body of the function, until ":endfunction" is found.
23969 */
23970 if (KeyTyped)
23971 {
23972 /* Check if the function already exists, don't let the user type the
23973 * whole function before telling him it doesn't work! For a script we
23974 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 if (!eap->skip && !eap->forceit)
23976 {
23977 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23978 EMSG(_(e_funcdict));
23979 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023980 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023983 if (!eap->skip && did_emsg)
23984 goto erret;
23985
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 msg_putchar('\n'); /* don't overwrite the function name */
23987 cmdline_row = msg_row;
23988 }
23989
23990 indent = 2;
23991 nesting = 0;
23992 for (;;)
23993 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023994 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023995 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023996 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023997 saved_wait_return = FALSE;
23998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024000 sourcing_lnum_off = sourcing_lnum;
24001
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024002 if (line_arg != NULL)
24003 {
24004 /* Use eap->arg, split up in parts by line breaks. */
24005 theline = line_arg;
24006 p = vim_strchr(theline, '\n');
24007 if (p == NULL)
24008 line_arg += STRLEN(line_arg);
24009 else
24010 {
24011 *p = NUL;
24012 line_arg = p + 1;
24013 }
24014 }
24015 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 theline = getcmdline(':', 0L, indent);
24017 else
24018 theline = eap->getline(':', eap->cookie, indent);
24019 if (KeyTyped)
24020 lines_left = Rows - 1;
24021 if (theline == NULL)
24022 {
24023 EMSG(_("E126: Missing :endfunction"));
24024 goto erret;
24025 }
24026
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024027 /* Detect line continuation: sourcing_lnum increased more than one. */
24028 if (sourcing_lnum > sourcing_lnum_off + 1)
24029 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24030 else
24031 sourcing_lnum_off = 0;
24032
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 if (skip_until != NULL)
24034 {
24035 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24036 * don't check for ":endfunc". */
24037 if (STRCMP(theline, skip_until) == 0)
24038 {
24039 vim_free(skip_until);
24040 skip_until = NULL;
24041 }
24042 }
24043 else
24044 {
24045 /* skip ':' and blanks*/
24046 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24047 ;
24048
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024049 /* Check for "endfunction". */
24050 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024052 if (line_arg == NULL)
24053 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 break;
24055 }
24056
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024057 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 * at "end". */
24059 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24060 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024061 else if (STRNCMP(p, "if", 2) == 0
24062 || STRNCMP(p, "wh", 2) == 0
24063 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 || STRNCMP(p, "try", 3) == 0)
24065 indent += 2;
24066
24067 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024068 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024070 if (*p == '!')
24071 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010024073 vim_free(trans_function_name(&p, TRUE, 0, NULL));
24074 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024076 ++nesting;
24077 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 }
24079 }
24080
24081 /* Check for ":append" or ":insert". */
24082 p = skip_range(p, NULL);
24083 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24084 || (p[0] == 'i'
24085 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24086 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24087 skip_until = vim_strsave((char_u *)".");
24088
24089 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24090 arg = skipwhite(skiptowhite(p));
24091 if (arg[0] == '<' && arg[1] =='<'
24092 && ((p[0] == 'p' && p[1] == 'y'
24093 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24094 || (p[0] == 'p' && p[1] == 'e'
24095 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24096 || (p[0] == 't' && p[1] == 'c'
24097 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024098 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24099 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24101 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024102 || (p[0] == 'm' && p[1] == 'z'
24103 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 ))
24105 {
24106 /* ":python <<" continues until a dot, like ":append" */
24107 p = skipwhite(arg + 2);
24108 if (*p == NUL)
24109 skip_until = vim_strsave((char_u *)".");
24110 else
24111 skip_until = vim_strsave(p);
24112 }
24113 }
24114
24115 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024116 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024117 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024118 if (line_arg == NULL)
24119 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024121 }
24122
24123 /* Copy the line to newly allocated memory. get_one_sourceline()
24124 * allocates 250 bytes per line, this saves 80% on average. The cost
24125 * is an extra alloc/free. */
24126 p = vim_strsave(theline);
24127 if (p != NULL)
24128 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024129 if (line_arg == NULL)
24130 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024131 theline = p;
24132 }
24133
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024134 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24135
24136 /* Add NULL lines for continuation lines, so that the line count is
24137 * equal to the index in the growarray. */
24138 while (sourcing_lnum_off-- > 0)
24139 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024140
24141 /* Check for end of eap->arg. */
24142 if (line_arg != NULL && *line_arg == NUL)
24143 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 }
24145
24146 /* Don't define the function when skipping commands or when an error was
24147 * detected. */
24148 if (eap->skip || did_emsg)
24149 goto erret;
24150
24151 /*
24152 * If there are no errors, add the function
24153 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024154 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024155 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024156 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024157 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024159 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024160 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024161 goto erret;
24162 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024163
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164 fp = find_func(name);
24165 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024167 if (!eap->forceit)
24168 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024169 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024170 goto erret;
24171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024172 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024173 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024174 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024175 name);
24176 goto erret;
24177 }
24178 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024179 ga_clear_strings(&(fp->uf_args));
24180 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024181 vim_free(name);
24182 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 }
24185 else
24186 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024187 char numbuf[20];
24188
24189 fp = NULL;
24190 if (fudi.fd_newkey == NULL && !eap->forceit)
24191 {
24192 EMSG(_(e_funcdict));
24193 goto erret;
24194 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024195 if (fudi.fd_di == NULL)
24196 {
24197 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024198 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024199 goto erret;
24200 }
24201 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024202 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024203 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204
24205 /* Give the function a sequential number. Can only be used with a
24206 * Funcref! */
24207 vim_free(name);
24208 sprintf(numbuf, "%d", ++func_nr);
24209 name = vim_strsave((char_u *)numbuf);
24210 if (name == NULL)
24211 goto erret;
24212 }
24213
24214 if (fp == NULL)
24215 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024216 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024217 {
24218 int slen, plen;
24219 char_u *scriptname;
24220
24221 /* Check that the autoload name matches the script name. */
24222 j = FAIL;
24223 if (sourcing_name != NULL)
24224 {
24225 scriptname = autoload_name(name);
24226 if (scriptname != NULL)
24227 {
24228 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024229 plen = (int)STRLEN(p);
24230 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024231 if (slen > plen && fnamecmp(p,
24232 sourcing_name + slen - plen) == 0)
24233 j = OK;
24234 vim_free(scriptname);
24235 }
24236 }
24237 if (j == FAIL)
24238 {
24239 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24240 goto erret;
24241 }
24242 }
24243
24244 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 if (fp == NULL)
24246 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247
24248 if (fudi.fd_dict != NULL)
24249 {
24250 if (fudi.fd_di == NULL)
24251 {
24252 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024253 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024254 if (fudi.fd_di == NULL)
24255 {
24256 vim_free(fp);
24257 goto erret;
24258 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024259 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24260 {
24261 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024262 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024263 goto erret;
24264 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024265 }
24266 else
24267 /* overwrite existing dict entry */
24268 clear_tv(&fudi.fd_di->di_tv);
24269 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024270 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024271 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024272 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024273
24274 /* behave like "dict" was used */
24275 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024276 }
24277
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024279 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024280 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24281 {
24282 vim_free(fp);
24283 goto erret;
24284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024286 fp->uf_args = newargs;
24287 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024288#ifdef FEAT_PROFILE
24289 fp->uf_tml_count = NULL;
24290 fp->uf_tml_total = NULL;
24291 fp->uf_tml_self = NULL;
24292 fp->uf_profiling = FALSE;
24293 if (prof_def_func())
24294 func_do_profile(fp);
24295#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 fp->uf_varargs = varargs;
24297 fp->uf_flags = flags;
24298 fp->uf_calls = 0;
24299 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024300 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301
24302erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 ga_clear_strings(&newargs);
24304 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024305ret_free:
24306 vim_free(skip_until);
24307 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024310 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311}
24312
24313/*
24314 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024315 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024317 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024318 * TFN_INT: internal function name OK
24319 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024320 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 * Advances "pp" to just after the function name (if no error).
24322 */
24323 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024324trans_function_name(
24325 char_u **pp,
24326 int skip, /* only find the end, don't evaluate */
24327 int flags,
24328 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024330 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 char_u *start;
24332 char_u *end;
24333 int lead;
24334 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024336 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024337
24338 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024339 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024341
24342 /* Check for hard coded <SNR>: already translated function ID (from a user
24343 * command). */
24344 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24345 && (*pp)[2] == (int)KE_SNR)
24346 {
24347 *pp += 3;
24348 len = get_id_len(pp) + 3;
24349 return vim_strnsave(start, len);
24350 }
24351
24352 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24353 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024355 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024357
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024358 /* Note that TFN_ flags use the same values as GLV_ flags. */
24359 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024360 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024361 if (end == start)
24362 {
24363 if (!skip)
24364 EMSG(_("E129: Function name required"));
24365 goto theend;
24366 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024367 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024368 {
24369 /*
24370 * Report an invalid expression in braces, unless the expression
24371 * evaluation has been cancelled due to an aborting error, an
24372 * interrupt, or an exception.
24373 */
24374 if (!aborting())
24375 {
24376 if (end != NULL)
24377 EMSG2(_(e_invarg2), start);
24378 }
24379 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024380 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024381 goto theend;
24382 }
24383
24384 if (lv.ll_tv != NULL)
24385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024386 if (fdp != NULL)
24387 {
24388 fdp->fd_dict = lv.ll_dict;
24389 fdp->fd_newkey = lv.ll_newkey;
24390 lv.ll_newkey = NULL;
24391 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024392 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024393 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24394 {
24395 name = vim_strsave(lv.ll_tv->vval.v_string);
24396 *pp = end;
24397 }
24398 else
24399 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024400 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24401 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024402 EMSG(_(e_funcref));
24403 else
24404 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024405 name = NULL;
24406 }
24407 goto theend;
24408 }
24409
24410 if (lv.ll_name == NULL)
24411 {
24412 /* Error found, but continue after the function name. */
24413 *pp = end;
24414 goto theend;
24415 }
24416
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024417 /* Check if the name is a Funcref. If so, use the value. */
24418 if (lv.ll_exp_name != NULL)
24419 {
24420 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024421 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024422 if (name == lv.ll_exp_name)
24423 name = NULL;
24424 }
24425 else
24426 {
24427 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024428 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024429 if (name == *pp)
24430 name = NULL;
24431 }
24432 if (name != NULL)
24433 {
24434 name = vim_strsave(name);
24435 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024436 if (STRNCMP(name, "<SNR>", 5) == 0)
24437 {
24438 /* Change "<SNR>" to the byte sequence. */
24439 name[0] = K_SPECIAL;
24440 name[1] = KS_EXTRA;
24441 name[2] = (int)KE_SNR;
24442 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24443 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024444 goto theend;
24445 }
24446
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024447 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024448 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024449 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024450 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24451 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24452 {
24453 /* When there was "s:" already or the name expanded to get a
24454 * leading "s:" then remove it. */
24455 lv.ll_name += 2;
24456 len -= 2;
24457 lead = 2;
24458 }
24459 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024460 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024461 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024462 /* skip over "s:" and "g:" */
24463 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024464 lv.ll_name += 2;
24465 len = (int)(end - lv.ll_name);
24466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024467
24468 /*
24469 * Copy the function name to allocated memory.
24470 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24471 * Accept <SNR>123_name() outside a script.
24472 */
24473 if (skip)
24474 lead = 0; /* do nothing */
24475 else if (lead > 0)
24476 {
24477 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024478 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24479 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024480 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024481 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024482 if (current_SID <= 0)
24483 {
24484 EMSG(_(e_usingsid));
24485 goto theend;
24486 }
24487 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24488 lead += (int)STRLEN(sid_buf);
24489 }
24490 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024491 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024492 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024493 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024494 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024495 goto theend;
24496 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024497 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024498 {
24499 char_u *cp = vim_strchr(lv.ll_name, ':');
24500
24501 if (cp != NULL && cp < end)
24502 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024503 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024504 goto theend;
24505 }
24506 }
24507
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024508 name = alloc((unsigned)(len + lead + 1));
24509 if (name != NULL)
24510 {
24511 if (lead > 0)
24512 {
24513 name[0] = K_SPECIAL;
24514 name[1] = KS_EXTRA;
24515 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024516 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024517 STRCPY(name + 3, sid_buf);
24518 }
24519 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024520 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024521 }
24522 *pp = end;
24523
24524theend:
24525 clear_lval(&lv);
24526 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527}
24528
24529/*
24530 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24531 * Return 2 if "p" starts with "s:".
24532 * Return 0 otherwise.
24533 */
24534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024535eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024537 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24538 * the standard library function. */
24539 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24540 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 return 5;
24542 if (p[0] == 's' && p[1] == ':')
24543 return 2;
24544 return 0;
24545}
24546
24547/*
24548 * Return TRUE if "p" starts with "<SID>" or "s:".
24549 * Only works if eval_fname_script() returned non-zero for "p"!
24550 */
24551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024552eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024553{
24554 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24555}
24556
24557/*
24558 * List the head of the function: "name(arg1, arg2)".
24559 */
24560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024561list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562{
24563 int j;
24564
24565 msg_start();
24566 if (indent)
24567 MSG_PUTS(" ");
24568 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024569 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 {
24571 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024572 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 }
24574 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024575 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024577 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 {
24579 if (j)
24580 MSG_PUTS(", ");
24581 msg_puts(FUNCARG(fp, j));
24582 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024583 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 {
24585 if (j)
24586 MSG_PUTS(", ");
24587 MSG_PUTS("...");
24588 }
24589 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024590 if (fp->uf_flags & FC_ABORT)
24591 MSG_PUTS(" abort");
24592 if (fp->uf_flags & FC_RANGE)
24593 MSG_PUTS(" range");
24594 if (fp->uf_flags & FC_DICT)
24595 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024596 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024597 if (p_verbose > 0)
24598 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599}
24600
24601/*
24602 * Find a function by name, return pointer to it in ufuncs.
24603 * Return NULL for unknown function.
24604 */
24605 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024606find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024608 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024610 hi = hash_find(&func_hashtab, name);
24611 if (!HASHITEM_EMPTY(hi))
24612 return HI2UF(hi);
24613 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614}
24615
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024616#if defined(EXITFREE) || defined(PROTO)
24617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024618free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024619{
24620 hashitem_T *hi;
24621
24622 /* Need to start all over every time, because func_free() may change the
24623 * hash table. */
24624 while (func_hashtab.ht_used > 0)
24625 for (hi = func_hashtab.ht_array; ; ++hi)
24626 if (!HASHITEM_EMPTY(hi))
24627 {
24628 func_free(HI2UF(hi));
24629 break;
24630 }
24631}
24632#endif
24633
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024635translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024636{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024637 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024638 return find_internal_func(name) >= 0;
24639 return find_func(name) != NULL;
24640}
24641
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024642/*
24643 * Return TRUE if a function "name" exists.
24644 */
24645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024646function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024647{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024648 char_u *nm = name;
24649 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024650 int n = FALSE;
24651
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024652 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24653 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024654 nm = skipwhite(nm);
24655
24656 /* Only accept "funcname", "funcname ", "funcname (..." and
24657 * "funcname(...", not "funcname!...". */
24658 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024659 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024660 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024661 return n;
24662}
24663
Bram Moolenaara1544c02013-05-30 12:35:52 +020024664 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024665get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024666{
24667 char_u *nm = name;
24668 char_u *p;
24669
24670 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24671
24672 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024673 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024674 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024675
Bram Moolenaara1544c02013-05-30 12:35:52 +020024676 vim_free(p);
24677 return NULL;
24678}
24679
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024680/*
24681 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024682 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24683 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024684 */
24685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024686builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024687{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024688 char_u *p;
24689
24690 if (!ASCII_ISLOWER(name[0]))
24691 return FALSE;
24692 p = vim_strchr(name, AUTOLOAD_CHAR);
24693 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024694}
24695
Bram Moolenaar05159a02005-02-26 23:04:13 +000024696#if defined(FEAT_PROFILE) || defined(PROTO)
24697/*
24698 * Start profiling function "fp".
24699 */
24700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024703 int len = fp->uf_lines.ga_len;
24704
24705 if (len == 0)
24706 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024707 fp->uf_tm_count = 0;
24708 profile_zero(&fp->uf_tm_self);
24709 profile_zero(&fp->uf_tm_total);
24710 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024711 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024712 if (fp->uf_tml_total == NULL)
24713 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024714 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024715 if (fp->uf_tml_self == NULL)
24716 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024717 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024718 fp->uf_tml_idx = -1;
24719 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24720 || fp->uf_tml_self == NULL)
24721 return; /* out of memory */
24722
24723 fp->uf_profiling = TRUE;
24724}
24725
24726/*
24727 * Dump the profiling results for all functions in file "fd".
24728 */
24729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024730func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024731{
24732 hashitem_T *hi;
24733 int todo;
24734 ufunc_T *fp;
24735 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024736 ufunc_T **sorttab;
24737 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024738
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024739 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024740 if (todo == 0)
24741 return; /* nothing to dump */
24742
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024743 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024744
Bram Moolenaar05159a02005-02-26 23:04:13 +000024745 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24746 {
24747 if (!HASHITEM_EMPTY(hi))
24748 {
24749 --todo;
24750 fp = HI2UF(hi);
24751 if (fp->uf_profiling)
24752 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024753 if (sorttab != NULL)
24754 sorttab[st_len++] = fp;
24755
Bram Moolenaar05159a02005-02-26 23:04:13 +000024756 if (fp->uf_name[0] == K_SPECIAL)
24757 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24758 else
24759 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24760 if (fp->uf_tm_count == 1)
24761 fprintf(fd, "Called 1 time\n");
24762 else
24763 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24764 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24765 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24766 fprintf(fd, "\n");
24767 fprintf(fd, "count total (s) self (s)\n");
24768
24769 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24770 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024771 if (FUNCLINE(fp, i) == NULL)
24772 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024773 prof_func_line(fd, fp->uf_tml_count[i],
24774 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024775 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24776 }
24777 fprintf(fd, "\n");
24778 }
24779 }
24780 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024781
24782 if (sorttab != NULL && st_len > 0)
24783 {
24784 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24785 prof_total_cmp);
24786 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24787 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24788 prof_self_cmp);
24789 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24790 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024791
24792 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024793}
Bram Moolenaar73830342005-02-28 22:48:19 +000024794
24795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024796prof_sort_list(
24797 FILE *fd,
24798 ufunc_T **sorttab,
24799 int st_len,
24800 char *title,
24801 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024802{
24803 int i;
24804 ufunc_T *fp;
24805
24806 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24807 fprintf(fd, "count total (s) self (s) function\n");
24808 for (i = 0; i < 20 && i < st_len; ++i)
24809 {
24810 fp = sorttab[i];
24811 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24812 prefer_self);
24813 if (fp->uf_name[0] == K_SPECIAL)
24814 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24815 else
24816 fprintf(fd, " %s()\n", fp->uf_name);
24817 }
24818 fprintf(fd, "\n");
24819}
24820
24821/*
24822 * Print the count and times for one function or function line.
24823 */
24824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024825prof_func_line(
24826 FILE *fd,
24827 int count,
24828 proftime_T *total,
24829 proftime_T *self,
24830 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024831{
24832 if (count > 0)
24833 {
24834 fprintf(fd, "%5d ", count);
24835 if (prefer_self && profile_equal(total, self))
24836 fprintf(fd, " ");
24837 else
24838 fprintf(fd, "%s ", profile_msg(total));
24839 if (!prefer_self && profile_equal(total, self))
24840 fprintf(fd, " ");
24841 else
24842 fprintf(fd, "%s ", profile_msg(self));
24843 }
24844 else
24845 fprintf(fd, " ");
24846}
24847
24848/*
24849 * Compare function for total time sorting.
24850 */
24851 static int
24852#ifdef __BORLANDC__
24853_RTLENTRYF
24854#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024855prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024856{
24857 ufunc_T *p1, *p2;
24858
24859 p1 = *(ufunc_T **)s1;
24860 p2 = *(ufunc_T **)s2;
24861 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24862}
24863
24864/*
24865 * Compare function for self time sorting.
24866 */
24867 static int
24868#ifdef __BORLANDC__
24869_RTLENTRYF
24870#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024871prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024872{
24873 ufunc_T *p1, *p2;
24874
24875 p1 = *(ufunc_T **)s1;
24876 p2 = *(ufunc_T **)s2;
24877 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24878}
24879
Bram Moolenaar05159a02005-02-26 23:04:13 +000024880#endif
24881
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024882/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024883 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024884 * Return TRUE if a package was loaded.
24885 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024887script_autoload(
24888 char_u *name,
24889 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024890{
24891 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024892 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024893 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024894 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024895
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024896 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024897 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024898 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024899 return FALSE;
24900
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024901 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024902
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024903 /* Find the name in the list of previously loaded package names. Skip
24904 * "autoload/", it's always the same. */
24905 for (i = 0; i < ga_loaded.ga_len; ++i)
24906 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24907 break;
24908 if (!reload && i < ga_loaded.ga_len)
24909 ret = FALSE; /* was loaded already */
24910 else
24911 {
24912 /* Remember the name if it wasn't loaded already. */
24913 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24914 {
24915 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24916 tofree = NULL;
24917 }
24918
24919 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024920 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024921 ret = TRUE;
24922 }
24923
24924 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024925 return ret;
24926}
24927
24928/*
24929 * Return the autoload script name for a function or variable name.
24930 * Returns NULL when out of memory.
24931 */
24932 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024933autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024934{
24935 char_u *p;
24936 char_u *scriptname;
24937
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024938 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024939 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24940 if (scriptname == NULL)
24941 return FALSE;
24942 STRCPY(scriptname, "autoload/");
24943 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024944 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024945 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024946 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024947 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024948 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024949}
24950
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24952
24953/*
24954 * Function given to ExpandGeneric() to obtain the list of user defined
24955 * function names.
24956 */
24957 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024958get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024960 static long_u done;
24961 static hashitem_T *hi;
24962 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963
24964 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024966 done = 0;
24967 hi = func_hashtab.ht_array;
24968 }
24969 if (done < func_hashtab.ht_used)
24970 {
24971 if (done++ > 0)
24972 ++hi;
24973 while (HASHITEM_EMPTY(hi))
24974 ++hi;
24975 fp = HI2UF(hi);
24976
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024977 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024978 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024979
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024980 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24981 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024982
24983 cat_func_name(IObuff, fp);
24984 if (xp->xp_context != EXPAND_USER_FUNC)
24985 {
24986 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024987 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 STRCAT(IObuff, ")");
24989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 return IObuff;
24991 }
24992 return NULL;
24993}
24994
24995#endif /* FEAT_CMDL_COMPL */
24996
24997/*
24998 * Copy the function name of "fp" to buffer "buf".
24999 * "buf" must be able to hold the function name plus three bytes.
25000 * Takes care of script-local function names.
25001 */
25002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025003cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025005 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 {
25007 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025008 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 }
25010 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025011 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012}
25013
25014/*
25015 * ":delfunction {name}"
25016 */
25017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025018ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025020 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025021 char_u *p;
25022 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024
25025 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025026 name = trans_function_name(&p, eap->skip, 0, &fudi);
25027 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025029 {
25030 if (fudi.fd_dict != NULL && !eap->skip)
25031 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 if (!ends_excmd(*skipwhite(p)))
25035 {
25036 vim_free(name);
25037 EMSG(_(e_trailing));
25038 return;
25039 }
25040 eap->nextcmd = check_nextcmd(p);
25041 if (eap->nextcmd != NULL)
25042 *p = NUL;
25043
25044 if (!eap->skip)
25045 fp = find_func(name);
25046 vim_free(name);
25047
25048 if (!eap->skip)
25049 {
25050 if (fp == NULL)
25051 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025052 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 return;
25054 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025055 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 {
25057 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25058 return;
25059 }
25060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025061 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025063 /* Delete the dict item that refers to the function, it will
25064 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025065 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025067 else
25068 func_free(fp);
25069 }
25070}
25071
25072/*
25073 * Free a function and remove it from the list of functions.
25074 */
25075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025076func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025077{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025078 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025079
25080 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025081 ga_clear_strings(&(fp->uf_args));
25082 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025083#ifdef FEAT_PROFILE
25084 vim_free(fp->uf_tml_count);
25085 vim_free(fp->uf_tml_total);
25086 vim_free(fp->uf_tml_self);
25087#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025088
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025089 /* remove the function from the function hashtable */
25090 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25091 if (HASHITEM_EMPTY(hi))
25092 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025093 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025094 hash_remove(&func_hashtab, hi);
25095
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025096 vim_free(fp);
25097}
25098
25099/*
25100 * Unreference a Function: decrement the reference count and free it when it
25101 * becomes zero. Only for numbered functions.
25102 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025104func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025105{
25106 ufunc_T *fp;
25107
25108 if (name != NULL && isdigit(*name))
25109 {
25110 fp = find_func(name);
25111 if (fp == NULL)
25112 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025113 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025114 {
25115 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025116 * when "uf_calls" becomes zero. */
25117 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025118 func_free(fp);
25119 }
25120 }
25121}
25122
25123/*
25124 * Count a reference to a Function.
25125 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025127func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025128{
25129 ufunc_T *fp;
25130
25131 if (name != NULL && isdigit(*name))
25132 {
25133 fp = find_func(name);
25134 if (fp == NULL)
25135 EMSG2(_(e_intern2), "func_ref()");
25136 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025137 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 }
25139}
25140
25141/*
25142 * Call a user function.
25143 */
25144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025145call_user_func(
25146 ufunc_T *fp, /* pointer to function */
25147 int argcount, /* nr of args */
25148 typval_T *argvars, /* arguments */
25149 typval_T *rettv, /* return value */
25150 linenr_T firstline, /* first line of range */
25151 linenr_T lastline, /* last line of range */
25152 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153{
Bram Moolenaar33570922005-01-25 22:26:29 +000025154 char_u *save_sourcing_name;
25155 linenr_T save_sourcing_lnum;
25156 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025157 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025158 int save_did_emsg;
25159 static int depth = 0;
25160 dictitem_T *v;
25161 int fixvar_idx = 0; /* index in fixvar[] */
25162 int i;
25163 int ai;
25164 char_u numbuf[NUMBUFLEN];
25165 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025166 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025167#ifdef FEAT_PROFILE
25168 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025169 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171
25172 /* If depth of calling is getting too high, don't execute the function */
25173 if (depth >= p_mfd)
25174 {
25175 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025176 rettv->v_type = VAR_NUMBER;
25177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 return;
25179 }
25180 ++depth;
25181
25182 line_breakcheck(); /* check for CTRL-C hit */
25183
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025184 fc = (funccall_T *)alloc(sizeof(funccall_T));
25185 fc->caller = current_funccal;
25186 current_funccal = fc;
25187 fc->func = fp;
25188 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025189 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025190 fc->linenr = 0;
25191 fc->returned = FALSE;
25192 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025194 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25195 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196
Bram Moolenaar33570922005-01-25 22:26:29 +000025197 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025198 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025199 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25200 * each argument variable and saves a lot of time.
25201 */
25202 /*
25203 * Init l: variables.
25204 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025205 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025206 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025207 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025208 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25209 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025210 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025211 name = v->di_key;
25212 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025213 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025214 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025215 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025216 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025217 v->di_tv.vval.v_dict = selfdict;
25218 ++selfdict->dv_refcount;
25219 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025220
Bram Moolenaar33570922005-01-25 22:26:29 +000025221 /*
25222 * Init a: variables.
25223 * Set a:0 to "argcount".
25224 * Set a:000 to a list with room for the "..." arguments.
25225 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025226 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025227 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025228 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025229 /* Use "name" to avoid a warning from some compiler that checks the
25230 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025231 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025232 name = v->di_key;
25233 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025234 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025235 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025236 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025237 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025238 v->di_tv.vval.v_list = &fc->l_varlist;
25239 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25240 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25241 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025242
25243 /*
25244 * Set a:firstline to "firstline" and a:lastline to "lastline".
25245 * Set a:name to named arguments.
25246 * Set a:N to the "..." arguments.
25247 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025248 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025250 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025251 (varnumber_T)lastline);
25252 for (i = 0; i < argcount; ++i)
25253 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025254 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025255 if (ai < 0)
25256 /* named argument a:name */
25257 name = FUNCARG(fp, i);
25258 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025259 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 /* "..." argument a:1, a:2, etc. */
25261 sprintf((char *)numbuf, "%d", ai + 1);
25262 name = numbuf;
25263 }
25264 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25265 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025266 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025267 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25268 }
25269 else
25270 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025271 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25272 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025273 if (v == NULL)
25274 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025275 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 }
25277 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025279
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025280 /* Note: the values are copied directly to avoid alloc/free.
25281 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025282 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025283 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025284
25285 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25286 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025287 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25288 fc->l_listitems[ai].li_tv = argvars[i];
25289 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025290 }
25291 }
25292
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 /* Don't redraw while executing the function. */
25294 ++RedrawingDisabled;
25295 save_sourcing_name = sourcing_name;
25296 save_sourcing_lnum = sourcing_lnum;
25297 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025298 /* need space for function name + ("function " + 3) or "[number]" */
25299 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25300 + STRLEN(fp->uf_name) + 20;
25301 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (sourcing_name != NULL)
25303 {
25304 if (save_sourcing_name != NULL
25305 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025306 sprintf((char *)sourcing_name, "%s[%d]..",
25307 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 else
25309 STRCPY(sourcing_name, "function ");
25310 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25311
25312 if (p_verbose >= 12)
25313 {
25314 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025315 verbose_enter_scroll();
25316
Bram Moolenaar555b2802005-05-19 21:08:39 +000025317 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 if (p_verbose >= 14)
25319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025321 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025322 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025323 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324
25325 msg_puts((char_u *)"(");
25326 for (i = 0; i < argcount; ++i)
25327 {
25328 if (i > 0)
25329 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025330 if (argvars[i].v_type == VAR_NUMBER)
25331 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 else
25333 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025334 /* Do not want errors such as E724 here. */
25335 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025336 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025337 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025338 if (s != NULL)
25339 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025340 if (vim_strsize(s) > MSG_BUF_CLEN)
25341 {
25342 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25343 s = buf;
25344 }
25345 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025346 vim_free(tofree);
25347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 }
25349 }
25350 msg_puts((char_u *)")");
25351 }
25352 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025353
25354 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 --no_wait_return;
25356 }
25357 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360 {
25361 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25362 func_do_profile(fp);
25363 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025364 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025365 {
25366 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025367 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368 profile_zero(&fp->uf_tm_children);
25369 }
25370 script_prof_save(&wait_start);
25371 }
25372#endif
25373
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025375 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 save_did_emsg = did_emsg;
25377 did_emsg = FALSE;
25378
25379 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025380 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25382
25383 --RedrawingDisabled;
25384
25385 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025386 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025388 clear_tv(rettv);
25389 rettv->v_type = VAR_NUMBER;
25390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 }
25392
Bram Moolenaar05159a02005-02-26 23:04:13 +000025393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025394 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025395 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025396 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025397 profile_end(&call_start);
25398 profile_sub_wait(&wait_start, &call_start);
25399 profile_add(&fp->uf_tm_total, &call_start);
25400 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025401 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025402 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025403 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25404 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025405 }
25406 }
25407#endif
25408
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 /* when being verbose, mention the return value */
25410 if (p_verbose >= 12)
25411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025413 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025416 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025417 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025418 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025419 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025422 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025423 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025424 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025425 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025426
Bram Moolenaar555b2802005-05-19 21:08:39 +000025427 /* The value may be very long. Skip the middle part, so that we
25428 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025429 * truncate it at the end. Don't want errors such as E724 here. */
25430 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025431 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025432 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025433 if (s != NULL)
25434 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025435 if (vim_strsize(s) > MSG_BUF_CLEN)
25436 {
25437 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25438 s = buf;
25439 }
25440 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025441 vim_free(tofree);
25442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 }
25444 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025445
25446 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 --no_wait_return;
25448 }
25449
25450 vim_free(sourcing_name);
25451 sourcing_name = save_sourcing_name;
25452 sourcing_lnum = save_sourcing_lnum;
25453 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025454#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025455 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025456 script_prof_restore(&wait_start);
25457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458
25459 if (p_verbose >= 12 && sourcing_name != NULL)
25460 {
25461 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025462 verbose_enter_scroll();
25463
Bram Moolenaar555b2802005-05-19 21:08:39 +000025464 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025466
25467 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 --no_wait_return;
25469 }
25470
25471 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025472 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025474
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025475 /* If the a:000 list and the l: and a: dicts are not referenced we can
25476 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025477 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25478 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25479 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25480 {
25481 free_funccal(fc, FALSE);
25482 }
25483 else
25484 {
25485 hashitem_T *hi;
25486 listitem_T *li;
25487 int todo;
25488
25489 /* "fc" is still in use. This can happen when returning "a:000" or
25490 * assigning "l:" to a global variable.
25491 * Link "fc" in the list for garbage collection later. */
25492 fc->caller = previous_funccal;
25493 previous_funccal = fc;
25494
25495 /* Make a copy of the a: variables, since we didn't do that above. */
25496 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25497 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25498 {
25499 if (!HASHITEM_EMPTY(hi))
25500 {
25501 --todo;
25502 v = HI2DI(hi);
25503 copy_tv(&v->di_tv, &v->di_tv);
25504 }
25505 }
25506
25507 /* Make a copy of the a:000 items, since we didn't do that above. */
25508 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25509 copy_tv(&li->li_tv, &li->li_tv);
25510 }
25511}
25512
25513/*
25514 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025515 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025516 */
25517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025518can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025519{
25520 return (fc->l_varlist.lv_copyID != copyID
25521 && fc->l_vars.dv_copyID != copyID
25522 && fc->l_avars.dv_copyID != copyID);
25523}
25524
25525/*
25526 * Free "fc" and what it contains.
25527 */
25528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025529free_funccal(
25530 funccall_T *fc,
25531 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025532{
25533 listitem_T *li;
25534
25535 /* The a: variables typevals may not have been allocated, only free the
25536 * allocated variables. */
25537 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25538
25539 /* free all l: variables */
25540 vars_clear(&fc->l_vars.dv_hashtab);
25541
25542 /* Free the a:000 variables if they were allocated. */
25543 if (free_val)
25544 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25545 clear_tv(&li->li_tv);
25546
25547 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548}
25549
25550/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025551 * Add a number variable "name" to dict "dp" with value "nr".
25552 */
25553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025554add_nr_var(
25555 dict_T *dp,
25556 dictitem_T *v,
25557 char *name,
25558 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025559{
25560 STRCPY(v->di_key, name);
25561 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25562 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25563 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025564 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 v->di_tv.vval.v_number = nr;
25566}
25567
25568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 * ":return [expr]"
25570 */
25571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025572ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573{
25574 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025575 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 int returning = FALSE;
25577
25578 if (current_funccal == NULL)
25579 {
25580 EMSG(_("E133: :return not inside a function"));
25581 return;
25582 }
25583
25584 if (eap->skip)
25585 ++emsg_skip;
25586
25587 eap->nextcmd = NULL;
25588 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025589 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590 {
25591 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025592 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025594 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595 }
25596 /* It's safer to return also on error. */
25597 else if (!eap->skip)
25598 {
25599 /*
25600 * Return unless the expression evaluation has been cancelled due to an
25601 * aborting error, an interrupt, or an exception.
25602 */
25603 if (!aborting())
25604 returning = do_return(eap, FALSE, TRUE, NULL);
25605 }
25606
25607 /* When skipping or the return gets pending, advance to the next command
25608 * in this line (!returning). Otherwise, ignore the rest of the line.
25609 * Following lines will be ignored by get_func_line(). */
25610 if (returning)
25611 eap->nextcmd = NULL;
25612 else if (eap->nextcmd == NULL) /* no argument */
25613 eap->nextcmd = check_nextcmd(arg);
25614
25615 if (eap->skip)
25616 --emsg_skip;
25617}
25618
25619/*
25620 * Return from a function. Possibly makes the return pending. Also called
25621 * for a pending return at the ":endtry" or after returning from an extra
25622 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025623 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025624 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625 * FALSE when the return gets pending.
25626 */
25627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025628do_return(
25629 exarg_T *eap,
25630 int reanimate,
25631 int is_cmd,
25632 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633{
25634 int idx;
25635 struct condstack *cstack = eap->cstack;
25636
25637 if (reanimate)
25638 /* Undo the return. */
25639 current_funccal->returned = FALSE;
25640
25641 /*
25642 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25643 * not in its finally clause (which then is to be executed next) is found.
25644 * In this case, make the ":return" pending for execution at the ":endtry".
25645 * Otherwise, return normally.
25646 */
25647 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25648 if (idx >= 0)
25649 {
25650 cstack->cs_pending[idx] = CSTP_RETURN;
25651
25652 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025653 /* A pending return again gets pending. "rettv" points to an
25654 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025656 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 else
25658 {
25659 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025660 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025662 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025664 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 {
25666 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025667 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025668 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 else
25670 EMSG(_(e_outofmem));
25671 }
25672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025673 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674
25675 if (reanimate)
25676 {
25677 /* The pending return value could be overwritten by a ":return"
25678 * without argument in a finally clause; reset the default
25679 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025680 current_funccal->rettv->v_type = VAR_NUMBER;
25681 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 }
25683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025684 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 }
25686 else
25687 {
25688 current_funccal->returned = TRUE;
25689
25690 /* If the return is carried out now, store the return value. For
25691 * a return immediately after reanimation, the value is already
25692 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025693 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025695 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025696 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025698 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 }
25700 }
25701
25702 return idx < 0;
25703}
25704
25705/*
25706 * Free the variable with a pending return value.
25707 */
25708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025709discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710{
Bram Moolenaar33570922005-01-25 22:26:29 +000025711 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712}
25713
25714/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025715 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716 * is an allocated string. Used by report_pending() for verbose messages.
25717 */
25718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025719get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025721 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025722 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025725 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025726 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025727 if (s == NULL)
25728 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025729
25730 STRCPY(IObuff, ":return ");
25731 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25732 if (STRLEN(s) + 8 >= IOSIZE)
25733 STRCPY(IObuff + IOSIZE - 4, "...");
25734 vim_free(tofree);
25735 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736}
25737
25738/*
25739 * Get next function line.
25740 * Called by do_cmdline() to get the next line.
25741 * Returns allocated string, or NULL for end of function.
25742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025744get_func_line(
25745 int c UNUSED,
25746 void *cookie,
25747 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748{
Bram Moolenaar33570922005-01-25 22:26:29 +000025749 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025750 ufunc_T *fp = fcp->func;
25751 char_u *retval;
25752 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753
25754 /* If breakpoints have been added/deleted need to check for it. */
25755 if (fcp->dbg_tick != debug_tick)
25756 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025757 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 sourcing_lnum);
25759 fcp->dbg_tick = debug_tick;
25760 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025761#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025762 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025763 func_line_end(cookie);
25764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765
Bram Moolenaar05159a02005-02-26 23:04:13 +000025766 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025767 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25768 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 retval = NULL;
25770 else
25771 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025772 /* Skip NULL lines (continuation lines). */
25773 while (fcp->linenr < gap->ga_len
25774 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25775 ++fcp->linenr;
25776 if (fcp->linenr >= gap->ga_len)
25777 retval = NULL;
25778 else
25779 {
25780 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25781 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025782#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025783 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025784 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025785#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 }
25788
25789 /* Did we encounter a breakpoint? */
25790 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25791 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025792 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025794 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795 sourcing_lnum);
25796 fcp->dbg_tick = debug_tick;
25797 }
25798
25799 return retval;
25800}
25801
Bram Moolenaar05159a02005-02-26 23:04:13 +000025802#if defined(FEAT_PROFILE) || defined(PROTO)
25803/*
25804 * Called when starting to read a function line.
25805 * "sourcing_lnum" must be correct!
25806 * When skipping lines it may not actually be executed, but we won't find out
25807 * until later and we need to store the time now.
25808 */
25809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025810func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025811{
25812 funccall_T *fcp = (funccall_T *)cookie;
25813 ufunc_T *fp = fcp->func;
25814
25815 if (fp->uf_profiling && sourcing_lnum >= 1
25816 && sourcing_lnum <= fp->uf_lines.ga_len)
25817 {
25818 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025819 /* Skip continuation lines. */
25820 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25821 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025822 fp->uf_tml_execed = FALSE;
25823 profile_start(&fp->uf_tml_start);
25824 profile_zero(&fp->uf_tml_children);
25825 profile_get_wait(&fp->uf_tml_wait);
25826 }
25827}
25828
25829/*
25830 * Called when actually executing a function line.
25831 */
25832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025833func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025834{
25835 funccall_T *fcp = (funccall_T *)cookie;
25836 ufunc_T *fp = fcp->func;
25837
25838 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25839 fp->uf_tml_execed = TRUE;
25840}
25841
25842/*
25843 * Called when done with a function line.
25844 */
25845 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025846func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025847{
25848 funccall_T *fcp = (funccall_T *)cookie;
25849 ufunc_T *fp = fcp->func;
25850
25851 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25852 {
25853 if (fp->uf_tml_execed)
25854 {
25855 ++fp->uf_tml_count[fp->uf_tml_idx];
25856 profile_end(&fp->uf_tml_start);
25857 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025858 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025859 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25860 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025861 }
25862 fp->uf_tml_idx = -1;
25863 }
25864}
25865#endif
25866
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867/*
25868 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025869 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 */
25871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025872func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873{
Bram Moolenaar33570922005-01-25 22:26:29 +000025874 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875
25876 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25877 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025878 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 || fcp->returned);
25880}
25881
25882/*
25883 * return TRUE if cookie indicates a function which "abort"s on errors.
25884 */
25885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025886func_has_abort(
25887 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025889 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890}
25891
25892#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25893typedef enum
25894{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025895 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25896 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25897 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898} var_flavour_T;
25899
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025900static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025901
25902 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025903var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904{
25905 char_u *p = varname;
25906
25907 if (ASCII_ISUPPER(*p))
25908 {
25909 while (*(++p))
25910 if (ASCII_ISLOWER(*p))
25911 return VAR_FLAVOUR_SESSION;
25912 return VAR_FLAVOUR_VIMINFO;
25913 }
25914 else
25915 return VAR_FLAVOUR_DEFAULT;
25916}
25917#endif
25918
25919#if defined(FEAT_VIMINFO) || defined(PROTO)
25920/*
25921 * Restore global vars that start with a capital from the viminfo file
25922 */
25923 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925{
25926 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025927 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025928 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025929 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930
25931 if (!writing && (find_viminfo_parameter('!') != NULL))
25932 {
25933 tab = vim_strchr(virp->vir_line + 1, '\t');
25934 if (tab != NULL)
25935 {
25936 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025937 switch (*tab)
25938 {
25939 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025940#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025941 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025942#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025943 case 'D': type = VAR_DICT; break;
25944 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025945 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947
25948 tab = vim_strchr(tab, '\t');
25949 if (tab != NULL)
25950 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025951 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025952 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025953 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025955#ifdef FEAT_FLOAT
25956 else if (type == VAR_FLOAT)
25957 (void)string2float(tab + 1, &tv.vval.v_float);
25958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025960 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025961 if (type == VAR_DICT || type == VAR_LIST)
25962 {
25963 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25964
25965 if (etv == NULL)
25966 /* Failed to parse back the dict or list, use it as a
25967 * string. */
25968 tv.v_type = VAR_STRING;
25969 else
25970 {
25971 vim_free(tv.vval.v_string);
25972 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025973 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025974 }
25975 }
25976
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025977 /* when in a function use global variables */
25978 save_funccal = current_funccal;
25979 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025980 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025981 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025982
25983 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025984 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025985 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25986 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 }
25988 }
25989 }
25990
25991 return viminfo_readline(virp);
25992}
25993
25994/*
25995 * Write global vars that start with a capital to the viminfo file
25996 */
25997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025998write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999{
Bram Moolenaar33570922005-01-25 22:26:29 +000026000 hashitem_T *hi;
26001 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026002 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026003 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026004 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026005 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026006 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007
26008 if (find_viminfo_parameter('!') == NULL)
26009 return;
26010
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026011 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026013 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026014 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026016 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026018 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026019 this_var = HI2DI(hi);
26020 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026022 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026023 {
26024 case VAR_STRING: s = "STR"; break;
26025 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026026 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026027 case VAR_DICT: s = "DIC"; break;
26028 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026029 case VAR_SPECIAL: s = "XPL"; break;
26030
26031 case VAR_UNKNOWN:
26032 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026033 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026034 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026035 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026036 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026037 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026038 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026039 if (p != NULL)
26040 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026041 vim_free(tofree);
26042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 }
26044 }
26045}
26046#endif
26047
26048#if defined(FEAT_SESSION) || defined(PROTO)
26049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026050store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051{
Bram Moolenaar33570922005-01-25 22:26:29 +000026052 hashitem_T *hi;
26053 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026054 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 char_u *p, *t;
26056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026057 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026058 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026060 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026062 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026063 this_var = HI2DI(hi);
26064 if ((this_var->di_tv.v_type == VAR_NUMBER
26065 || this_var->di_tv.v_type == VAR_STRING)
26066 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026067 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026068 /* Escape special characters with a backslash. Turn a LF and
26069 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026070 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026071 (char_u *)"\\\"\n\r");
26072 if (p == NULL) /* out of memory */
26073 break;
26074 for (t = p; *t != NUL; ++t)
26075 if (*t == '\n')
26076 *t = 'n';
26077 else if (*t == '\r')
26078 *t = 'r';
26079 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026080 this_var->di_key,
26081 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26082 : ' ',
26083 p,
26084 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26085 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026086 || put_eol(fd) == FAIL)
26087 {
26088 vim_free(p);
26089 return FAIL;
26090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091 vim_free(p);
26092 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026093#ifdef FEAT_FLOAT
26094 else if (this_var->di_tv.v_type == VAR_FLOAT
26095 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26096 {
26097 float_T f = this_var->di_tv.vval.v_float;
26098 int sign = ' ';
26099
26100 if (f < 0)
26101 {
26102 f = -f;
26103 sign = '-';
26104 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026105 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026106 this_var->di_key, sign, f) < 0)
26107 || put_eol(fd) == FAIL)
26108 return FAIL;
26109 }
26110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 }
26112 }
26113 return OK;
26114}
26115#endif
26116
Bram Moolenaar661b1822005-07-28 22:36:45 +000026117/*
26118 * Display script name where an item was last set.
26119 * Should only be invoked when 'verbose' is non-zero.
26120 */
26121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026122last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026123{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026124 char_u *p;
26125
Bram Moolenaar661b1822005-07-28 22:36:45 +000026126 if (scriptID != 0)
26127 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026128 p = home_replace_save(NULL, get_scriptname(scriptID));
26129 if (p != NULL)
26130 {
26131 verbose_enter();
26132 MSG_PUTS(_("\n\tLast set from "));
26133 MSG_PUTS(p);
26134 vim_free(p);
26135 verbose_leave();
26136 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026137 }
26138}
26139
Bram Moolenaard812df62008-11-09 12:46:09 +000026140/*
26141 * List v:oldfiles in a nice way.
26142 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026144ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026145{
26146 list_T *l = vimvars[VV_OLDFILES].vv_list;
26147 listitem_T *li;
26148 int nr = 0;
26149
26150 if (l == NULL)
26151 msg((char_u *)_("No old files"));
26152 else
26153 {
26154 msg_start();
26155 msg_scroll = TRUE;
26156 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26157 {
26158 msg_outnum((long)++nr);
26159 MSG_PUTS(": ");
26160 msg_outtrans(get_tv_string(&li->li_tv));
26161 msg_putchar('\n');
26162 out_flush(); /* output one line at a time */
26163 ui_breakcheck();
26164 }
26165 /* Assume "got_int" was set to truncate the listing. */
26166 got_int = FALSE;
26167
26168#ifdef FEAT_BROWSE_CMD
26169 if (cmdmod.browse)
26170 {
26171 quit_more = FALSE;
26172 nr = prompt_for_number(FALSE);
26173 msg_starthere();
26174 if (nr > 0)
26175 {
26176 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26177 (long)nr);
26178
26179 if (p != NULL)
26180 {
26181 p = expand_env_save(p);
26182 eap->arg = p;
26183 eap->cmdidx = CMD_edit;
26184 cmdmod.browse = FALSE;
26185 do_exedit(eap, NULL);
26186 vim_free(p);
26187 }
26188 }
26189 }
26190#endif
26191 }
26192}
26193
Bram Moolenaar53744302015-07-17 17:38:22 +020026194/* reset v:option_new, v:option_old and v:option_type */
26195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026196reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026197{
26198 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26199 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26200 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26201}
26202
26203
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204#endif /* FEAT_EVAL */
26205
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026207#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208
26209#ifdef WIN3264
26210/*
26211 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26212 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026213static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26214static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26215static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026216
26217/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026218 * Get the short path (8.3) for the filename in "fnamep".
26219 * Only works for a valid file name.
26220 * When the path gets longer "fnamep" is changed and the allocated buffer
26221 * is put in "bufp".
26222 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26223 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224 */
26225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026226get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026228 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026229 char_u *newbuf;
26230
26231 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026232 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026233 if (l > len - 1)
26234 {
26235 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026236 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237 newbuf = vim_strnsave(*fnamep, l);
26238 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026239 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240
26241 vim_free(*bufp);
26242 *fnamep = *bufp = newbuf;
26243
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026244 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026245 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 }
26247
26248 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026249 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250}
26251
26252/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026253 * Get the short path (8.3) for the filename in "fname". The converted
26254 * path is returned in "bufp".
26255 *
26256 * Some of the directories specified in "fname" may not exist. This function
26257 * will shorten the existing directories at the beginning of the path and then
26258 * append the remaining non-existing path.
26259 *
26260 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026261 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026262 * bufp - Pointer to an allocated buffer for the filename.
26263 * fnamelen - Length of the filename pointed to by fname
26264 *
26265 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026266 */
26267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026268shortpath_for_invalid_fname(
26269 char_u **fname,
26270 char_u **bufp,
26271 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026272{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026273 char_u *short_fname, *save_fname, *pbuf_unused;
26274 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 int old_len, len;
26277 int new_len, sfx_len;
26278 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026279
26280 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026281 old_len = *fnamelen;
26282 save_fname = vim_strnsave(*fname, old_len);
26283 pbuf_unused = NULL;
26284 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026286 endp = save_fname + old_len - 1; /* Find the end of the copy */
26287 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026289 /*
26290 * Try shortening the supplied path till it succeeds by removing one
26291 * directory at a time from the tail of the path.
26292 */
26293 len = 0;
26294 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026296 /* go back one path-separator */
26297 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26298 --endp;
26299 if (endp <= save_fname)
26300 break; /* processed the complete path */
26301
26302 /*
26303 * Replace the path separator with a NUL and try to shorten the
26304 * resulting path.
26305 */
26306 ch = *endp;
26307 *endp = 0;
26308 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026309 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026310 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26311 {
26312 retval = FAIL;
26313 goto theend;
26314 }
26315 *endp = ch; /* preserve the string */
26316
26317 if (len > 0)
26318 break; /* successfully shortened the path */
26319
26320 /* failed to shorten the path. Skip the path separator */
26321 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026322 }
26323
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026326 /*
26327 * Succeeded in shortening the path. Now concatenate the shortened
26328 * path with the remaining path at the tail.
26329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026331 /* Compute the length of the new path. */
26332 sfx_len = (int)(save_endp - endp) + 1;
26333 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026334
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026335 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026337 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026338 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026339 /* There is not enough space in the currently allocated string,
26340 * copy it to a buffer big enough. */
26341 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026342 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026343 {
26344 retval = FAIL;
26345 goto theend;
26346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026347 }
26348 else
26349 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026350 /* Transfer short_fname to the main buffer (it's big enough),
26351 * unless get_short_pathname() did its work in-place. */
26352 *fname = *bufp = save_fname;
26353 if (short_fname != save_fname)
26354 vim_strncpy(save_fname, short_fname, len);
26355 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026357
26358 /* concat the not-shortened part of the path */
26359 vim_strncpy(*fname + len, endp, sfx_len);
26360 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026362
26363theend:
26364 vim_free(pbuf_unused);
26365 vim_free(save_fname);
26366
26367 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368}
26369
26370/*
26371 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026372 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026373 */
26374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026375shortpath_for_partial(
26376 char_u **fnamep,
26377 char_u **bufp,
26378 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379{
26380 int sepcount, len, tflen;
26381 char_u *p;
26382 char_u *pbuf, *tfname;
26383 int hasTilde;
26384
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026385 /* Count up the path separators from the RHS.. so we know which part
26386 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026387 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026388 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389 if (vim_ispathsep(*p))
26390 ++sepcount;
26391
26392 /* Need full path first (use expand_env() to remove a "~/") */
26393 hasTilde = (**fnamep == '~');
26394 if (hasTilde)
26395 pbuf = tfname = expand_env_save(*fnamep);
26396 else
26397 pbuf = tfname = FullName_save(*fnamep, FALSE);
26398
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026399 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026401 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403
26404 if (len == 0)
26405 {
26406 /* Don't have a valid filename, so shorten the rest of the
26407 * path if we can. This CAN give us invalid 8.3 filenames, but
26408 * there's not a lot of point in guessing what it might be.
26409 */
26410 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026411 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26412 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413 }
26414
26415 /* Count the paths backward to find the beginning of the desired string. */
26416 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026417 {
26418#ifdef FEAT_MBYTE
26419 if (has_mbyte)
26420 p -= mb_head_off(tfname, p);
26421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422 if (vim_ispathsep(*p))
26423 {
26424 if (sepcount == 0 || (hasTilde && sepcount == 1))
26425 break;
26426 else
26427 sepcount --;
26428 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 if (hasTilde)
26431 {
26432 --p;
26433 if (p >= tfname)
26434 *p = '~';
26435 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026437 }
26438 else
26439 ++p;
26440
26441 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26442 vim_free(*bufp);
26443 *fnamelen = (int)STRLEN(p);
26444 *bufp = pbuf;
26445 *fnamep = p;
26446
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448}
26449#endif /* WIN3264 */
26450
26451/*
26452 * Adjust a filename, according to a string of modifiers.
26453 * *fnamep must be NUL terminated when called. When returning, the length is
26454 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026455 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 * When there is an error, *fnamep is set to NULL.
26457 */
26458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026459modify_fname(
26460 char_u *src, /* string with modifiers */
26461 int *usedlen, /* characters after src that are used */
26462 char_u **fnamep, /* file name so far */
26463 char_u **bufp, /* buffer for allocated file name or NULL */
26464 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465{
26466 int valid = 0;
26467 char_u *tail;
26468 char_u *s, *p, *pbuf;
26469 char_u dirname[MAXPATHL];
26470 int c;
26471 int has_fullname = 0;
26472#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026473 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474 int has_shortname = 0;
26475#endif
26476
26477repeat:
26478 /* ":p" - full path/file_name */
26479 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26480 {
26481 has_fullname = 1;
26482
26483 valid |= VALID_PATH;
26484 *usedlen += 2;
26485
26486 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26487 if ((*fnamep)[0] == '~'
26488#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26489 && ((*fnamep)[1] == '/'
26490# ifdef BACKSLASH_IN_FILENAME
26491 || (*fnamep)[1] == '\\'
26492# endif
26493 || (*fnamep)[1] == NUL)
26494
26495#endif
26496 )
26497 {
26498 *fnamep = expand_env_save(*fnamep);
26499 vim_free(*bufp); /* free any allocated file name */
26500 *bufp = *fnamep;
26501 if (*fnamep == NULL)
26502 return -1;
26503 }
26504
26505 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026506 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507 {
26508 if (vim_ispathsep(*p)
26509 && p[1] == '.'
26510 && (p[2] == NUL
26511 || vim_ispathsep(p[2])
26512 || (p[2] == '.'
26513 && (p[3] == NUL || vim_ispathsep(p[3])))))
26514 break;
26515 }
26516
26517 /* FullName_save() is slow, don't use it when not needed. */
26518 if (*p != NUL || !vim_isAbsName(*fnamep))
26519 {
26520 *fnamep = FullName_save(*fnamep, *p != NUL);
26521 vim_free(*bufp); /* free any allocated file name */
26522 *bufp = *fnamep;
26523 if (*fnamep == NULL)
26524 return -1;
26525 }
26526
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026527#ifdef WIN3264
26528# if _WIN32_WINNT >= 0x0500
26529 if (vim_strchr(*fnamep, '~') != NULL)
26530 {
26531 /* Expand 8.3 filename to full path. Needed to make sure the same
26532 * file does not have two different names.
26533 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26534 p = alloc(_MAX_PATH + 1);
26535 if (p != NULL)
26536 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026537 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026538 {
26539 vim_free(*bufp);
26540 *bufp = *fnamep = p;
26541 }
26542 else
26543 vim_free(p);
26544 }
26545 }
26546# endif
26547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026548 /* Append a path separator to a directory. */
26549 if (mch_isdir(*fnamep))
26550 {
26551 /* Make room for one or two extra characters. */
26552 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26553 vim_free(*bufp); /* free any allocated file name */
26554 *bufp = *fnamep;
26555 if (*fnamep == NULL)
26556 return -1;
26557 add_pathsep(*fnamep);
26558 }
26559 }
26560
26561 /* ":." - path relative to the current directory */
26562 /* ":~" - path relative to the home directory */
26563 /* ":8" - shortname path - postponed till after */
26564 while (src[*usedlen] == ':'
26565 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26566 {
26567 *usedlen += 2;
26568 if (c == '8')
26569 {
26570#ifdef WIN3264
26571 has_shortname = 1; /* Postpone this. */
26572#endif
26573 continue;
26574 }
26575 pbuf = NULL;
26576 /* Need full path first (use expand_env() to remove a "~/") */
26577 if (!has_fullname)
26578 {
26579 if (c == '.' && **fnamep == '~')
26580 p = pbuf = expand_env_save(*fnamep);
26581 else
26582 p = pbuf = FullName_save(*fnamep, FALSE);
26583 }
26584 else
26585 p = *fnamep;
26586
26587 has_fullname = 0;
26588
26589 if (p != NULL)
26590 {
26591 if (c == '.')
26592 {
26593 mch_dirname(dirname, MAXPATHL);
26594 s = shorten_fname(p, dirname);
26595 if (s != NULL)
26596 {
26597 *fnamep = s;
26598 if (pbuf != NULL)
26599 {
26600 vim_free(*bufp); /* free any allocated file name */
26601 *bufp = pbuf;
26602 pbuf = NULL;
26603 }
26604 }
26605 }
26606 else
26607 {
26608 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26609 /* Only replace it when it starts with '~' */
26610 if (*dirname == '~')
26611 {
26612 s = vim_strsave(dirname);
26613 if (s != NULL)
26614 {
26615 *fnamep = s;
26616 vim_free(*bufp);
26617 *bufp = s;
26618 }
26619 }
26620 }
26621 vim_free(pbuf);
26622 }
26623 }
26624
26625 tail = gettail(*fnamep);
26626 *fnamelen = (int)STRLEN(*fnamep);
26627
26628 /* ":h" - head, remove "/file_name", can be repeated */
26629 /* Don't remove the first "/" or "c:\" */
26630 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26631 {
26632 valid |= VALID_HEAD;
26633 *usedlen += 2;
26634 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026635 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026636 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026637 *fnamelen = (int)(tail - *fnamep);
26638#ifdef VMS
26639 if (*fnamelen > 0)
26640 *fnamelen += 1; /* the path separator is part of the path */
26641#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026642 if (*fnamelen == 0)
26643 {
26644 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26645 p = vim_strsave((char_u *)".");
26646 if (p == NULL)
26647 return -1;
26648 vim_free(*bufp);
26649 *bufp = *fnamep = tail = p;
26650 *fnamelen = 1;
26651 }
26652 else
26653 {
26654 while (tail > s && !after_pathsep(s, tail))
26655 mb_ptr_back(*fnamep, tail);
26656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 }
26658
26659 /* ":8" - shortname */
26660 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26661 {
26662 *usedlen += 2;
26663#ifdef WIN3264
26664 has_shortname = 1;
26665#endif
26666 }
26667
26668#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026669 /*
26670 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026671 */
26672 if (has_shortname)
26673 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026674 /* Copy the string if it is shortened by :h and when it wasn't copied
26675 * yet, because we are going to change it in place. Avoids changing
26676 * the buffer name for "%:8". */
26677 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026678 {
26679 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026680 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026681 return -1;
26682 vim_free(*bufp);
26683 *bufp = *fnamep = p;
26684 }
26685
26686 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026687 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026688 if (!has_fullname && !vim_isAbsName(*fnamep))
26689 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026690 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691 return -1;
26692 }
26693 else
26694 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026695 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696
Bram Moolenaardc935552011-08-17 15:23:23 +020026697 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026698 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026699 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 return -1;
26701
26702 if (l == 0)
26703 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026704 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026705 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026706 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026707 return -1;
26708 }
26709 *fnamelen = l;
26710 }
26711 }
26712#endif /* WIN3264 */
26713
26714 /* ":t" - tail, just the basename */
26715 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26716 {
26717 *usedlen += 2;
26718 *fnamelen -= (int)(tail - *fnamep);
26719 *fnamep = tail;
26720 }
26721
26722 /* ":e" - extension, can be repeated */
26723 /* ":r" - root, without extension, can be repeated */
26724 while (src[*usedlen] == ':'
26725 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26726 {
26727 /* find a '.' in the tail:
26728 * - for second :e: before the current fname
26729 * - otherwise: The last '.'
26730 */
26731 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26732 s = *fnamep - 2;
26733 else
26734 s = *fnamep + *fnamelen - 1;
26735 for ( ; s > tail; --s)
26736 if (s[0] == '.')
26737 break;
26738 if (src[*usedlen + 1] == 'e') /* :e */
26739 {
26740 if (s > tail)
26741 {
26742 *fnamelen += (int)(*fnamep - (s + 1));
26743 *fnamep = s + 1;
26744#ifdef VMS
26745 /* cut version from the extension */
26746 s = *fnamep + *fnamelen - 1;
26747 for ( ; s > *fnamep; --s)
26748 if (s[0] == ';')
26749 break;
26750 if (s > *fnamep)
26751 *fnamelen = s - *fnamep;
26752#endif
26753 }
26754 else if (*fnamep <= tail)
26755 *fnamelen = 0;
26756 }
26757 else /* :r */
26758 {
26759 if (s > tail) /* remove one extension */
26760 *fnamelen = (int)(s - *fnamep);
26761 }
26762 *usedlen += 2;
26763 }
26764
26765 /* ":s?pat?foo?" - substitute */
26766 /* ":gs?pat?foo?" - global substitute */
26767 if (src[*usedlen] == ':'
26768 && (src[*usedlen + 1] == 's'
26769 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26770 {
26771 char_u *str;
26772 char_u *pat;
26773 char_u *sub;
26774 int sep;
26775 char_u *flags;
26776 int didit = FALSE;
26777
26778 flags = (char_u *)"";
26779 s = src + *usedlen + 2;
26780 if (src[*usedlen + 1] == 'g')
26781 {
26782 flags = (char_u *)"g";
26783 ++s;
26784 }
26785
26786 sep = *s++;
26787 if (sep)
26788 {
26789 /* find end of pattern */
26790 p = vim_strchr(s, sep);
26791 if (p != NULL)
26792 {
26793 pat = vim_strnsave(s, (int)(p - s));
26794 if (pat != NULL)
26795 {
26796 s = p + 1;
26797 /* find end of substitution */
26798 p = vim_strchr(s, sep);
26799 if (p != NULL)
26800 {
26801 sub = vim_strnsave(s, (int)(p - s));
26802 str = vim_strnsave(*fnamep, *fnamelen);
26803 if (sub != NULL && str != NULL)
26804 {
26805 *usedlen = (int)(p + 1 - src);
26806 s = do_string_sub(str, pat, sub, flags);
26807 if (s != NULL)
26808 {
26809 *fnamep = s;
26810 *fnamelen = (int)STRLEN(s);
26811 vim_free(*bufp);
26812 *bufp = s;
26813 didit = TRUE;
26814 }
26815 }
26816 vim_free(sub);
26817 vim_free(str);
26818 }
26819 vim_free(pat);
26820 }
26821 }
26822 /* after using ":s", repeat all the modifiers */
26823 if (didit)
26824 goto repeat;
26825 }
26826 }
26827
Bram Moolenaar26df0922014-02-23 23:39:13 +010026828 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26829 {
26830 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26831 if (p == NULL)
26832 return -1;
26833 vim_free(*bufp);
26834 *bufp = *fnamep = p;
26835 *fnamelen = (int)STRLEN(p);
26836 *usedlen += 2;
26837 }
26838
Bram Moolenaar071d4272004-06-13 20:20:40 +000026839 return valid;
26840}
26841
26842/*
26843 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26844 * "flags" can be "g" to do a global substitute.
26845 * Returns an allocated string, NULL for error.
26846 */
26847 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026848do_string_sub(
26849 char_u *str,
26850 char_u *pat,
26851 char_u *sub,
26852 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026853{
26854 int sublen;
26855 regmatch_T regmatch;
26856 int i;
26857 int do_all;
26858 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026859 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026860 garray_T ga;
26861 char_u *ret;
26862 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026863 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864
26865 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26866 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026867 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026868
26869 ga_init2(&ga, 1, 200);
26870
26871 do_all = (flags[0] == 'g');
26872
26873 regmatch.rm_ic = p_ic;
26874 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26875 if (regmatch.regprog != NULL)
26876 {
26877 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026878 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026879 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26880 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026881 /* Skip empty match except for first match. */
26882 if (regmatch.startp[0] == regmatch.endp[0])
26883 {
26884 if (zero_width == regmatch.startp[0])
26885 {
26886 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026887 i = MB_PTR2LEN(tail);
26888 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26889 (size_t)i);
26890 ga.ga_len += i;
26891 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026892 continue;
26893 }
26894 zero_width = regmatch.startp[0];
26895 }
26896
Bram Moolenaar071d4272004-06-13 20:20:40 +000026897 /*
26898 * Get some space for a temporary buffer to do the substitution
26899 * into. It will contain:
26900 * - The text up to where the match is.
26901 * - The substituted text.
26902 * - The text after the match.
26903 */
26904 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026905 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026906 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26907 {
26908 ga_clear(&ga);
26909 break;
26910 }
26911
26912 /* copy the text up to where the match is */
26913 i = (int)(regmatch.startp[0] - tail);
26914 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26915 /* add the substituted text */
26916 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26917 + ga.ga_len + i, TRUE, TRUE, FALSE);
26918 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026919 tail = regmatch.endp[0];
26920 if (*tail == NUL)
26921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026922 if (!do_all)
26923 break;
26924 }
26925
26926 if (ga.ga_data != NULL)
26927 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26928
Bram Moolenaar473de612013-06-08 18:19:48 +020026929 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026930 }
26931
26932 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26933 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026934 if (p_cpo == empty_option)
26935 p_cpo = save_cpo;
26936 else
26937 /* Darn, evaluating {sub} expression changed the value. */
26938 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939
26940 return ret;
26941}
26942
26943#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */