blob: e78bbb067fdf0d62d7a861ad650269121872bcc4 [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 Moolenaar509ce2a2016-03-11 22:52:15 +0100499#ifdef FEAT_JOB_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 +0100504static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
507static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100508static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100512static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
630static void f_isnan(typval_T *argvars, typval_T *rettv);
631#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100632static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100635static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100636static void f_job_start(typval_T *argvars, typval_T *rettv);
637static void f_job_stop(typval_T *argvars, typval_T *rettv);
638static void f_job_status(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100641static void f_js_decode(typval_T *argvars, typval_T *rettv);
642static void f_js_encode(typval_T *argvars, typval_T *rettv);
643static void f_json_decode(typval_T *argvars, typval_T *rettv);
644static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_keys(typval_T *argvars, typval_T *rettv);
646static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
647static void f_len(typval_T *argvars, typval_T *rettv);
648static void f_libcall(typval_T *argvars, typval_T *rettv);
649static void f_libcallnr(typval_T *argvars, typval_T *rettv);
650static void f_line(typval_T *argvars, typval_T *rettv);
651static void f_line2byte(typval_T *argvars, typval_T *rettv);
652static void f_lispindent(typval_T *argvars, typval_T *rettv);
653static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_log(typval_T *argvars, typval_T *rettv);
656static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_map(typval_T *argvars, typval_T *rettv);
662static void f_maparg(typval_T *argvars, typval_T *rettv);
663static void f_mapcheck(typval_T *argvars, typval_T *rettv);
664static void f_match(typval_T *argvars, typval_T *rettv);
665static void f_matchadd(typval_T *argvars, typval_T *rettv);
666static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
667static void f_matcharg(typval_T *argvars, typval_T *rettv);
668static void f_matchdelete(typval_T *argvars, typval_T *rettv);
669static void f_matchend(typval_T *argvars, typval_T *rettv);
670static void f_matchlist(typval_T *argvars, typval_T *rettv);
671static void f_matchstr(typval_T *argvars, typval_T *rettv);
672static void f_max(typval_T *argvars, typval_T *rettv);
673static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
682static void f_nr2char(typval_T *argvars, typval_T *rettv);
683static void f_or(typval_T *argvars, typval_T *rettv);
684static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
692static void f_printf(typval_T *argvars, typval_T *rettv);
693static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
697#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_range(typval_T *argvars, typval_T *rettv);
701static void f_readfile(typval_T *argvars, typval_T *rettv);
702static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100703#ifdef FEAT_FLOAT
704static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_reltimestr(typval_T *argvars, typval_T *rettv);
707static void f_remote_expr(typval_T *argvars, typval_T *rettv);
708static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
709static void f_remote_peek(typval_T *argvars, typval_T *rettv);
710static void f_remote_read(typval_T *argvars, typval_T *rettv);
711static void f_remote_send(typval_T *argvars, typval_T *rettv);
712static void f_remove(typval_T *argvars, typval_T *rettv);
713static void f_rename(typval_T *argvars, typval_T *rettv);
714static void f_repeat(typval_T *argvars, typval_T *rettv);
715static void f_resolve(typval_T *argvars, typval_T *rettv);
716static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_screenattr(typval_T *argvars, typval_T *rettv);
721static void f_screenchar(typval_T *argvars, typval_T *rettv);
722static void f_screencol(typval_T *argvars, typval_T *rettv);
723static void f_screenrow(typval_T *argvars, typval_T *rettv);
724static void f_search(typval_T *argvars, typval_T *rettv);
725static void f_searchdecl(typval_T *argvars, typval_T *rettv);
726static void f_searchpair(typval_T *argvars, typval_T *rettv);
727static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
728static void f_searchpos(typval_T *argvars, typval_T *rettv);
729static void f_server2client(typval_T *argvars, typval_T *rettv);
730static void f_serverlist(typval_T *argvars, typval_T *rettv);
731static void f_setbufvar(typval_T *argvars, typval_T *rettv);
732static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
733static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100734static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tolower(typval_T *argvars, typval_T *rettv);
797static void f_toupper(typval_T *argvars, typval_T *rettv);
798static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_type(typval_T *argvars, typval_T *rettv);
803static void f_undofile(typval_T *argvars, typval_T *rettv);
804static void f_undotree(typval_T *argvars, typval_T *rettv);
805static void f_uniq(typval_T *argvars, typval_T *rettv);
806static void f_values(typval_T *argvars, typval_T *rettv);
807static void f_virtcol(typval_T *argvars, typval_T *rettv);
808static void f_visualmode(typval_T *argvars, typval_T *rettv);
809static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
810static void f_winbufnr(typval_T *argvars, typval_T *rettv);
811static void f_wincol(typval_T *argvars, typval_T *rettv);
812static void f_winheight(typval_T *argvars, typval_T *rettv);
813static void f_winline(typval_T *argvars, typval_T *rettv);
814static void f_winnr(typval_T *argvars, typval_T *rettv);
815static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
816static void f_winrestview(typval_T *argvars, typval_T *rettv);
817static void f_winsaveview(typval_T *argvars, typval_T *rettv);
818static void f_winwidth(typval_T *argvars, typval_T *rettv);
819static void f_writefile(typval_T *argvars, typval_T *rettv);
820static void f_wordcount(typval_T *argvars, typval_T *rettv);
821static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
824static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
825static int get_env_len(char_u **arg);
826static int get_id_len(char_u **arg);
827static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
828static 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 +0000829#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
830#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
831 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
833static int eval_isnamec(int c);
834static int eval_isnamec1(int c);
835static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
836static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static typval_T *alloc_string_tv(char_u *string);
838static void init_tv(typval_T *varp);
839static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100840#ifdef FEAT_FLOAT
841static float_T get_tv_float(typval_T *varp);
842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static linenr_T get_tv_lnum(typval_T *argvars);
844static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
845static char_u *get_tv_string(typval_T *varp);
846static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
847static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
848static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
849static hashtab_T *find_var_ht(char_u *name, char_u **varname);
850static funccall_T *get_funccal(void);
851static void vars_clear_ext(hashtab_T *ht, int free_val);
852static void delete_var(hashtab_T *ht, hashitem_T *hi);
853static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
854static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
855static void set_var(char_u *name, typval_T *varp, int copy);
856static int var_check_ro(int flags, char_u *name, int use_gettext);
857static int var_check_fixed(int flags, char_u *name, int use_gettext);
858static int var_check_func_name(char_u *name, int new_var);
859static int valid_varname(char_u *varname);
860static int tv_check_lock(int lock, char_u *name, int use_gettext);
861static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
862static char_u *find_option_end(char_u **arg, int *opt_flags);
863static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
864static int eval_fname_script(char_u *p);
865static int eval_fname_sid(char_u *p);
866static void list_func_head(ufunc_T *fp, int indent);
867static ufunc_T *find_func(char_u *name);
868static int function_exists(char_u *name);
869static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static void func_do_profile(ufunc_T *fp);
872static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
873static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000874static int
875# ifdef __BORLANDC__
876 _RTLENTRYF
877# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000879static int
880# ifdef __BORLANDC__
881 _RTLENTRYF
882# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int script_autoload(char_u *name, int reload);
886static char_u *autoload_name(char_u *name);
887static void cat_func_name(char_u *buf, ufunc_T *fp);
888static void func_free(ufunc_T *fp);
889static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
890static int can_free_funccal(funccall_T *fc, int copyID) ;
891static void free_funccal(funccall_T *fc, int free_val);
892static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
893static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
894static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
895static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
896static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
897static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
898static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int write_list(FILE *fd, list_T *list, int binary);
900static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000901
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902
903#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static int compare_func_name(const void *s1, const void *s2);
905static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906#endif
907
Bram Moolenaar33570922005-01-25 22:26:29 +0000908/*
909 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000910 */
911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000913{
Bram Moolenaar33570922005-01-25 22:26:29 +0000914 int i;
915 struct vimvar *p;
916
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200917 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
918 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200919 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000920 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000921 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000922
923 for (i = 0; i < VV_LEN; ++i)
924 {
925 p = &vimvars[i];
926 STRCPY(p->vv_di.di_key, p->vv_name);
927 if (p->vv_flags & VV_RO)
928 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
929 else if (p->vv_flags & VV_RO_SBX)
930 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
931 else
932 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000933
934 /* add to v: scope dict, unless the value is not always available */
935 if (p->vv_type != VAR_UNKNOWN)
936 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000938 /* add to compat scope dict */
939 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100941 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
942
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000943 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100944 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200945 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100946 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100947
948 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
949 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
950 set_vim_var_nr(VV_NONE, VVAL_NONE);
951 set_vim_var_nr(VV_NULL, VVAL_NULL);
952
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200953 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954
955#ifdef EBCDIC
956 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100957 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958 */
959 sortFunctions();
960#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000961}
962
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000963#if defined(EXITFREE) || defined(PROTO)
964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100965eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966{
967 int i;
968 struct vimvar *p;
969
970 for (i = 0; i < VV_LEN; ++i)
971 {
972 p = &vimvars[i];
973 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000975 vim_free(p->vv_str);
976 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000977 }
978 else if (p->vv_di.di_tv.v_type == VAR_LIST)
979 {
980 list_unref(p->vv_list);
981 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000982 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000983 }
984 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000985 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 hash_clear(&compat_hashtab);
987
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200990 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100991# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992
993 /* global variables */
994 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000995
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000996 /* autoloaded script names */
997 ga_clear_strings(&ga_loaded);
998
Bram Moolenaarcca74132013-09-25 21:00:28 +0200999 /* Script-local variables. First clear all the variables and in a second
1000 * loop free the scriptvar_T, because a variable in one script might hold
1001 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001004 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 ga_clear(&ga_scripts);
1007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001008 /* unreferenced lists and dicts */
1009 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001010
1011 /* functions */
1012 free_all_functions();
1013 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014}
1015#endif
1016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 * Return the name of the executed function.
1019 */
1020 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001021func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024}
1025
1026/*
1027 * Return the address holding the next breakpoint line for a funccall cookie.
1028 */
1029 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar33570922005-01-25 22:26:29 +00001032 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the debug tick for a funccall cookie.
1037 */
1038 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001039func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the nesting level for a funccall cookie.
1046 */
1047 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001054funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001056/* pointer to list of previously used funccal, still around because some
1057 * item in it is still being used. */
1058funccall_T *previous_funccal = NULL;
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060/*
1061 * Return TRUE when a function was ended by a ":return" command.
1062 */
1063 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 return current_funccal->returned;
1067}
1068
1069
1070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * Set an internal variable to a string value. Creates the variable if it does
1072 * not already exist.
1073 */
1074 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001077 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001078 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 val = vim_strsave(value);
1081 if (val != NULL)
1082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090}
1091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094static char_u *redir_endp = NULL;
1095static char_u *redir_varname = NULL;
1096
1097/*
1098 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 * Returns OK if successfully completed the setup. FAIL otherwise.
1101 */
1102 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104{
1105 int save_emsg;
1106 int err;
1107 typval_T tv;
1108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001110 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 {
1112 EMSG(_(e_invarg));
1113 return FAIL;
1114 }
1115
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 redir_varname = vim_strsave(name);
1118 if (redir_varname == NULL)
1119 return FAIL;
1120
1121 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1122 if (redir_lval == NULL)
1123 {
1124 var_redir_stop();
1125 return FAIL;
1126 }
1127
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 /* The output is stored in growarray "redir_ga" until redirection ends. */
1129 ga_init2(&redir_ga, (int)sizeof(char), 500);
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001132 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001133 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1135 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001136 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp != NULL && *redir_endp != NUL)
1138 /* Trailing characters are present after the variable name */
1139 EMSG(_(e_trailing));
1140 else
1141 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 var_redir_stop();
1144 return FAIL;
1145 }
1146
1147 /* check if we can write to the variable: set it to or append an empty
1148 * string */
1149 save_emsg = did_emsg;
1150 did_emsg = FALSE;
1151 tv.v_type = VAR_STRING;
1152 tv.vval.v_string = (char_u *)"";
1153 if (append)
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1155 else
1156 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001157 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001159 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (err)
1161 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 var_redir_stop();
1164 return FAIL;
1165 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166
1167 return OK;
1168}
1169
1170/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 * Append "value[value_len]" to the variable set by var_redir_start().
1172 * The actual appending is postponed until redirection ends, because the value
1173 * appended may in fact be the string we write to, changing it may cause freed
1174 * memory to be used:
1175 * :redir => foo
1176 * :let foo
1177 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 */
1179 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001180var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001182 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183
1184 if (redir_lval == NULL)
1185 return;
1186
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 {
1194 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 }
1197 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199}
1200
1201/*
1202 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 */
1205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001206var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001208 typval_T tv;
1209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 if (redir_lval != NULL)
1211 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 /* If there was no error: assign the text to the variable. */
1213 if (redir_endp != NULL)
1214 {
1215 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1216 tv.v_type = VAR_STRING;
1217 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 /* Call get_lval() again, if it's inside a Dict or List it may
1219 * have changed. */
1220 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001221 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1223 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1224 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001227 /* free the collected output */
1228 vim_free(redir_ga.ga_data);
1229 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 vim_free(redir_lval);
1232 redir_lval = NULL;
1233 }
1234 vim_free(redir_varname);
1235 redir_varname = NULL;
1236}
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238# if defined(FEAT_MBYTE) || defined(PROTO)
1239 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001240eval_charconvert(
1241 char_u *enc_from,
1242 char_u *enc_to,
1243 char_u *fname_from,
1244 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 int err = FALSE;
1247
1248 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1249 set_vim_var_string(VV_CC_TO, enc_to, -1);
1250 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1251 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1252 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1253 err = TRUE;
1254 set_vim_var_string(VV_CC_FROM, NULL, -1);
1255 set_vim_var_string(VV_CC_TO, NULL, -1);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258
1259 if (err)
1260 return FAIL;
1261 return OK;
1262}
1263# endif
1264
1265# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001267eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 int err = FALSE;
1270
1271 set_vim_var_string(VV_FNAME_IN, fname, -1);
1272 set_vim_var_string(VV_CMDARG, args, -1);
1273 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1274 err = TRUE;
1275 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1276 set_vim_var_string(VV_CMDARG, NULL, -1);
1277
1278 if (err)
1279 {
1280 mch_remove(fname);
1281 return FAIL;
1282 }
1283 return OK;
1284}
1285# endif
1286
1287# if defined(FEAT_DIFF) || defined(PROTO)
1288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289eval_diff(
1290 char_u *origfile,
1291 char_u *newfile,
1292 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int err = FALSE;
1295
1296 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1297 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1298 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1299 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1300 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1301 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1302 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1303}
1304
1305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_patch(
1307 char_u *origfile,
1308 char_u *difffile,
1309 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int err;
1312
1313 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1315 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1316 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1317 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1319 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1320}
1321# endif
1322
1323/*
1324 * Top level evaluation function, returning a boolean.
1325 * Sets "error" to TRUE if there was an error.
1326 * Return TRUE or FALSE.
1327 */
1328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329eval_to_bool(
1330 char_u *arg,
1331 int *error,
1332 char_u **nextcmd,
1333 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar33570922005-01-25 22:26:29 +00001335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int retval = FALSE;
1337
1338 if (skip)
1339 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 {
1344 *error = FALSE;
1345 if (!skip)
1346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001347 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 }
1351 if (skip)
1352 --emsg_skip;
1353
1354 return retval;
1355}
1356
1357/*
1358 * Top level evaluation function, returning a string. If "skip" is TRUE,
1359 * only parsing to "nextcmd" is done, without reporting errors. Return
1360 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1361 */
1362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001363eval_to_string_skip(
1364 char_u *arg,
1365 char_u **nextcmd,
1366 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *retval;
1370
1371 if (skip)
1372 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 retval = NULL;
1375 else
1376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 retval = vim_strsave(get_tv_string(&tv));
1378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001387 * Skip over an expression at "*pp".
1388 * Return FAIL for an error, OK otherwise.
1389 */
1390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001391skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392{
Bram Moolenaar33570922005-01-25 22:26:29 +00001393 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394
1395 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397}
1398
1399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 * When "convert" is TRUE convert a List into a sequence of lines and convert
1402 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Return pointer to allocated memory, or NULL for failure.
1404 */
1405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_to_string(
1407 char_u *arg,
1408 char_u **nextcmd,
1409 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001413 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = NULL;
1420 else
1421 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001422 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001423 {
1424 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001427 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001428 if (tv.vval.v_list->lv_len > 0)
1429 ga_append(&ga, NL);
1430 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 ga_append(&ga, NUL);
1432 retval = (char_u *)ga.ga_data;
1433 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001434#ifdef FEAT_FLOAT
1435 else if (convert && tv.v_type == VAR_FLOAT)
1436 {
1437 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1438 retval = vim_strsave(numbuf);
1439 }
1440#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 else
1442 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
1446 return retval;
1447}
1448
1449/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001450 * Call eval_to_string() without using current local variables and using
1451 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001454eval_to_string_safe(
1455 char_u *arg,
1456 char_u **nextcmd,
1457 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 char_u *retval;
1460 void *save_funccalp;
1461
1462 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 ++sandbox;
1465 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 --sandbox;
1469 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 restore_funccal(save_funccalp);
1471 return retval;
1472}
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474/*
1475 * Top level evaluation function, returning a number.
1476 * Evaluates "expr" silently.
1477 * Returns -1 for an error.
1478 */
1479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
Bram Moolenaar33570922005-01-25 22:26:29 +00001482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001484 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 ++emsg_off;
1487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 retval = -1;
1490 else
1491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 --emsg_off;
1496
1497 return retval;
1498}
1499
Bram Moolenaara40058a2005-07-11 22:42:07 +00001500/*
1501 * Prepare v: variable "idx" to be used.
1502 * Save the current typeval in "save_tv".
1503 * When not used yet add the variable to the v: hashtable.
1504 */
1505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001507{
1508 *save_tv = vimvars[idx].vv_tv;
1509 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1510 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1511}
1512
1513/*
1514 * Restore v: variable "idx" to typeval "save_tv".
1515 * When no longer defined, remove the variable from the v: hashtable.
1516 */
1517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001519{
1520 hashitem_T *hi;
1521
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522 vimvars[idx].vv_tv = *save_tv;
1523 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1524 {
1525 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1526 if (HASHITEM_EMPTY(hi))
1527 EMSG2(_(e_intern2), "restore_vimvar()");
1528 else
1529 hash_remove(&vimvarht, hi);
1530 }
1531}
1532
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001533#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534/*
1535 * Evaluate an expression to a list with suggestions.
1536 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001537 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538 */
1539 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001540eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541{
1542 typval_T save_val;
1543 typval_T rettv;
1544 list_T *list = NULL;
1545 char_u *p = skipwhite(expr);
1546
1547 /* Set "v:val" to the bad word. */
1548 prepare_vimvar(VV_VAL, &save_val);
1549 vimvars[VV_VAL].vv_type = VAR_STRING;
1550 vimvars[VV_VAL].vv_str = badword;
1551 if (p_verbose == 0)
1552 ++emsg_off;
1553
1554 if (eval1(&p, &rettv, TRUE) == OK)
1555 {
1556 if (rettv.v_type != VAR_LIST)
1557 clear_tv(&rettv);
1558 else
1559 list = rettv.vval.v_list;
1560 }
1561
1562 if (p_verbose == 0)
1563 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001564 restore_vimvar(VV_VAL, &save_val);
1565
1566 return list;
1567}
1568
1569/*
1570 * "list" is supposed to contain two items: a word and a number. Return the
1571 * word in "pp" and the number as the return value.
1572 * Return -1 if anything isn't right.
1573 * Used to get the good word and score from the eval_spell_expr() result.
1574 */
1575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577{
1578 listitem_T *li;
1579
1580 li = list->lv_first;
1581 if (li == NULL)
1582 return -1;
1583 *pp = get_tv_string(&li->li_tv);
1584
1585 li = li->li_next;
1586 if (li == NULL)
1587 return -1;
1588 return get_tv_number(&li->li_tv);
1589}
1590#endif
1591
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001593 * Top level evaluation function.
1594 * Returns an allocated typval_T with the result.
1595 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596 */
1597 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001598eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599{
1600 typval_T *tv;
1601
1602 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001603 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001604 {
1605 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 }
1608
1609 return tv;
1610}
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 * Uses argv[argc] for the function arguments. Only Number and String
1616 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620call_vim_function(
1621 char_u *func,
1622 int argc,
1623 char_u **argv,
1624 int safe, /* use the sandbox */
1625 int str_arg_only, /* all arguments are strings */
1626 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar33570922005-01-25 22:26:29 +00001628 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 long n;
1630 int len;
1631 int i;
1632 int doesrange;
1633 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001636 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 for (i = 0; i < argc; i++)
1641 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001642 /* Pass a NULL or empty argument as an empty string */
1643 if (argv[i] == NULL || *argv[i] == NUL)
1644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001645 argvars[i].v_type = VAR_STRING;
1646 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001647 continue;
1648 }
1649
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001650 if (str_arg_only)
1651 len = 0;
1652 else
1653 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001654 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (len != 0 && len == (int)STRLEN(argv[i]))
1656 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001657 argvars[i].v_type = VAR_NUMBER;
1658 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 else
1661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001662 argvars[i].v_type = VAR_STRING;
1663 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 }
1666
1667 if (safe)
1668 {
1669 save_funccalp = save_funccal();
1670 ++sandbox;
1671 }
1672
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1674 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (safe)
1678 {
1679 --sandbox;
1680 restore_funccal(save_funccalp);
1681 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 vim_free(argvars);
1683
1684 if (ret == FAIL)
1685 clear_tv(rettv);
1686
1687 return ret;
1688}
1689
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001690/*
1691 * Call vimL function "func" and return the result as a number.
1692 * Returns -1 when calling the function fails.
1693 * Uses argv[argc] for the function arguments.
1694 */
1695 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001696call_func_retnr(
1697 char_u *func,
1698 int argc,
1699 char_u **argv,
1700 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001701{
1702 typval_T rettv;
1703 long retval;
1704
1705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1707 return -1;
1708
1709 retval = get_tv_number_chk(&rettv, NULL);
1710 clear_tv(&rettv);
1711 return retval;
1712}
1713
1714#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1715 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1716
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719 * Call vimL function "func" and return the result as a string.
1720 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
1722 */
1723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724call_func_retstr(
1725 char_u *func,
1726 int argc,
1727 char_u **argv,
1728 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729{
1730 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001731 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 return retval;
1740}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001741# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001743/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001746 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 */
1748 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749call_func_retlist(
1750 char_u *func,
1751 int argc,
1752 char_u **argv,
1753 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754{
1755 typval_T rettv;
1756
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001757 /* All arguments are passed as strings, no conversion to number. */
1758 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 return NULL;
1760
1761 if (rettv.v_type != VAR_LIST)
1762 {
1763 clear_tv(&rettv);
1764 return NULL;
1765 }
1766
1767 return rettv.vval.v_list;
1768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
1770
1771/*
1772 * Save the current function call pointer, and set it to NULL.
1773 * Used when executing autocommands and for ":source".
1774 */
1775 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 current_funccal = NULL;
1781 return (void *)fc;
1782}
1783
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = (funccall_T *)vfc;
1788
1789 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790}
1791
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792#if defined(FEAT_PROFILE) || defined(PROTO)
1793/*
1794 * Prepare profiling for entering a child or something else that is not
1795 * counted for the script/function itself.
1796 * Should always be called in pair with prof_child_exit().
1797 */
1798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799prof_child_enter(
1800 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801{
1802 funccall_T *fc = current_funccal;
1803
1804 if (fc != NULL && fc->func->uf_profiling)
1805 profile_start(&fc->prof_child);
1806 script_prof_save(tm);
1807}
1808
1809/*
1810 * Take care of time spent in a child.
1811 * Should always be called after prof_child_enter().
1812 */
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814prof_child_exit(
1815 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816{
1817 funccall_T *fc = current_funccal;
1818
1819 if (fc != NULL && fc->func->uf_profiling)
1820 {
1821 profile_end(&fc->prof_child);
1822 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1823 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1824 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1825 }
1826 script_prof_restore(tm);
1827}
1828#endif
1829
1830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_FOLDING
1832/*
1833 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1834 * it in "*cp". Doesn't give error messages.
1835 */
1836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int retval;
1841 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001842 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1843 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001846 if (use_sandbox)
1847 ++sandbox;
1848 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 retval = 0;
1852 else
1853 {
1854 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 if (tv.v_type == VAR_NUMBER)
1856 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001857 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 retval = 0;
1859 else
1860 {
1861 /* If the result is a string, check if there is a non-digit before
1862 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (!VIM_ISDIGIT(*s) && *s != '-')
1865 *cp = *s++;
1866 retval = atol((char *)s);
1867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001871 if (use_sandbox)
1872 --sandbox;
1873 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 return retval;
1876}
1877#endif
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 * ":let" list all variable values
1881 * ":let var1 var2" list variable values
1882 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 * ":let var += expr" assignment command.
1884 * ":let var -= expr" assignment command.
1885 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 int var_count = 0;
1896 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001898 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 argend = skip_var_list(arg, &var_count, &semicolon);
1902 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001904 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1905 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001906 expr = skipwhite(argend);
1907 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1908 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001910 /*
1911 * ":let" without "=": list variables
1912 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 if (*arg == '[')
1914 EMSG(_(e_invarg));
1915 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_glob_vars(&first);
1922 list_buf_vars(&first);
1923 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001926#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_script_vars(&first);
1928 list_func_vars(&first);
1929 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 eap->nextcmd = check_nextcmd(arg);
1932 }
1933 else
1934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 op[0] = '=';
1936 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1940 op[0] = *expr; /* +=, -= or .= */
1941 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 else
1944 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (eap->skip)
1947 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 {
1951 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 --emsg_skip;
1954 }
1955 else if (i != FAIL)
1956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962}
1963
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964/*
1965 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1966 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 * When "nextchars" is not NULL it points to a string with characters that
1968 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1969 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 * Returns OK or FAIL;
1971 */
1972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973ex_let_vars(
1974 char_u *arg_start,
1975 typval_T *tv,
1976 int copy, /* copy values from "tv", don't move */
1977 int semicolon, /* from skip_var_list() */
1978 int var_count, /* from skip_var_list() */
1979 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980{
1981 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 listitem_T *item;
1985 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986
1987 if (*arg != '[')
1988 {
1989 /*
1990 * ":let var = expr" or ":for var in list"
1991 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 return FAIL;
1994 return OK;
1995 }
1996
1997 /*
1998 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1999 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002000 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 {
2002 EMSG(_(e_listreq));
2003 return FAIL;
2004 }
2005
2006 i = list_len(l);
2007 if (semicolon == 0 && var_count < i)
2008 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002009 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 return FAIL;
2011 }
2012 if (var_count - semicolon > i)
2013 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002014 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015 return FAIL;
2016 }
2017
2018 item = l->lv_first;
2019 while (*arg != ']')
2020 {
2021 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 item = item->li_next;
2024 if (arg == NULL)
2025 return FAIL;
2026
2027 arg = skipwhite(arg);
2028 if (*arg == ';')
2029 {
2030 /* Put the rest of the list (may be empty) in the var after ';'.
2031 * Create a new list for this. */
2032 l = list_alloc();
2033 if (l == NULL)
2034 return FAIL;
2035 while (item != NULL)
2036 {
2037 list_append_tv(l, &item->li_tv);
2038 item = item->li_next;
2039 }
2040
2041 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002042 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 ltv.vval.v_list = l;
2044 l->lv_refcount = 1;
2045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2047 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 clear_tv(&ltv);
2049 if (arg == NULL)
2050 return FAIL;
2051 break;
2052 }
2053 else if (*arg != ',' && *arg != ']')
2054 {
2055 EMSG2(_(e_intern2), "ex_let_vars()");
2056 return FAIL;
2057 }
2058 }
2059
2060 return OK;
2061}
2062
2063/*
2064 * Skip over assignable variable "var" or list of variables "[var, var]".
2065 * Used for ":let varvar = expr" and ":for varvar in expr".
2066 * For "[var, var]" increment "*var_count" for each variable.
2067 * for "[var, var; var]" set "semicolon".
2068 * Return NULL for an error.
2069 */
2070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002071skip_var_list(
2072 char_u *arg,
2073 int *var_count,
2074 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075{
2076 char_u *p, *s;
2077
2078 if (*arg == '[')
2079 {
2080 /* "[var, var]": find the matching ']'. */
2081 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002082 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083 {
2084 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2085 s = skip_var_one(p);
2086 if (s == p)
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 ++*var_count;
2092
2093 p = skipwhite(s);
2094 if (*p == ']')
2095 break;
2096 else if (*p == ';')
2097 {
2098 if (*semicolon == 1)
2099 {
2100 EMSG(_("Double ; in list of variables"));
2101 return NULL;
2102 }
2103 *semicolon = 1;
2104 }
2105 else if (*p != ',')
2106 {
2107 EMSG2(_(e_invarg2), p);
2108 return NULL;
2109 }
2110 }
2111 return p + 1;
2112 }
2113 else
2114 return skip_var_one(arg);
2115}
2116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002118 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002119 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002124 if (*arg == '@' && arg[1] != NUL)
2125 return arg + 2;
2126 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2127 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128}
2129
Bram Moolenaara7043832005-01-21 11:56:39 +00002130/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 * List variables for hashtab "ht" with prefix "prefix".
2132 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002135list_hashtable_vars(
2136 hashtab_T *ht,
2137 char_u *prefix,
2138 int empty,
2139 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 hashitem_T *hi;
2142 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 int todo;
2144
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2147 {
2148 if (!HASHITEM_EMPTY(hi))
2149 {
2150 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002151 di = HI2DI(hi);
2152 if (empty || di->di_tv.v_type != VAR_STRING
2153 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 }
2156 }
2157}
2158
2159/*
2160 * List global variables.
2161 */
2162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002164{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List buffer variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 char_u numbuf[NUMBUFLEN];
2175
Bram Moolenaar429fa852013-04-15 12:27:36 +02002176 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178
2179 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2181 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182}
2183
2184/*
2185 * List window variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002192}
2193
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002194#ifdef FEAT_WINDOWS
2195/*
2196 * List tab page variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002201 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203}
2204#endif
2205
Bram Moolenaara7043832005-01-21 11:56:39 +00002206/*
2207 * List Vim variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213}
2214
2215/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216 * List script-local variables, if there is a script.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2223 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
2227 * List function variables, if there is a function.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231{
2232 if (current_funccal != NULL)
2233 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235}
2236
2237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 * List variables in "arg".
2239 */
2240 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
2243 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 char_u *name_start;
2247 char_u *arg_subsc;
2248 char_u *tofree;
2249 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
2251 while (!ends_excmd(*arg) && !got_int)
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2257 {
2258 emsg_severe = TRUE;
2259 EMSG(_(e_trailing));
2260 break;
2261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* get_name_len() takes care of expanding curly braces */
2266 name_start = name = arg;
2267 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2268 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* This is mainly to keep test 49 working: when expanding
2271 * curly braces fails overrule the exception error message. */
2272 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 emsg_severe = TRUE;
2275 EMSG2(_(e_invarg2), arg);
2276 break;
2277 }
2278 error = TRUE;
2279 }
2280 else
2281 {
2282 if (tofree != NULL)
2283 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002284 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
2287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* handle d.key, l[idx], f(expr) */
2289 arg_subsc = arg;
2290 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'g': list_glob_vars(first); break;
2299 case 'b': list_buf_vars(first); break;
2300 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'v': list_vim_vars(first); break;
2305 case 's': list_script_vars(first); break;
2306 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 default:
2308 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
2311 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 {
2313 char_u numbuf[NUMBUFLEN];
2314 char_u *tf;
2315 int c;
2316 char_u *s;
2317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002318 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 c = *arg;
2320 *arg = NUL;
2321 list_one_var_a((char_u *)"",
2322 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 tv.v_type,
2324 s == NULL ? (char_u *)"" : s,
2325 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 *arg = c;
2327 vim_free(tf);
2328 }
2329 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339
2340 return arg;
2341}
2342
2343/*
2344 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2345 * Returns a pointer to the char just after the var name.
2346 * Returns NULL if there is an error.
2347 */
2348 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002349ex_let_one(
2350 char_u *arg, /* points to variable name */
2351 typval_T *tv, /* value to assign to variable */
2352 int copy, /* copy value from "tv" */
2353 char_u *endchars, /* valid chars after variable name or NULL */
2354 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355{
2356 int c1;
2357 char_u *name;
2358 char_u *p;
2359 char_u *arg_end = NULL;
2360 int len;
2361 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
2364 /*
2365 * ":let $VAR = expr": Set environment variable.
2366 */
2367 if (*arg == '$')
2368 {
2369 /* Find the end of the name. */
2370 ++arg;
2371 name = arg;
2372 len = get_env_len(&arg);
2373 if (len == 0)
2374 EMSG2(_(e_invarg2), name - 1);
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 if (op != NULL && (*op == '+' || *op == '-'))
2378 EMSG2(_(e_letwrong), op);
2379 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002382 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
2384 c1 = name[len];
2385 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 p = get_tv_string_chk(tv);
2387 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 {
2389 int mustfree = FALSE;
2390 char_u *s = vim_getenv(name, &mustfree);
2391
2392 if (s != NULL)
2393 {
2394 p = tofree = concat_str(s, p);
2395 if (mustfree)
2396 vim_free(s);
2397 }
2398 }
2399 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 if (STRICMP(name, "HOME") == 0)
2403 init_homedir();
2404 else if (didset_vim && STRICMP(name, "VIM") == 0)
2405 didset_vim = FALSE;
2406 else if (didset_vimruntime
2407 && STRICMP(name, "VIMRUNTIME") == 0)
2408 didset_vimruntime = FALSE;
2409 arg_end = arg;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415 }
2416
2417 /*
2418 * ":let &option = expr": Set option value.
2419 * ":let &l:option = expr": Set local option value.
2420 * ":let &g:option = expr": Set global option value.
2421 */
2422 else if (*arg == '&')
2423 {
2424 /* Find the end of the name. */
2425 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 if (p == NULL || (endchars != NULL
2427 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 long n;
2432 int opt_type;
2433 long numval;
2434 char_u *stringval = NULL;
2435 char_u *s;
2436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 c1 = *p;
2438 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439
2440 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 s = get_tv_string_chk(tv); /* != NULL if number or string */
2442 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 opt_type = get_option_value(arg, &numval,
2445 &stringval, opt_flags);
2446 if ((opt_type == 1 && *op == '.')
2447 || (opt_type == 0 && *op != '.'))
2448 EMSG2(_(e_letwrong), op);
2449 else
2450 {
2451 if (opt_type == 1) /* number */
2452 {
2453 if (*op == '+')
2454 n = numval + n;
2455 else
2456 n = numval - n;
2457 }
2458 else if (opt_type == 0 && stringval != NULL) /* string */
2459 {
2460 s = concat_str(stringval, s);
2461 vim_free(stringval);
2462 stringval = s;
2463 }
2464 }
2465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (s != NULL)
2467 {
2468 set_option_value(arg, n, s, opt_flags);
2469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let @r = expr": Set register contents.
2478 */
2479 else if (*arg == '@')
2480 {
2481 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (op != NULL && (*op == '+' || *op == '-'))
2483 EMSG2(_(e_letwrong), op);
2484 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 EMSG(_(e_letunexp));
2487 else
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 char_u *s;
2491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 p = get_tv_string_chk(tv);
2493 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002495 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 if (s != NULL)
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(s);
2500 }
2501 }
2502 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 arg_end = arg + 1;
2506 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
2509 }
2510
2511 /*
2512 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002519 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2523 EMSG(_(e_letunexp));
2524 else
2525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 arg_end = p;
2528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532
2533 else
2534 EMSG2(_(e_invarg2), arg);
2535
2536 return arg_end;
2537}
2538
2539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2541 */
2542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544{
2545 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2546 {
2547 EMSG2(_(e_readonlyvar), arg);
2548 return TRUE;
2549 }
2550 return FALSE;
2551}
2552
2553/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Get an lval: variable, Dict item or List item that can be assigned a value
2555 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2556 * "name.key", "name.key[expr]" etc.
2557 * Indexing only works if "name" is an existing List or Dictionary.
2558 * "name" points to the start of the name.
2559 * If "rettv" is not NULL it points to the value to be assigned.
2560 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2561 * wrong; must end in space or cmd separator.
2562 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 * flags:
2564 * GLV_QUIET: do not give error messages
2565 * GLV_NO_AUTOLOAD: do not use script autoloading
2566 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002568 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 */
2571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572get_lval(
2573 char_u *name,
2574 typval_T *rettv,
2575 lval_T *lp,
2576 int unlet,
2577 int skip,
2578 int flags, /* GLV_ values */
2579 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 char_u *expr_start, *expr_end;
2583 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 dictitem_T *v;
2585 typval_T var1;
2586 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596
2597 if (skip)
2598 {
2599 /* When skipping just find the end of the name. */
2600 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 }
2603
2604 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (expr_start != NULL)
2607 {
2608 /* Don't expand the name when we already know there is an error. */
2609 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2610 && *p != '[' && *p != '.')
2611 {
2612 EMSG(_(e_trailing));
2613 return NULL;
2614 }
2615
2616 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2617 if (lp->ll_exp_name == NULL)
2618 {
2619 /* Report an invalid expression in braces, unless the
2620 * expression evaluation has been cancelled due to an
2621 * aborting error, an interrupt, or an exception. */
2622 if (!aborting() && !quiet)
2623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002624 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 EMSG2(_(e_invarg2), name);
2626 return NULL;
2627 }
2628 }
2629 lp->ll_name = lp->ll_exp_name;
2630 }
2631 else
2632 lp->ll_name = name;
2633
2634 /* Without [idx] or .key we are done. */
2635 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2636 return p;
2637
2638 cc = *p;
2639 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002640 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (v == NULL && !quiet)
2642 EMSG2(_(e_undefvar), lp->ll_name);
2643 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 if (v == NULL)
2645 return NULL;
2646
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /*
2648 * Loop until no more [idx] or .key is following.
2649 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2654 && !(lp->ll_tv->v_type == VAR_DICT
2655 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E689: Can only index a List or Dictionary"));
2659 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E708: [:] must come last"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 len = -1;
2669 if (*p == '.')
2670 {
2671 key = p + 1;
2672 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2673 ;
2674 if (len == 0)
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_(e_emptykey));
2678 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
2680 p = key + len;
2681 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 else
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 if (*p == ':')
2687 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 empty1 = FALSE;
2691 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 if (get_tv_string_chk(&var1) == NULL)
2694 {
2695 /* not a number or string */
2696 clear_tv(&var1);
2697 return NULL;
2698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Optionally get the second index [ :expr]. */
2702 if (*p == ':')
2703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (rettv != NULL && (rettv->v_type != VAR_LIST
2713 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721 p = skipwhite(p + 1);
2722 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 else
2725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (!empty1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 if (get_tv_string_chk(&var2) == NULL)
2734 {
2735 /* not a number or string */
2736 if (!empty1)
2737 clear_tv(&var1);
2738 clear_tv(&var2);
2739 return NULL;
2740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
2750 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (!empty1)
2752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757
2758 /* Skip to past ']'. */
2759 ++p;
2760 }
2761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
2764 if (len == -1)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (*key == NUL)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 lp->ll_list = NULL;
2777 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 /* When assigning to a scope dictionary check that a function and
2781 * variable name is valid (only variable name unless it is l: or
2782 * g: dictionary). Disallow overwriting a builtin function. */
2783 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 int prevval;
2786 int wrong;
2787
2788 if (len != -1)
2789 {
2790 prevval = key[len];
2791 key[len] = NUL;
2792 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002793 else
2794 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2796 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 || !valid_varname(key);
2799 if (len != -1)
2800 key[len] = prevval;
2801 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002802 return NULL;
2803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 /* Can't add "v:" variable. */
2808 if (lp->ll_dict == &vimvardict)
2809 {
2810 EMSG2(_(e_illvar), name);
2811 return NULL;
2812 }
2813
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 if (len == -1)
2820 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 }
2823 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 p = NULL;
2831 break;
2832 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002834 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 return NULL;
2836
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 }
2841 else
2842 {
2843 /*
2844 * Get the number and item for the only or first index of the List.
2845 */
2846 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 else
2849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002850 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 clear_tv(&var1);
2852 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002853 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_list = lp->ll_tv->vval.v_list;
2855 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2856 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002858 if (lp->ll_n1 < 0)
2859 {
2860 lp->ll_n1 = 0;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 }
2863 }
2864 if (lp->ll_li == NULL)
2865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872
2873 /*
2874 * May need to find the item or absolute index for the second
2875 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * When no index given: "lp->ll_empty2" is TRUE.
2877 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002881 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 {
2888 if (!quiet)
2889 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2896 if (lp->ll_n1 < 0)
2897 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2898 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 {
2900 if (!quiet)
2901 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 return p;
2911}
2912
2913/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 */
2916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002917clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918{
2919 vim_free(lp->ll_exp_name);
2920 vim_free(lp->ll_newkey);
2921}
2922
2923/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002924 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 */
2928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929set_var_lval(
2930 lval_T *lp,
2931 char_u *endp,
2932 typval_T *rettv,
2933 int copy,
2934 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935{
2936 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 listitem_T *ri;
2938 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939
2940 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 cc = *endp;
2945 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002952 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 if ((di == NULL
2956 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2957 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2958 FALSE)))
2959 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 set_var(lp->ll_name, &tv, FALSE);
2961 clear_tv(&tv);
2962 }
2963 }
2964 else
2965 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 else if (tv_check_lock(lp->ll_newkey == NULL
2970 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002971 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 else if (lp->ll_range)
2974 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 listitem_T *ll_li = lp->ll_li;
2976 int ll_n1 = lp->ll_n1;
2977
2978 /*
2979 * Check whether any of the list items is locked
2980 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002981 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002983 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 return;
2985 ri = ri->li_next;
2986 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2987 break;
2988 ll_li = ll_li->li_next;
2989 ++ll_n1;
2990 }
2991
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 /*
2993 * Assign the List values to the list items.
2994 */
2995 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2999 else
3000 {
3001 clear_tv(&lp->ll_li->li_tv);
3002 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 ri = ri->li_next;
3005 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3006 break;
3007 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003010 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 ri = NULL;
3013 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 lp->ll_li = lp->ll_li->li_next;
3017 ++lp->ll_n1;
3018 }
3019 if (ri != NULL)
3020 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 else if (lp->ll_empty2
3022 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 : lp->ll_n1 != lp->ll_n2)
3024 EMSG(_("E711: List value has not enough items"));
3025 }
3026 else
3027 {
3028 /*
3029 * Assign to a List or Dictionary item.
3030 */
3031 if (lp->ll_newkey != NULL)
3032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 {
3035 EMSG2(_(e_letwrong), op);
3036 return;
3037 }
3038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 if (di == NULL)
3042 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3044 {
3045 vim_free(di);
3046 return;
3047 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 else if (op != NULL && *op != '=')
3051 {
3052 tv_op(lp->ll_tv, rettv, op);
3053 return;
3054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 /*
3059 * Assign the value to the variable or list item.
3060 */
3061 if (copy)
3062 copy_tv(rettv, lp->ll_tv);
3063 else
3064 {
3065 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003066 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068 }
3069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070}
3071
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3074 * Returns OK or FAIL.
3075 */
3076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078{
3079 long n;
3080 char_u numbuf[NUMBUFLEN];
3081 char_u *s;
3082
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003083 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3084 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3085 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 {
3087 switch (tv1->v_type)
3088 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 case VAR_DICT:
3091 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003094 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 break;
3096
3097 case VAR_LIST:
3098 if (*op != '+' || tv2->v_type != VAR_LIST)
3099 break;
3100 /* List += List */
3101 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3102 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3103 return OK;
3104
3105 case VAR_NUMBER:
3106 case VAR_STRING:
3107 if (tv2->v_type == VAR_LIST)
3108 break;
3109 if (*op == '+' || *op == '-')
3110 {
3111 /* nr += nr or nr -= nr*/
3112 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113#ifdef FEAT_FLOAT
3114 if (tv2->v_type == VAR_FLOAT)
3115 {
3116 float_T f = n;
3117
3118 if (*op == '+')
3119 f += tv2->vval.v_float;
3120 else
3121 f -= tv2->vval.v_float;
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_FLOAT;
3124 tv1->vval.v_float = f;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127#endif
3128 {
3129 if (*op == '+')
3130 n += get_tv_number(tv2);
3131 else
3132 n -= get_tv_number(tv2);
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_NUMBER;
3135 tv1->vval.v_number = n;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 }
3138 else
3139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (tv2->v_type == VAR_FLOAT)
3141 break;
3142
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 /* str .= str */
3144 s = get_tv_string(tv1);
3145 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3146 clear_tv(tv1);
3147 tv1->v_type = VAR_STRING;
3148 tv1->vval.v_string = s;
3149 }
3150 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003153#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154 {
3155 float_T f;
3156
3157 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3158 && tv2->v_type != VAR_NUMBER
3159 && tv2->v_type != VAR_STRING))
3160 break;
3161 if (tv2->v_type == VAR_FLOAT)
3162 f = tv2->vval.v_float;
3163 else
3164 f = get_tv_number(tv2);
3165 if (*op == '+')
3166 tv1->vval.v_float += f;
3167 else
3168 tv1->vval.v_float -= f;
3169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003171 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 }
3173 }
3174
3175 EMSG2(_(e_letwrong), op);
3176 return FAIL;
3177}
3178
3179/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 * Add a watcher to a list.
3181 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003183list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184{
3185 lw->lw_next = l->lv_watch;
3186 l->lv_watch = lw;
3187}
3188
3189/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003190 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * No warning when it isn't found...
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197
3198 lwp = &l->lv_watch;
3199 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3200 {
3201 if (lw == lwrem)
3202 {
3203 *lwp = lw->lw_next;
3204 break;
3205 }
3206 lwp = &lw->lw_next;
3207 }
3208}
3209
3210/*
3211 * Just before removing an item from a list: advance watchers to the next
3212 * item.
3213 */
3214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3220 if (lw->lw_item == item)
3221 lw->lw_item = item->li_next;
3222}
3223
3224/*
3225 * Evaluate the expression used in a ":for var in expr" command.
3226 * "arg" points to "var".
3227 * Set "*errp" to TRUE for an error, FALSE otherwise;
3228 * Return a pointer that holds the info. Null when there is an error.
3229 */
3230 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231eval_for_line(
3232 char_u *arg,
3233 int *errp,
3234 char_u **nextcmdp,
3235 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 typval_T tv;
3240 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 *errp = TRUE; /* default: there is an error */
3243
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 if (fi == NULL)
3246 return NULL;
3247
3248 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3249 if (expr == NULL)
3250 return fi;
3251
3252 expr = skipwhite(expr);
3253 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3254 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003255 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 return fi;
3257 }
3258
3259 if (skip)
3260 ++emsg_skip;
3261 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3262 {
3263 *errp = FALSE;
3264 if (!skip)
3265 {
3266 l = tv.vval.v_list;
3267 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 clear_tv(&tv);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 else
3273 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003274 /* No need to increment the refcount, it's already set for the
3275 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 fi->fi_list = l;
3277 list_add_watch(l, &fi->fi_lw);
3278 fi->fi_lw.lw_item = l->lv_first;
3279 }
3280 }
3281 }
3282 if (skip)
3283 --emsg_skip;
3284
3285 return fi;
3286}
3287
3288/*
3289 * Use the first item in a ":for" list. Advance to the next.
3290 * Assign the values to the variable (list). "arg" points to the first one.
3291 * Return TRUE when a valid item was found, FALSE when at end of list or
3292 * something wrong.
3293 */
3294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003321 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 list_unref(fi->fi_list);
3325 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 vim_free(fi);
3327}
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3330
3331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332set_context_for_expression(
3333 expand_T *xp,
3334 char_u *arg,
3335 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 int got_eq = FALSE;
3338 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 if (cmdidx == CMD_let)
3342 {
3343 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (vim_iswhite(*p))
3352 break;
3353 }
3354 return;
3355 }
3356 }
3357 else
3358 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3359 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 while ((xp->xp_pattern = vim_strpbrk(arg,
3361 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3362 {
3363 c = *xp->xp_pattern;
3364 if (c == '&')
3365 {
3366 c = xp->xp_pattern[1];
3367 if (c == '&')
3368 {
3369 ++xp->xp_pattern;
3370 xp->xp_context = cmdidx != CMD_let || got_eq
3371 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3372 }
3373 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3377 xp->xp_pattern += 2;
3378
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else if (c == '$')
3382 {
3383 /* environment variable */
3384 xp->xp_context = EXPAND_ENV_VARS;
3385 }
3386 else if (c == '=')
3387 {
3388 got_eq = TRUE;
3389 xp->xp_context = EXPAND_EXPRESSION;
3390 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 && xp->xp_context == EXPAND_FUNCTIONS
3393 && vim_strchr(xp->xp_pattern, '(') == NULL)
3394 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397 }
3398 else if (cmdidx != CMD_let || got_eq)
3399 {
3400 if (c == '"') /* string */
3401 {
3402 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3403 if (c == '\\' && xp->xp_pattern[1] != NUL)
3404 ++xp->xp_pattern;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '\'') /* literal string */
3408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003409 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3411 /* skip */ ;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '|')
3415 {
3416 if (xp->xp_pattern[1] == '|')
3417 {
3418 ++xp->xp_pattern;
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
3422 xp->xp_context = EXPAND_COMMANDS;
3423 }
3424 else
3425 xp->xp_context = EXPAND_EXPRESSION;
3426 }
3427 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003428 /* Doesn't look like something valid, expand as an expression
3429 * anyway. */
3430 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 arg = xp->xp_pattern;
3432 if (*arg != NUL)
3433 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3434 /* skip */ ;
3435 }
3436 xp->xp_pattern = arg;
3437}
3438
3439#endif /* FEAT_CMDL_COMPL */
3440
3441/*
3442 * ":1,25call func(arg1, arg2)" function call.
3443 */
3444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003458 if (eap->skip)
3459 {
3460 /* trans_function_name() doesn't work well when skipping, use eval0()
3461 * instead to skip to any following command, e.g. for:
3462 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003463 ++emsg_skip;
3464 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3465 clear_tv(&rettv);
3466 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 return;
3468 }
3469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003470 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003471 if (fudi.fd_newkey != NULL)
3472 {
3473 /* Still need to give an error message for missing key. */
3474 EMSG2(_(e_dictkey), fudi.fd_newkey);
3475 vim_free(fudi.fd_newkey);
3476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 if (tofree == NULL)
3478 return;
3479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 /* Increase refcount on dictionary, it could get deleted when evaluating
3481 * the arguments. */
3482 if (fudi.fd_dict != NULL)
3483 ++fudi.fd_dict->dv_refcount;
3484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003487 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 /* Skip white space to allow ":call func ()". Not good, but required for
3490 * backward compatibility. */
3491 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 if (*startarg != '(')
3495 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003496 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 goto end;
3498 }
3499
3500 /*
3501 * When skipping, evaluate the function once, to find the end of the
3502 * arguments.
3503 * When the function takes a range, this is discovered after the first
3504 * call, and the loop is broken.
3505 */
3506 if (eap->skip)
3507 {
3508 ++emsg_skip;
3509 lnum = eap->line2; /* do it once, also with an invalid range */
3510 }
3511 else
3512 lnum = eap->line1;
3513 for ( ; lnum <= eap->line2; ++lnum)
3514 {
3515 if (!eap->skip && eap->addr_count > 0)
3516 {
3517 curwin->w_cursor.lnum = lnum;
3518 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003519#ifdef FEAT_VIRTUALEDIT
3520 curwin->w_cursor.coladd = 0;
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003525 eap->line1, eap->line2, &doesrange,
3526 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 failed = TRUE;
3529 break;
3530 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
3532 /* Handle a function returning a Funcref, Dictionary or List. */
3533 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3534 {
3535 failed = TRUE;
3536 break;
3537 }
3538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (doesrange || eap->skip)
3541 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (aborting())
3548 break;
3549 }
3550 if (eap->skip)
3551 --emsg_skip;
3552
3553 if (!failed)
3554 {
3555 /* Check for trailing illegal characters and a following command. */
3556 if (!ends_excmd(*arg))
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 else
3562 eap->nextcmd = check_nextcmd(arg);
3563 }
3564
3565end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003566 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570/*
3571 * ":unlet[!] var1 ... " command.
3572 */
3573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 ex_unletlock(eap, eap->arg, 0);
3577}
3578
3579/*
3580 * ":lockvar" and ":unlockvar" commands
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 int deep = 2;
3587
3588 if (eap->forceit)
3589 deep = -1;
3590 else if (vim_isdigit(*arg))
3591 {
3592 deep = getdigits(&arg);
3593 arg = skipwhite(arg);
3594 }
3595
3596 ex_unletlock(eap, arg, deep);
3597}
3598
3599/*
3600 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3601 */
3602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ex_unletlock(
3604 exarg_T *eap,
3605 char_u *argstart,
3606 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607{
3608 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 do
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003616 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003617 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lv.ll_name == NULL)
3619 error = TRUE; /* error but continue parsing */
3620 if (name_end == NULL || (!vim_iswhite(*name_end)
3621 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (name_end != NULL)
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 if (!(eap->skip || error))
3629 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 break;
3631 }
3632
3633 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 {
3635 if (eap->cmdidx == CMD_unlet)
3636 {
3637 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3638 error = TRUE;
3639 }
3640 else
3641 {
3642 if (do_lock_var(&lv, name_end, deep,
3643 eap->cmdidx == CMD_lockvar) == FAIL)
3644 error = TRUE;
3645 }
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 if (!eap->skip)
3649 clear_lval(&lv);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 arg = skipwhite(name_end);
3652 } while (!ends_excmd(*arg));
3653
3654 eap->nextcmd = check_nextcmd(arg);
3655}
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658do_unlet_var(
3659 lval_T *lp,
3660 char_u *name_end,
3661 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 int ret = OK;
3664 int cc;
3665
3666 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 cc = *name_end;
3669 *name_end = NUL;
3670
3671 /* Normal name or expanded name. */
3672 if (check_changedtick(lp->ll_name))
3673 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 else if (lp->ll_range)
3684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003687 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688
3689 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3690 {
3691 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 return FAIL;
3694 ll_li = li;
3695 ++ll_n1;
3696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697
3698 /* Delete a range of List items. */
3699 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3700 {
3701 li = lp->ll_li->li_next;
3702 listitem_remove(lp->ll_list, lp->ll_li);
3703 lp->ll_li = li;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else
3708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a List item. */
3711 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003714 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 }
3716
3717 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718}
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaar33570922005-01-25 22:26:29 +00003727 hashtab_T *ht;
3728 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003729 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003731 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 ht = find_var_ht(name, &varname);
3734 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003736 if (ht == &globvarht)
3737 d = &globvardict;
3738 else if (current_funccal != NULL
3739 && ht == &current_funccal->l_vars.dv_hashtab)
3740 d = &current_funccal->l_vars;
3741 else if (ht == &compat_hashtab)
3742 d = &vimvardict;
3743 else
3744 {
3745 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3746 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3747 }
3748 if (d == NULL)
3749 {
3750 EMSG2(_(e_intern2), "do_unlet()");
3751 return FAIL;
3752 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003753 hi = hash_find(ht, varname);
3754 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003755 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003756 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003757 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003758 || var_check_ro(di->di_flags, name, FALSE)
3759 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003760 return FAIL;
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 delete_var(ht, hi);
3763 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 if (forceit)
3767 return OK;
3768 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770}
3771
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772/*
3773 * Lock or unlock variable indicated by "lp".
3774 * "deep" is the levels to go (-1 for unlimited);
3775 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3776 */
3777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_lock_var(
3779 lval_T *lp,
3780 char_u *name_end,
3781 int deep,
3782 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783{
3784 int ret = OK;
3785 int cc;
3786 dictitem_T *di;
3787
3788 if (deep == 0) /* nothing to do */
3789 return OK;
3790
3791 if (lp->ll_tv == NULL)
3792 {
3793 cc = *name_end;
3794 *name_end = NUL;
3795
3796 /* Normal name or expanded name. */
3797 if (check_changedtick(lp->ll_name))
3798 ret = FAIL;
3799 else
3800 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003801 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 if (di == NULL)
3803 ret = FAIL;
3804 else
3805 {
3806 if (lock)
3807 di->di_flags |= DI_FLAGS_LOCK;
3808 else
3809 di->di_flags &= ~DI_FLAGS_LOCK;
3810 item_lock(&di->di_tv, deep, lock);
3811 }
3812 }
3813 *name_end = cc;
3814 }
3815 else if (lp->ll_range)
3816 {
3817 listitem_T *li = lp->ll_li;
3818
3819 /* (un)lock a range of List items. */
3820 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3821 {
3822 item_lock(&li->li_tv, deep, lock);
3823 li = li->li_next;
3824 ++lp->ll_n1;
3825 }
3826 }
3827 else if (lp->ll_list != NULL)
3828 /* (un)lock a List item. */
3829 item_lock(&lp->ll_li->li_tv, deep, lock);
3830 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003831 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003832 item_lock(&lp->ll_di->di_tv, deep, lock);
3833
3834 return ret;
3835}
3836
3837/*
3838 * Lock or unlock an item. "deep" is nr of levels to go.
3839 */
3840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003841item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003842{
3843 static int recurse = 0;
3844 list_T *l;
3845 listitem_T *li;
3846 dict_T *d;
3847 hashitem_T *hi;
3848 int todo;
3849
3850 if (recurse >= DICT_MAXNEST)
3851 {
3852 EMSG(_("E743: variable nested too deep for (un)lock"));
3853 return;
3854 }
3855 if (deep == 0)
3856 return;
3857 ++recurse;
3858
3859 /* lock/unlock the item itself */
3860 if (lock)
3861 tv->v_lock |= VAR_LOCKED;
3862 else
3863 tv->v_lock &= ~VAR_LOCKED;
3864
3865 switch (tv->v_type)
3866 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003867 case VAR_UNKNOWN:
3868 case VAR_NUMBER:
3869 case VAR_STRING:
3870 case VAR_FUNC:
3871 case VAR_FLOAT:
3872 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003873 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003874 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003875 break;
3876
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003877 case VAR_LIST:
3878 if ((l = tv->vval.v_list) != NULL)
3879 {
3880 if (lock)
3881 l->lv_lock |= VAR_LOCKED;
3882 else
3883 l->lv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 /* recursive: lock/unlock the items the List contains */
3886 for (li = l->lv_first; li != NULL; li = li->li_next)
3887 item_lock(&li->li_tv, deep - 1, lock);
3888 }
3889 break;
3890 case VAR_DICT:
3891 if ((d = tv->vval.v_dict) != NULL)
3892 {
3893 if (lock)
3894 d->dv_lock |= VAR_LOCKED;
3895 else
3896 d->dv_lock &= ~VAR_LOCKED;
3897 if (deep < 0 || deep > 1)
3898 {
3899 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003900 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003901 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3902 {
3903 if (!HASHITEM_EMPTY(hi))
3904 {
3905 --todo;
3906 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3907 }
3908 }
3909 }
3910 }
3911 }
3912 --recurse;
3913}
3914
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003916 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3917 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918 */
3919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003964static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 int len;
3976
3977 len = (int)STRLEN(name) + 3;
3978 if (len > varnamebuflen)
3979 {
3980 vim_free(varnamebuf);
3981 len += 10; /* some additional space */
3982 varnamebuf = alloc(len);
3983 if (varnamebuf == NULL)
3984 {
3985 varnamebuflen = 0;
3986 return NULL;
3987 }
3988 varnamebuflen = len;
3989 }
3990 *varnamebuf = prefix;
3991 varnamebuf[1] = ':';
3992 STRCPY(varnamebuf + 2, name);
3993 return varnamebuf;
3994}
3995
3996/*
3997 * Function given to ExpandGeneric() to obtain the list of user defined
3998 * (global/buffer/window/built-in) variable names.
3999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static long_u gdone;
4004 static long_u bdone;
4005 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006#ifdef FEAT_WINDOWS
4007 static long_u tdone;
4008#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 static int vidx;
4010 static hashitem_T *hi;
4011 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016#ifdef FEAT_WINDOWS
4017 tdone = 0;
4018#endif
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* Global variables */
4022 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 else
4027 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 while (HASHITEM_EMPTY(hi))
4029 ++hi;
4030 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4031 return cat_prefix_varname('g', hi->hi_key);
4032 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return (char_u *)"b:changedtick";
4051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004057 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066#ifdef FEAT_WINDOWS
4067 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (tdone < ht->ht_used)
4070 {
4071 if (tdone++ == 0)
4072 hi = ht->ht_array;
4073 else
4074 ++hi;
4075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('t', hi->hi_key);
4078 }
4079#endif
4080
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 /* v: variables */
4082 if (vidx < VV_LEN)
4083 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 vim_free(varnamebuf);
4086 varnamebuf = NULL;
4087 varnamebuflen = 0;
4088 return NULL;
4089}
4090
4091#endif /* FEAT_CMDL_COMPL */
4092
4093/*
4094 * types for expressions.
4095 */
4096typedef enum
4097{
4098 TYPE_UNKNOWN = 0
4099 , TYPE_EQUAL /* == */
4100 , TYPE_NEQUAL /* != */
4101 , TYPE_GREATER /* > */
4102 , TYPE_GEQUAL /* >= */
4103 , TYPE_SMALLER /* < */
4104 , TYPE_SEQUAL /* <= */
4105 , TYPE_MATCH /* =~ */
4106 , TYPE_NOMATCH /* !~ */
4107} exptype_T;
4108
4109/*
4110 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4113 */
4114
4115/*
4116 * Handle zero level expression.
4117 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004119 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123eval0(
4124 char_u *arg,
4125 typval_T *rettv,
4126 char_u **nextcmd,
4127 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 int ret;
4130 char_u *p;
4131
4132 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (ret == FAIL || !ends_excmd(*p))
4135 {
4136 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 /*
4139 * Report the invalid expression unless the expression evaluation has
4140 * been cancelled due to an aborting error, an interrupt, or an
4141 * exception.
4142 */
4143 if (!aborting())
4144 EMSG2(_(e_invexpr2), arg);
4145 ret = FAIL;
4146 }
4147 if (nextcmd != NULL)
4148 *nextcmd = check_nextcmd(p);
4149
4150 return ret;
4151}
4152
4153/*
4154 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004155 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004160 * Note: "rettv.v_lock" is not set.
4161 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Return OK or FAIL.
4163 */
4164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 if ((*arg)[0] == '?')
4177 {
4178 result = FALSE;
4179 if (evaluate)
4180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 int error = FALSE;
4182
4183 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 /*
4191 * Get the second variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Check for the ":".
4199 */
4200 if ((*arg)[0] != ':')
4201 {
4202 EMSG(_("E109: Missing ':' after '?'"));
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 /*
4209 * Get the third variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4213 {
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle first level expression:
4227 * expr2 || expr2 || expr2 logical OR
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 long result;
4239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 /*
4249 * Repeat until there is no following "||".
4250 */
4251 first = TRUE;
4252 result = FALSE;
4253 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4254 {
4255 if (evaluate && first)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 first = FALSE;
4263 }
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 2);
4269 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4270 return FAIL;
4271
4272 /*
4273 * Compute the result.
4274 */
4275 if (evaluate && !result)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (evaluate)
4284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * Handle second level expression:
4295 * expr3 && expr3 && expr3 logical AND
4296 *
4297 * "arg" must point to the first non-white of the expression.
4298 * "arg" is advanced to the next non-white after the recognized expression.
4299 *
4300 * Return OK or FAIL.
4301 */
4302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004303eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 long result;
4307 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * Get the first variable.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315
4316 /*
4317 * Repeat until there is no following "&&".
4318 */
4319 first = TRUE;
4320 result = TRUE;
4321 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4322 {
4323 if (evaluate && first)
4324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (error)
4329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 first = FALSE;
4331 }
4332
4333 /*
4334 * Get the second variable.
4335 */
4336 *arg = skipwhite(*arg + 2);
4337 if (eval4(arg, &var2, evaluate && result) == FAIL)
4338 return FAIL;
4339
4340 /*
4341 * Compute the result.
4342 */
4343 if (evaluate && result)
4344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (error)
4349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 if (evaluate)
4352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 rettv->v_type = VAR_NUMBER;
4354 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 }
4357
4358 return OK;
4359}
4360
4361/*
4362 * Handle third level expression:
4363 * var1 == var2
4364 * var1 =~ var2
4365 * var1 != var2
4366 * var1 !~ var2
4367 * var1 > var2
4368 * var1 >= var2
4369 * var1 < var2
4370 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004371 * var1 is var2
4372 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *
4374 * "arg" must point to the first non-white of the expression.
4375 * "arg" is advanced to the next non-white after the recognized expression.
4376 *
4377 * Return OK or FAIL.
4378 */
4379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004380eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004434 i = p[len];
4435 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 {
4437 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4438 type_is = TRUE;
4439 }
4440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 if (type != TYPE_UNKNOWN)
4448 {
4449 /* extra question mark appended: ignore case */
4450 if (p[len] == '?')
4451 {
4452 ic = TRUE;
4453 ++len;
4454 }
4455 /* extra '#' appended: match case */
4456 else if (p[len] == '#')
4457 {
4458 ic = FALSE;
4459 ++len;
4460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 ic = p_ic;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(p + len);
4469 if (eval5(arg, &var2, evaluate) == FAIL)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473 }
4474
4475 if (evaluate)
4476 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 if (type_is && rettv->v_type != var2.v_type)
4478 {
4479 /* For "is" a different type always means FALSE, for "notis"
4480 * it means TRUE. */
4481 n1 = (type == TYPE_NEQUAL);
4482 }
4483 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_list == var2.vval.v_list);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004496 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004498 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4507 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4514 {
4515 if (type_is)
4516 {
4517 n1 = (rettv->v_type == var2.v_type
4518 && rettv->vval.v_dict == var2.vval.v_dict);
4519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 else if (rettv->v_type != var2.v_type
4523 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4524 {
4525 if (rettv->v_type != var2.v_type)
4526 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4527 else
4528 EMSG(_("E736: Invalid operation for Dictionary"));
4529 clear_tv(rettv);
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
4533 else
4534 {
4535 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004536 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4537 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 }
4542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4544 {
4545 if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Funcrefs for being equal or unequal. */
4559 if (rettv->vval.v_string == NULL
4560 || var2.vval.v_string == NULL)
4561 n1 = FALSE;
4562 else
4563 n1 = STRCMP(rettv->vval.v_string,
4564 var2.vval.v_string) == 0;
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004687eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869eval6(
4870 char_u **arg,
4871 typval_T *rettv,
4872 int evaluate,
4873 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051eval7(
5052 char_u **arg,
5053 typval_T *rettv,
5054 int evaluate,
5055 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005134 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
5231 /* If "s" is the name of a variable of type VAR_FUNC
5232 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005233 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
5235 /* Invoke the function. */
5236 ret = get_func_tv(s, len, rettv, arg,
5237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005239
5240 /* If evaluate is FALSE rettv->v_type was not set in
5241 * get_func_tv, but it's needed in handle_subscript() to parse
5242 * what follows. So set it here. */
5243 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5244 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005245 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246 rettv->v_type = VAR_FUNC;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 /* Stop the expression evaluation when immediately
5250 * aborting on error, or when an interrupt occurred or
5251 * an exception was thrown but not caught. */
5252 if (aborting())
5253 {
5254 if (ret == OK)
5255 clear_tv(rettv);
5256 ret = FAIL;
5257 }
5258 }
5259 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005260 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005261 else
5262 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005264 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *arg = skipwhite(*arg);
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5270 * expr(expr). */
5271 if (ret == OK)
5272 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 /*
5275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5276 */
5277 if (ret == OK && evaluate && end_leader > start_leader)
5278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 int val = 0;
5281#ifdef FEAT_FLOAT
5282 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 if (rettv->v_type == VAR_FLOAT)
5285 f = rettv->vval.v_float;
5286 else
5287#endif
5288 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 clear_tv(rettv);
5292 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 else
5295 {
5296 while (end_leader > start_leader)
5297 {
5298 --end_leader;
5299 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = !f;
5304 else
5305#endif
5306 val = !val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = -f;
5313 else
5314#endif
5315 val = -val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 {
5321 clear_tv(rettv);
5322 rettv->vval.v_float = f;
5323 }
5324 else
5325#endif
5326 {
5327 clear_tv(rettv);
5328 rettv->v_type = VAR_NUMBER;
5329 rettv->vval.v_number = val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 return ret;
5335}
5336
5337/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5339 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5341 */
5342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343eval_index(
5344 char_u **arg,
5345 typval_T *rettv,
5346 int evaluate,
5347 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
5349 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 long len = -1;
5353 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 case VAR_FUNC:
5360 if (verbose)
5361 EMSG(_("E695: Cannot index a Funcref"));
5362 return FAIL;
5363 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 if (verbose)
5366 EMSG(_(e_float_as_string));
5367 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005370 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005371 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 if (verbose)
5373 EMSG(_("E909: Cannot index a special variable"));
5374 return FAIL;
5375 case VAR_UNKNOWN:
5376 if (evaluate)
5377 return FAIL;
5378 /* FALLTHROUGH */
5379
5380 case VAR_STRING:
5381 case VAR_NUMBER:
5382 case VAR_LIST:
5383 case VAR_DICT:
5384 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005387 init_tv(&var1);
5388 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * dict.name
5393 */
5394 key = *arg + 1;
5395 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5396 ;
5397 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 /*
5404 * something[idx]
5405 *
5406 * Get the (first) variable from inside the [].
5407 */
5408 *arg = skipwhite(*arg + 1);
5409 if (**arg == ':')
5410 empty1 = TRUE;
5411 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5412 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5414 {
5415 /* not a number or string */
5416 clear_tv(&var1);
5417 return FAIL;
5418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
5420 /*
5421 * Get the second variable from inside the [:].
5422 */
5423 if (**arg == ':')
5424 {
5425 range = TRUE;
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ']')
5428 empty2 = TRUE;
5429 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 if (!empty1)
5432 clear_tv(&var1);
5433 return FAIL;
5434 }
5435 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5436 {
5437 /* not a number or string */
5438 if (!empty1)
5439 clear_tv(&var1);
5440 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 return FAIL;
5442 }
5443 }
5444
5445 /* Check for the ']'. */
5446 if (**arg != ']')
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 clear_tv(&var1);
5451 if (range)
5452 clear_tv(&var2);
5453 return FAIL;
5454 }
5455 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457
5458 if (evaluate)
5459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 n1 = 0;
5461 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 n1 = get_tv_number(&var1);
5464 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466 if (range)
5467 {
5468 if (empty2)
5469 n2 = -1;
5470 else
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n2 = get_tv_number(&var2);
5473 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 }
5476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005480 case VAR_FUNC:
5481 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_SPECIAL:
5483 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005484 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 break; /* not evaluating, skipping over subscript */
5486
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 case VAR_NUMBER:
5488 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 len = (long)STRLEN(s);
5491 if (range)
5492 {
5493 /* The resulting variable is a substring. If the indexes
5494 * are out of range the result is empty. */
5495 if (n1 < 0)
5496 {
5497 n1 = len + n1;
5498 if (n1 < 0)
5499 n1 = 0;
5500 }
5501 if (n2 < 0)
5502 n2 = len + n2;
5503 else if (n2 >= len)
5504 n2 = len;
5505 if (n1 >= len || n2 < 0 || n1 > n2)
5506 s = NULL;
5507 else
5508 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5509 }
5510 else
5511 {
5512 /* The resulting variable is a string of a single
5513 * character. If the index is too big or negative the
5514 * result is empty. */
5515 if (n1 >= len || n1 < 0)
5516 s = NULL;
5517 else
5518 s = vim_strnsave(s + n1, 1);
5519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 clear_tv(rettv);
5521 rettv->v_type = VAR_STRING;
5522 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 break;
5524
5525 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 if (n1 < 0)
5528 n1 = len + n1;
5529 if (!empty1 && (n1 < 0 || n1 >= len))
5530 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 /* For a range we allow invalid values and return an empty
5532 * list. A list index out of range is an error. */
5533 if (!range)
5534 {
5535 if (verbose)
5536 EMSGN(_(e_listidx), n1);
5537 return FAIL;
5538 }
5539 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 }
5541 if (range)
5542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 list_T *l;
5544 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545
5546 if (n2 < 0)
5547 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005548 else if (n2 >= len)
5549 n2 = len - 1;
5550 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005551 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 l = list_alloc();
5553 if (l == NULL)
5554 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 n1 <= n2; ++n1)
5557 {
5558 if (list_append_tv(l, &item->li_tv) == FAIL)
5559 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005560 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 return FAIL;
5562 }
5563 item = item->li_next;
5564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 clear_tv(rettv);
5566 rettv->v_type = VAR_LIST;
5567 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005568 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 else
5571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005572 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 clear_tv(rettv);
5574 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 }
5576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577
5578 case VAR_DICT:
5579 if (range)
5580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (verbose)
5582 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (len == -1)
5584 clear_tv(&var1);
5585 return FAIL;
5586 }
5587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589
5590 if (len == -1)
5591 {
5592 key = get_tv_string(&var1);
5593 if (*key == NUL)
5594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005595 if (verbose)
5596 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 clear_tv(&var1);
5598 return FAIL;
5599 }
5600 }
5601
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005602 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005605 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 if (len == -1)
5607 clear_tv(&var1);
5608 if (item == NULL)
5609 return FAIL;
5610
5611 copy_tv(&item->di_tv, &var1);
5612 clear_tv(rettv);
5613 *rettv = var1;
5614 }
5615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 }
5617 }
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return OK;
5620}
5621
5622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 * Get an option value.
5624 * "arg" points to the '&' or '+' before the option name.
5625 * "arg" is advanced to character after the option name.
5626 * Return OK or FAIL.
5627 */
5628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629get_option_tv(
5630 char_u **arg,
5631 typval_T *rettv, /* when NULL, only check if option exists */
5632 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 char_u *option_end;
5635 long numval;
5636 char_u *stringval;
5637 int opt_type;
5638 int c;
5639 int working = (**arg == '+'); /* has("+option") */
5640 int ret = OK;
5641 int opt_flags;
5642
5643 /*
5644 * Isolate the option name and find its value.
5645 */
5646 option_end = find_option_end(arg, &opt_flags);
5647 if (option_end == NULL)
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 EMSG2(_("E112: Option name missing: %s"), *arg);
5651 return FAIL;
5652 }
5653
5654 if (!evaluate)
5655 {
5656 *arg = option_end;
5657 return OK;
5658 }
5659
5660 c = *option_end;
5661 *option_end = NUL;
5662 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 if (opt_type == -3) /* invalid name */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 EMSG2(_("E113: Unknown option: %s"), *arg);
5669 ret = FAIL;
5670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 if (opt_type == -2) /* hidden string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else if (opt_type == -1) /* hidden number option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_NUMBER;
5681 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else if (opt_type == 1) /* number option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_NUMBER;
5686 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else /* string option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_STRING;
5691 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 }
5694 else if (working && (opt_type == -2 || opt_type == -1))
5695 ret = FAIL;
5696
5697 *option_end = c; /* put back for error messages */
5698 *arg = option_end;
5699
5700 return ret;
5701}
5702
5703/*
5704 * Allocate a variable for a string constant.
5705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 char_u *p;
5711 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int extra = 0;
5713
5714 /*
5715 * Find the end of the string, skipping backslashed characters.
5716 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (*p == '\\' && p[1] != NUL)
5720 {
5721 ++p;
5722 /* A "\<x>" form occupies at least 4 characters, and produces up
5723 * to 6 characters: reserve space for 2 extra */
5724 if (*p == '<')
5725 extra += 2;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728
5729 if (*p != '"')
5730 {
5731 EMSG2(_("E114: Missing quote: %s"), *arg);
5732 return FAIL;
5733 }
5734
5735 /* If only parsing, set *arg and return here */
5736 if (!evaluate)
5737 {
5738 *arg = p + 1;
5739 return OK;
5740 }
5741
5742 /*
5743 * Copy the string into allocated memory, handling backslashed
5744 * characters.
5745 */
5746 name = alloc((unsigned)(p - *arg + extra));
5747 if (name == NULL)
5748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 rettv->v_type = VAR_STRING;
5750 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (*p == '\\')
5755 {
5756 switch (*++p)
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 case 'b': *name++ = BS; ++p; break;
5759 case 'e': *name++ = ESC; ++p; break;
5760 case 'f': *name++ = FF; ++p; break;
5761 case 'n': *name++ = NL; ++p; break;
5762 case 'r': *name++ = CAR; ++p; break;
5763 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 case 'X': /* hex: "\x1", "\x12" */
5766 case 'x':
5767 case 'u': /* Unicode: "\u0023" */
5768 case 'U':
5769 if (vim_isxdigit(p[1]))
5770 {
5771 int n, nr;
5772 int c = toupper(*p);
5773
5774 if (c == 'X')
5775 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else
5779 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 nr = 0;
5781 while (--n >= 0 && vim_isxdigit(p[1]))
5782 {
5783 ++p;
5784 nr = (nr << 4) + hex2nr(*p);
5785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787#ifdef FEAT_MBYTE
5788 /* For "\u" store the number according to
5789 * 'encoding'. */
5790 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797
5798 /* octal: "\1", "\12", "\123" */
5799 case '0':
5800 case '1':
5801 case '2':
5802 case '3':
5803 case '4':
5804 case '5':
5805 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '7': *name = *p++ - '0';
5807 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 *name = (*name << 3) + *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
5811 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815
5816 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (extra != 0)
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 /* FALLTHROUGH */
5824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 }
5829 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 *arg = p + 1;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return OK;
5837}
5838
5839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return OK or FAIL.
5842 */
5843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 int reduce = 0;
5849
5850 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 */
5853 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 break;
5859 ++reduce;
5860 ++p;
5861 }
5862 }
5863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return FAIL;
5868 }
5869
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 if (!evaluate)
5872 {
5873 *arg = p + 1;
5874 return OK;
5875 }
5876
5877 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 */
5880 str = alloc((unsigned)((p - *arg) - reduce));
5881 if (str == NULL)
5882 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 break;
5892 ++p;
5893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 *arg = p + 1;
5898
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 return OK;
5900}
5901
5902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 * Allocate a variable for a List and fill it from "*arg".
5904 * Return OK or FAIL.
5905 */
5906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 list_T *l = NULL;
5910 typval_T tv;
5911 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912
5913 if (evaluate)
5914 {
5915 l = list_alloc();
5916 if (l == NULL)
5917 return FAIL;
5918 }
5919
5920 *arg = skipwhite(*arg + 1);
5921 while (**arg != ']' && **arg != NUL)
5922 {
5923 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5924 goto failret;
5925 if (evaluate)
5926 {
5927 item = listitem_alloc();
5928 if (item != NULL)
5929 {
5930 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005931 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 list_append(l, item);
5933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 else
5935 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 }
5937
5938 if (**arg == ']')
5939 break;
5940 if (**arg != ',')
5941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 goto failret;
5944 }
5945 *arg = skipwhite(*arg + 1);
5946 }
5947
5948 if (**arg != ']')
5949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951failret:
5952 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005953 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 return FAIL;
5955 }
5956
5957 *arg = skipwhite(*arg + 1);
5958 if (evaluate)
5959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 rettv->v_type = VAR_LIST;
5961 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 ++l->lv_refcount;
5963 }
5964
5965 return OK;
5966}
5967
5968/*
5969 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005970 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005972 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 list_T *l;
5976
5977 l = (list_T *)alloc_clear(sizeof(list_T));
5978 if (l != NULL)
5979 {
5980 /* Prepend the list to the list of lists for garbage collection. */
5981 if (first_list != NULL)
5982 first_list->lv_used_prev = l;
5983 l->lv_used_prev = NULL;
5984 l->lv_used_next = first_list;
5985 first_list = l;
5986 }
5987 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988}
5989
5990/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005991 * Allocate an empty list for a return value.
5992 * Returns OK or FAIL.
5993 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996{
5997 list_T *l = list_alloc();
5998
5999 if (l == NULL)
6000 return FAIL;
6001
6002 rettv->vval.v_list = l;
6003 rettv->v_type = VAR_LIST;
6004 ++l->lv_refcount;
6005 return OK;
6006}
6007
6008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Unreference a list: decrement the reference count and free it when it
6010 * becomes zero.
6011 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (l != NULL && --l->lv_refcount <= 0)
6016 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006020 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Ignores the reference count.
6022 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024list_free(
6025 list_T *l,
6026 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the list from the list of lists for garbage collection. */
6031 if (l->lv_used_prev == NULL)
6032 first_list = l->lv_used_next;
6033 else
6034 l->lv_used_prev->lv_used_next = l->lv_used_next;
6035 if (l->lv_used_next != NULL)
6036 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6037
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 /* Remove the item before deleting it. */
6041 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (recurse || (item->li_tv.v_type != VAR_LIST
6043 && item->li_tv.v_type != VAR_DICT))
6044 clear_tv(&item->li_tv);
6045 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 }
6047 vim_free(l);
6048}
6049
6050/*
6051 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006052 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006055listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058}
6059
6060/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006061 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006066 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 vim_free(item);
6068}
6069
6070/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 * Remove a list item from a List and free it. Also clears the value.
6072 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006076 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077 listitem_free(item);
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Get the number of items in a list.
6082 */
6083 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (l == NULL)
6087 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE when two lists have exactly the same values.
6093 */
6094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_equal(
6096 list_T *l1,
6097 list_T *l2,
6098 int ic, /* ignore case for strings */
6099 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006103 if (l1 == NULL || l2 == NULL)
6104 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 if (l1 == l2)
6106 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 if (list_len(l1) != list_len(l2))
6108 return FALSE;
6109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 for (item1 = l1->lv_first, item2 = l2->lv_first;
6111 item1 != NULL && item2 != NULL;
6112 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return FALSE;
6115 return item1 == NULL && item2 == NULL;
6116}
6117
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006118/*
6119 * Return the dictitem that an entry in a hashtable points to.
6120 */
6121 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123{
6124 return HI2DI(hi);
6125}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 * Return TRUE when two dictionaries have exactly the same key/values.
6129 */
6130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131dict_equal(
6132 dict_T *d1,
6133 dict_T *d2,
6134 int ic, /* ignore case for strings */
6135 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136{
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 hashitem_T *hi;
6138 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006141 if (d1 == NULL || d2 == NULL)
6142 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006143 if (d1 == d2)
6144 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 if (dict_len(d1) != dict_len(d2))
6146 return FALSE;
6147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006148 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 if (!HASHITEM_EMPTY(hi))
6152 {
6153 item2 = dict_find(d2, hi->hi_key, -1);
6154 if (item2 == NULL)
6155 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 return FALSE;
6158 --todo;
6159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 }
6161 return TRUE;
6162}
6163
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164static int tv_equal_recurse_limit;
6165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006169 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 */
6171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172tv_equal(
6173 typval_T *tv1,
6174 typval_T *tv2,
6175 int ic, /* ignore case */
6176 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177{
6178 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006179 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006181 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 * recursiveness to a limit. We guess they are equal then.
6188 * A fixed limit has the problem of still taking an awful long time.
6189 * Reduce the limit every time running into it. That should work fine for
6190 * deeply linked structures that are not recursively linked and catch
6191 * recursiveness quickly. */
6192 if (!recursive)
6193 tv_equal_recurse_limit = 1000;
6194 if (recursive_cnt >= tv_equal_recurse_limit)
6195 {
6196 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199
6200 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 ++recursive_cnt;
6210 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6211 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006212 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 case VAR_FUNC:
6215 return (tv1->vval.v_string != NULL
6216 && tv2->vval.v_string != NULL
6217 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6218
6219 case VAR_NUMBER:
6220 return tv1->vval.v_number == tv2->vval.v_number;
6221
6222 case VAR_STRING:
6223 s1 = get_tv_string_buf(tv1, buf1);
6224 s2 = get_tv_string_buf(tv2, buf2);
6225 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006226
6227 case VAR_SPECIAL:
6228 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006229
6230 case VAR_FLOAT:
6231#ifdef FEAT_FLOAT
6232 return tv1->vval.v_float == tv2->vval.v_float;
6233#endif
6234 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006235#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006236 return tv1->vval.v_job == tv2->vval.v_job;
6237#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006238 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006239#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006240 return tv1->vval.v_channel == tv2->vval.v_channel;
6241#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006242 case VAR_UNKNOWN:
6243 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006245
Bram Moolenaara03f2332016-02-06 18:09:59 +01006246 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6247 * does not equal anything, not even itself. */
6248 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249}
6250
6251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 * Locate item with index "n" in list "l" and return it.
6253 * A negative index is counted from the end; -1 is the last item.
6254 * Returns NULL when "n" is out of range.
6255 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258{
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long idx;
6261
6262 if (l == NULL)
6263 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264
6265 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 n = l->lv_len + n;
6268
6269 /* Check for index out of range. */
6270 if (n < 0 || n >= l->lv_len)
6271 return NULL;
6272
6273 /* When there is a cached index may start search from there. */
6274 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 if (n < l->lv_idx / 2)
6277 {
6278 /* closest to the start of the list */
6279 item = l->lv_first;
6280 idx = 0;
6281 }
6282 else if (n > (l->lv_idx + l->lv_len) / 2)
6283 {
6284 /* closest to the end of the list */
6285 item = l->lv_last;
6286 idx = l->lv_len - 1;
6287 }
6288 else
6289 {
6290 /* closest to the cached index */
6291 item = l->lv_idx_item;
6292 idx = l->lv_idx;
6293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 }
6295 else
6296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297 if (n < l->lv_len / 2)
6298 {
6299 /* closest to the start of the list */
6300 item = l->lv_first;
6301 idx = 0;
6302 }
6303 else
6304 {
6305 /* closest to the end of the list */
6306 item = l->lv_last;
6307 idx = l->lv_len - 1;
6308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310
6311 while (n > idx)
6312 {
6313 /* search forward */
6314 item = item->li_next;
6315 ++idx;
6316 }
6317 while (n < idx)
6318 {
6319 /* search backward */
6320 item = item->li_prev;
6321 --idx;
6322 }
6323
6324 /* cache the used index */
6325 l->lv_idx = idx;
6326 l->lv_idx_item = item;
6327
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 return item;
6329}
6330
6331/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006332 * Get list item "l[idx]" as a number.
6333 */
6334 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335list_find_nr(
6336 list_T *l,
6337 long idx,
6338 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006339{
6340 listitem_T *li;
6341
6342 li = list_find(l, idx);
6343 if (li == NULL)
6344 {
6345 if (errorp != NULL)
6346 *errorp = TRUE;
6347 return -1L;
6348 }
6349 return get_tv_number_chk(&li->li_tv, errorp);
6350}
6351
6352/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006353 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6354 */
6355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006357{
6358 listitem_T *li;
6359
6360 li = list_find(l, idx - 1);
6361 if (li == NULL)
6362 {
6363 EMSGN(_(e_listidx), idx);
6364 return NULL;
6365 }
6366 return get_tv_string(&li->li_tv);
6367}
6368
6369/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006370 * Locate "item" list "l" and return its index.
6371 * Returns -1 when "item" is not in the list.
6372 */
6373 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375{
6376 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378
6379 if (l == NULL)
6380 return -1;
6381 idx = 0;
6382 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6383 ++idx;
6384 if (li == NULL)
6385 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006386 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387}
6388
6389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Append item "item" to the end of list "l".
6391 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394{
6395 if (l->lv_last == NULL)
6396 {
6397 /* empty list */
6398 l->lv_first = item;
6399 l->lv_last = item;
6400 item->li_prev = NULL;
6401 }
6402 else
6403 {
6404 l->lv_last->li_next = item;
6405 item->li_prev = l->lv_last;
6406 l->lv_last = item;
6407 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006408 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 item->li_next = NULL;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 copy_tv(tv, &li->li_tv);
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006429 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 * Return FAIL when out of memory.
6431 */
6432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434{
6435 listitem_T *li = listitem_alloc();
6436
6437 if (li == NULL)
6438 return FAIL;
6439 li->li_tv.v_type = VAR_DICT;
6440 li->li_tv.v_lock = 0;
6441 li->li_tv.vval.v_dict = dict;
6442 list_append(list, li);
6443 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 return OK;
6445}
6446
6447/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006449 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Returns FAIL when out of memory.
6451 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 list_append(l, li);
6460 li->li_tv.v_type = VAR_STRING;
6461 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (str == NULL)
6463 li->li_tv.vval.v_string = NULL;
6464 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006465 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 return FAIL;
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006471 * Append "n" to list "l".
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476{
6477 listitem_T *li;
6478
6479 li = listitem_alloc();
6480 if (li == NULL)
6481 return FAIL;
6482 li->li_tv.v_type = VAR_NUMBER;
6483 li->li_tv.v_lock = 0;
6484 li->li_tv.vval.v_number = n;
6485 list_append(l, li);
6486 return OK;
6487}
6488
6489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491 * If "item" is NULL append at the end.
6492 * Return FAIL when out of memory.
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498
6499 if (ni == NULL)
6500 return FAIL;
6501 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502 list_insert(l, ni, item);
6503 return OK;
6504}
6505
6506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006507list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006508{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 if (item == NULL)
6510 /* Append new item at end of list. */
6511 list_append(l, ni);
6512 else
6513 {
6514 /* Insert new item before existing item. */
6515 ni->li_prev = item->li_prev;
6516 ni->li_next = item;
6517 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 ++l->lv_idx;
6521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 l->lv_idx_item = NULL;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530}
6531
6532/*
6533 * Extend "l1" with "l2".
6534 * If "bef" is NULL append at the end, otherwise insert before this item.
6535 * Returns FAIL when out of memory.
6536 */
6537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006543 /* We also quit the loop when we have inserted the original item count of
6544 * the list, avoid a hang when we extend a list with itself. */
6545 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6547 return FAIL;
6548 return OK;
6549}
6550
6551/*
6552 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6553 * Return FAIL when out of memory.
6554 */
6555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006556list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557{
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006560 if (l1 == NULL || l2 == NULL)
6561 return FAIL;
6562
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 if (l == NULL)
6566 return FAIL;
6567 tv->v_type = VAR_LIST;
6568 tv->vval.v_list = l;
6569
6570 /* append all items from the second list */
6571 return list_extend(l, l2, NULL);
6572}
6573
6574/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 * Returns NULL when out of memory.
6579 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *copy;
6584 listitem_T *item;
6585 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
6587 if (orig == NULL)
6588 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 copy = list_alloc();
6591 if (copy != NULL)
6592 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 if (copyID != 0)
6594 {
6595 /* Do this before adding the items, because one of the items may
6596 * refer back to this list. */
6597 orig->lv_copyID = copyID;
6598 orig->lv_copylist = copy;
6599 }
6600 for (item = orig->lv_first; item != NULL && !got_int;
6601 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 {
6603 ni = listitem_alloc();
6604 if (ni == NULL)
6605 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6609 {
6610 vim_free(ni);
6611 break;
6612 }
6613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006615 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 list_append(copy, ni);
6617 }
6618 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 if (item != NULL)
6620 {
6621 list_unref(copy);
6622 copy = NULL;
6623 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 }
6625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 return copy;
6627}
6628
6629/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006631 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006632 * This used to be called list_remove, but that conflicts with a Sun header
6633 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006637{
Bram Moolenaar33570922005-01-25 22:26:29 +00006638 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 /* notify watchers */
6641 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006643 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 list_fix_watch(l, ip);
6645 if (ip == item2)
6646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648
6649 if (item2->li_next == NULL)
6650 l->lv_last = item->li_prev;
6651 else
6652 item2->li_next->li_prev = item->li_prev;
6653 if (item->li_prev == NULL)
6654 l->lv_first = item2->li_next;
6655 else
6656 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006657 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658}
6659
6660/*
6661 * Return an allocated string with the string representation of a list.
6662 * May return NULL.
6663 */
6664 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666{
6667 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
6669 if (tv->vval.v_list == NULL)
6670 return NULL;
6671 ga_init2(&ga, (int)sizeof(char), 80);
6672 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 {
6675 vim_free(ga.ga_data);
6676 return NULL;
6677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 ga_append(&ga, ']');
6679 ga_append(&ga, NUL);
6680 return (char_u *)ga.ga_data;
6681}
6682
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683typedef struct join_S {
6684 char_u *s;
6685 char_u *tofree;
6686} join_T;
6687
6688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689list_join_inner(
6690 garray_T *gap, /* to store the result in */
6691 list_T *l,
6692 char_u *sep,
6693 int echo_style,
6694 int copyID,
6695 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696{
6697 int i;
6698 join_T *p;
6699 int len;
6700 int sumlen = 0;
6701 int first = TRUE;
6702 char_u *tofree;
6703 char_u numbuf[NUMBUFLEN];
6704 listitem_T *item;
6705 char_u *s;
6706
6707 /* Stringify each item in the list. */
6708 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6709 {
6710 if (echo_style)
6711 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6712 else
6713 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6714 if (s == NULL)
6715 return FAIL;
6716
6717 len = (int)STRLEN(s);
6718 sumlen += len;
6719
Bram Moolenaarcde88542015-08-11 19:14:00 +02006720 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6722 if (tofree != NULL || s != numbuf)
6723 {
6724 p->s = s;
6725 p->tofree = tofree;
6726 }
6727 else
6728 {
6729 p->s = vim_strnsave(s, len);
6730 p->tofree = p->s;
6731 }
6732
6733 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006734 if (did_echo_string_emsg) /* recursion error, bail out */
6735 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 }
6737
6738 /* Allocate result buffer with its total size, avoid re-allocation and
6739 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6740 if (join_gap->ga_len >= 2)
6741 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6742 if (ga_grow(gap, sumlen + 2) == FAIL)
6743 return FAIL;
6744
6745 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6746 {
6747 if (first)
6748 first = FALSE;
6749 else
6750 ga_concat(gap, sep);
6751 p = ((join_T *)join_gap->ga_data) + i;
6752
6753 if (p->s != NULL)
6754 ga_concat(gap, p->s);
6755 line_breakcheck();
6756 }
6757
6758 return OK;
6759}
6760
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006763 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list_join(
6768 garray_T *gap,
6769 list_T *l,
6770 char_u *sep,
6771 int echo_style,
6772 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 garray_T join_ga;
6775 int retval;
6776 join_T *p;
6777 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778
Bram Moolenaard39a7512015-04-16 22:51:22 +02006779 if (l->lv_len < 1)
6780 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6782 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6783
6784 /* Dispose each item in join_ga. */
6785 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006787 p = (join_T *)join_ga.ga_data;
6788 for (i = 0; i < join_ga.ga_len; ++i)
6789 {
6790 vim_free(p->tofree);
6791 ++p;
6792 }
6793 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795
6796 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797}
6798
6799/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006800 * Return the next (unique) copy ID.
6801 * Used for serializing nested structures.
6802 */
6803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006805{
6806 current_copyID += COPYID_INC;
6807 return current_copyID;
6808}
6809
6810/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 * Garbage collection for lists and dictionaries.
6812 *
6813 * We use reference counts to be able to free most items right away when they
6814 * are no longer used. But for composite items it's possible that it becomes
6815 * unused while the reference count is > 0: When there is a recursive
6816 * reference. Example:
6817 * :let l = [1, 2, 3]
6818 * :let d = {9: l}
6819 * :let l[1] = d
6820 *
6821 * Since this is quite unusual we handle this with garbage collection: every
6822 * once in a while find out which lists and dicts are not referenced from any
6823 * variable.
6824 *
6825 * Here is a good reference text about garbage collection (refers to Python
6826 * but it applies to all reference-counting mechanisms):
6827 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Do garbage collection for lists and dicts.
6832 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006838 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 buf_T *buf;
6840 win_T *wp;
6841 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006842 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006843 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006845#ifdef FEAT_WINDOWS
6846 tabpage_T *tp;
6847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849 /* Only do this once. */
6850 want_garbage_collect = FALSE;
6851 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006852 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 /* We advance by two because we add one for items referenced through
6855 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006856 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 /*
6859 * 1. Go through all accessible variables and mark all lists and dicts
6860 * with copyID.
6861 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862
6863 /* Don't free variables in the previous_funccal list unless they are only
6864 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006865 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6867 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006868 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6869 NULL);
6870 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6871 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 }
6873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 /* script-local variables */
6875 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877
6878 /* buffer-local variables */
6879 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6881 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882
6883 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006884 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#ifdef FEAT_AUTOCMD
6888 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893#ifdef FEAT_WINDOWS
6894 /* tabpage-local variables */
6895 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006898#endif
6899
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902
6903 /* function-local variables */
6904 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6905 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6907 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 }
6909
Bram Moolenaard812df62008-11-09 12:46:09 +00006910 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006912
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006915#endif
6916
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
6921#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006925#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006926 abort = abort || set_ref_in_channel(copyID);
6927#endif
6928
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 /*
6932 * 2. Free lists and dictionaries that are not referenced.
6933 */
6934 did_free = free_unref_items(copyID);
6935
6936 /*
6937 * 3. Check if any funccal can be freed now.
6938 */
6939 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006940 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 if (can_free_funccal(*pfc, copyID))
6942 {
6943 fc = *pfc;
6944 *pfc = fc->caller;
6945 free_funccal(fc, TRUE);
6946 did_free = TRUE;
6947 did_free_funccal = TRUE;
6948 }
6949 else
6950 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (did_free_funccal)
6953 /* When a funccal was freed some more items might be garbage
6954 * collected, so run again. */
6955 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 else if (p_verbose > 0)
6958 {
6959 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6960 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961
6962 return did_free;
6963}
6964
6965/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006966 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 */
6968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006971 dict_T *dd, *dd_next;
6972 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 int did_free = FALSE;
6974
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 */
6978 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006979 {
6980 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006983 /* Free the Dictionary and ordinary items it contains, but don't
6984 * recurse into Lists and Dictionaries, they will be in the list
6985 * of dicts or list of lists. */
6986 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 dd = dd_next;
6990 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 * Go through the list of lists and free items without the copyID.
6994 * But don't free a list that has a watcher (used in a for loop), these
6995 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 */
6997 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 {
6999 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7001 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007003 /* Free the List and ordinary items it contains, but don't recurse
7004 * into Lists and Dictionaries, they will be in the list of dicts
7005 * or list of lists. */
7006 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007009 ll = ll_next;
7010 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 return did_free;
7013}
7014
7015/*
7016 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 * "list_stack" is used to add lists to be marked. Can be NULL.
7018 *
7019 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007022set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023{
7024 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 hashtab_T *cur_ht;
7028 ht_stack_T *ht_stack = NULL;
7029 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 cur_ht = ht;
7032 for (;;)
7033 {
7034 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 /* Mark each item in the hashtab. If the item contains a hashtab
7037 * it is added to ht_stack, if it contains a list it is added to
7038 * list_stack. */
7039 todo = (int)cur_ht->ht_used;
7040 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7041 if (!HASHITEM_EMPTY(hi))
7042 {
7043 --todo;
7044 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7045 &ht_stack, list_stack);
7046 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048
7049 if (ht_stack == NULL)
7050 break;
7051
7052 /* take an item from the stack */
7053 cur_ht = ht_stack->ht;
7054 tempitem = ht_stack;
7055 ht_stack = ht_stack->prev;
7056 free(tempitem);
7057 }
7058
7059 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060}
7061
7062/*
7063 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7065 *
7066 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 listitem_T *li;
7072 int abort = FALSE;
7073 list_T *cur_l;
7074 list_stack_T *list_stack = NULL;
7075 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 cur_l = l;
7078 for (;;)
7079 {
7080 if (!abort)
7081 /* Mark each item in the list. If the item contains a hashtab
7082 * it is added to ht_stack, if it contains a list it is added to
7083 * list_stack. */
7084 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7085 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7086 ht_stack, &list_stack);
7087 if (list_stack == NULL)
7088 break;
7089
7090 /* take an item from the stack */
7091 cur_l = list_stack->list;
7092 tempitem = list_stack;
7093 list_stack = list_stack->prev;
7094 free(tempitem);
7095 }
7096
7097 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098}
7099
7100/*
7101 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 * "list_stack" is used to add lists to be marked. Can be NULL.
7103 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7104 *
7105 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108set_ref_in_item(
7109 typval_T *tv,
7110 int copyID,
7111 ht_stack_T **ht_stack,
7112 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113{
7114 dict_T *dd;
7115 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007120 dd = tv->vval.v_dict;
7121 if (dd != NULL && dd->dv_copyID != copyID)
7122 {
7123 /* Didn't see this dict yet. */
7124 dd->dv_copyID = copyID;
7125 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7128 }
7129 else
7130 {
7131 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7132 if (newitem == NULL)
7133 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 else
7135 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 newitem->ht = &dd->dv_hashtab;
7137 newitem->prev = *ht_stack;
7138 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007141 }
7142 }
7143 else if (tv->v_type == VAR_LIST)
7144 {
7145 ll = tv->vval.v_list;
7146 if (ll != NULL && ll->lv_copyID != copyID)
7147 {
7148 /* Didn't see this list yet. */
7149 ll->lv_copyID = copyID;
7150 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 abort = set_ref_in_list(ll, copyID, ht_stack);
7153 }
7154 else
7155 {
7156 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 if (newitem == NULL)
7159 abort = TRUE;
7160 else
7161 {
7162 newitem->list = ll;
7163 newitem->prev = *list_stack;
7164 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170}
7171
7172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173 * Allocate an empty header for a dictionary.
7174 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007175 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007176dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177{
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007182 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007183 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 if (first_dict != NULL)
7185 first_dict->dv_used_prev = d;
7186 d->dv_used_next = first_dict;
7187 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007188 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197}
7198
7199/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007200 * Allocate an empty dict for a return value.
7201 * Returns OK or FAIL.
7202 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007205{
7206 dict_T *d = dict_alloc();
7207
7208 if (d == NULL)
7209 return FAIL;
7210
7211 rettv->vval.v_dict = d;
7212 rettv->v_type = VAR_DICT;
7213 ++d->dv_refcount;
7214 return OK;
7215}
7216
7217
7218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 * Unreference a Dictionary: decrement the reference count and free it when it
7220 * becomes zero.
7221 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007223dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007225 if (d != NULL && --d->dv_refcount <= 0)
7226 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227}
7228
7229/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007230 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 * Ignores the reference count.
7232 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234dict_free(
7235 dict_T *d,
7236 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242 /* Remove the dict from the list of dicts for garbage collection. */
7243 if (d->dv_used_prev == NULL)
7244 first_dict = d->dv_used_next;
7245 else
7246 d->dv_used_prev->dv_used_next = d->dv_used_next;
7247 if (d->dv_used_next != NULL)
7248 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7249
7250 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007252 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (!HASHITEM_EMPTY(hi))
7256 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007257 /* Remove the item before deleting it, just in case there is
7258 * something recursive causing trouble. */
7259 di = HI2DI(hi);
7260 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 if (recurse || (di->di_tv.v_type != VAR_LIST
7262 && di->di_tv.v_type != VAR_DICT))
7263 clear_tv(&di->di_tv);
7264 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 --todo;
7266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 vim_free(d);
7270}
7271
7272/*
7273 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 * The "key" is copied to the new item.
7275 * Note that the value of the item "di_tv" still needs to be initialized!
7276 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007278 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007287 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Make a copy of a Dictionary item.
7294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7301 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 if (di != NULL)
7303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007305 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 copy_tv(&org->di_tv, &di->di_tv);
7307 }
7308 return di;
7309}
7310
7311/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 * Remove item "item" from Dictionary "dict" and free it.
7313 */
7314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (HASHITEM_EMPTY(hi))
7321 EMSG2(_(e_intern2), "dictitem_remove()");
7322 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 dictitem_free(item);
7325}
7326
7327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 * Free a dict item. Also clears the value.
7329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007334 if (item->di_flags & DI_FLAGS_ALLOC)
7335 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336}
7337
7338/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7340 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007341 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Returns NULL when out of memory.
7343 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346{
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 dict_T *copy;
7348 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351
7352 if (orig == NULL)
7353 return NULL;
7354
7355 copy = dict_alloc();
7356 if (copy != NULL)
7357 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358 if (copyID != 0)
7359 {
7360 orig->dv_copyID = copyID;
7361 orig->dv_copydict = copy;
7362 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007363 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 --todo;
7369
7370 di = dictitem_alloc(hi->hi_key);
7371 if (di == NULL)
7372 break;
7373 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 {
7375 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7376 copyID) == FAIL)
7377 {
7378 vim_free(di);
7379 break;
7380 }
7381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 else
7383 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7384 if (dict_add(copy, di) == FAIL)
7385 {
7386 dictitem_free(di);
7387 break;
7388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 if (todo > 0)
7394 {
7395 dict_unref(copy);
7396 copy = NULL;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
7399
7400 return copy;
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409{
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411}
7412
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007414 * Add a number or string entry to dictionary "d".
7415 * When "str" is NULL use number "nr", otherwise use "str".
7416 * Returns FAIL when out of memory and when key already exists.
7417 */
7418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419dict_add_nr_str(
7420 dict_T *d,
7421 char *key,
7422 long nr,
7423 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007424{
7425 dictitem_T *item;
7426
7427 item = dictitem_alloc((char_u *)key);
7428 if (item == NULL)
7429 return FAIL;
7430 item->di_tv.v_lock = 0;
7431 if (str == NULL)
7432 {
7433 item->di_tv.v_type = VAR_NUMBER;
7434 item->di_tv.vval.v_number = nr;
7435 }
7436 else
7437 {
7438 item->di_tv.v_type = VAR_STRING;
7439 item->di_tv.vval.v_string = vim_strsave(str);
7440 }
7441 if (dict_add(d, item) == FAIL)
7442 {
7443 dictitem_free(item);
7444 return FAIL;
7445 }
7446 return OK;
7447}
7448
7449/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007450 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 * Returns FAIL when out of memory and when key already exists.
7452 */
7453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007455{
7456 dictitem_T *item;
7457
7458 item = dictitem_alloc((char_u *)key);
7459 if (item == NULL)
7460 return FAIL;
7461 item->di_tv.v_lock = 0;
7462 item->di_tv.v_type = VAR_LIST;
7463 item->di_tv.vval.v_list = list;
7464 if (dict_add(d, item) == FAIL)
7465 {
7466 dictitem_free(item);
7467 return FAIL;
7468 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007469 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007470 return OK;
7471}
7472
7473/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 * Get the number of items in a Dictionary.
7475 */
7476 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479 if (d == NULL)
7480 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007481 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482}
7483
7484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 * Find item "key[len]" in Dictionary "d".
7486 * If "len" is negative use strlen(key).
7487 * Returns NULL when not found.
7488 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007489 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007490dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492#define AKEYLEN 200
7493 char_u buf[AKEYLEN];
7494 char_u *akey;
7495 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 if (len < 0)
7499 akey = key;
7500 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 tofree = akey = vim_strnsave(key, len);
7503 if (akey == NULL)
7504 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 else
7507 {
7508 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007509 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 akey = buf;
7511 }
7512
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 vim_free(tofree);
7515 if (HASHITEM_EMPTY(hi))
7516 return NULL;
7517 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518}
7519
7520/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007521 * Get a string item from a dictionary.
7522 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523 * Returns NULL if the entry doesn't exist or out of memory.
7524 */
7525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527{
7528 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007529 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530
7531 di = dict_find(d, key, -1);
7532 if (di == NULL)
7533 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007534 s = get_tv_string(&di->di_tv);
7535 if (save && s != NULL)
7536 s = vim_strsave(s);
7537 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007538}
7539
7540/*
7541 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007542 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543 */
7544 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546{
7547 dictitem_T *di;
7548
7549 di = dict_find(d, key, -1);
7550 if (di == NULL)
7551 return 0;
7552 return get_tv_number(&di->di_tv);
7553}
7554
7555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 * Return an allocated string with the string representation of a Dictionary.
7557 * May return NULL.
7558 */
7559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561{
7562 garray_T ga;
7563 int first = TRUE;
7564 char_u *tofree;
7565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 return NULL;
7573 ga_init2(&ga, (int)sizeof(char), 80);
7574 ga_append(&ga, '{');
7575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 --todo;
7582
7583 if (first)
7584 first = FALSE;
7585 else
7586 ga_concat(&ga, (char_u *)", ");
7587
7588 tofree = string_quote(hi->hi_key, FALSE);
7589 if (tofree != NULL)
7590 {
7591 ga_concat(&ga, tofree);
7592 vim_free(tofree);
7593 }
7594 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007595 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if (s != NULL)
7597 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007601 line_breakcheck();
7602
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007605 if (todo > 0)
7606 {
7607 vim_free(ga.ga_data);
7608 return NULL;
7609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610
7611 ga_append(&ga, '}');
7612 ga_append(&ga, NUL);
7613 return (char_u *)ga.ga_data;
7614}
7615
7616/*
7617 * Allocate a variable for a Dictionary and fill it from "*arg".
7618 * Return OK or FAIL. Returns NOTDONE for {expr}.
7619 */
7620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007621get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622{
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dict_T *d = NULL;
7624 typval_T tvkey;
7625 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007626 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 /*
7632 * First check if it's not a curly-braces thing: {expr}.
7633 * Must do this without evaluating, otherwise a function may be called
7634 * twice. Unfortunately this means we need to call eval1() twice for the
7635 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 if (*start != '}')
7639 {
7640 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7641 return FAIL;
7642 if (*start == '}')
7643 return NOTDONE;
7644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645
7646 if (evaluate)
7647 {
7648 d = dict_alloc();
7649 if (d == NULL)
7650 return FAIL;
7651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 tvkey.v_type = VAR_UNKNOWN;
7653 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 *arg = skipwhite(*arg + 1);
7656 while (**arg != '}' && **arg != NUL)
7657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007658 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 goto failret;
7660 if (**arg != ':')
7661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007662 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 goto failret;
7665 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007668 key = get_tv_string_buf_chk(&tvkey, buf);
7669 if (key == NULL || *key == NUL)
7670 {
7671 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7672 if (key != NULL)
7673 EMSG(_(e_emptykey));
7674 clear_tv(&tvkey);
7675 goto failret;
7676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 *arg = skipwhite(*arg + 1);
7680 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7681 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007682 if (evaluate)
7683 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 goto failret;
7685 }
7686 if (evaluate)
7687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 if (item != NULL)
7690 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007691 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 clear_tv(&tv);
7694 goto failret;
7695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 item = dictitem_alloc(key);
7697 clear_tv(&tvkey);
7698 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 if (dict_add(d, item) == FAIL)
7703 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 }
7705 }
7706
7707 if (**arg == '}')
7708 break;
7709 if (**arg != ',')
7710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007711 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 goto failret;
7713 }
7714 *arg = skipwhite(*arg + 1);
7715 }
7716
7717 if (**arg != '}')
7718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007719 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720failret:
7721 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007722 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 return FAIL;
7724 }
7725
7726 *arg = skipwhite(*arg + 1);
7727 if (evaluate)
7728 {
7729 rettv->v_type = VAR_DICT;
7730 rettv->vval.v_dict = d;
7731 ++d->dv_refcount;
7732 }
7733
7734 return OK;
7735}
7736
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007737#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007738/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007739 * Decrement the reference count on "channel" and maybe free it when it goes
7740 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007741 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007742 */
7743 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007744channel_unref(channel_T *channel)
7745{
7746 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007747 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007748 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007749}
Bram Moolenaar77073442016-02-13 23:23:53 +01007750
Bram Moolenaar65edff82016-02-21 16:40:11 +01007751static job_T *first_job = NULL;
7752
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753 static void
7754job_free(job_T *job)
7755{
Bram Moolenaard6051b52016-02-28 15:49:03 +01007756 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007757 if (job->jv_channel != NULL)
7758 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007759 /* The link from the channel to the job doesn't count as a reference,
7760 * thus don't decrement the refcount of the job. The reference from
7761 * the job to the channel does count the refrence, decrement it and
7762 * NULL the reference. We don't set ch_job_killed, unreferencing the
7763 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 job->jv_channel->ch_job = NULL;
7765 channel_unref(job->jv_channel);
7766 }
Bram Moolenaar76467df2016-02-12 19:30:26 +01007767 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007768
7769 if (job->jv_next != NULL)
7770 job->jv_next->jv_prev = job->jv_prev;
7771 if (job->jv_prev == NULL)
7772 first_job = job->jv_next;
7773 else
7774 job->jv_prev->jv_next = job->jv_next;
7775
7776 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007777 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007778 vim_free(job);
7779}
7780
7781 static void
7782job_unref(job_T *job)
7783{
7784 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007785 {
7786 /* Do not free the job when it has not ended yet and there is a
7787 * "stoponexit" flag or an exit callback. */
7788 if (job->jv_status != JOB_STARTED
7789 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007790 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007791 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007792 }
7793 else if (job->jv_channel != NULL)
7794 {
7795 /* Do remove the link to the channel, otherwise it hangs
7796 * around until Vim exits. See job_free() for refcount. */
7797 job->jv_channel->ch_job = NULL;
7798 channel_unref(job->jv_channel);
7799 job->jv_channel = NULL;
7800 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007801 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007802}
7803
7804/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007805 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007806 */
7807 static job_T *
7808job_alloc(void)
7809{
7810 job_T *job;
7811
7812 job = (job_T *)alloc_clear(sizeof(job_T));
7813 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007814 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007815 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007816 job->jv_stoponexit = vim_strsave((char_u *)"term");
7817
7818 if (first_job != NULL)
7819 {
7820 first_job->jv_prev = job;
7821 job->jv_next = first_job;
7822 }
7823 first_job = job;
7824 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 return job;
7826}
7827
Bram Moolenaar65edff82016-02-21 16:40:11 +01007828 static void
7829job_set_options(job_T *job, jobopt_T *opt)
7830{
7831 if (opt->jo_set & JO_STOPONEXIT)
7832 {
7833 vim_free(job->jv_stoponexit);
7834 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7835 job->jv_stoponexit = NULL;
7836 else
7837 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7838 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007839 if (opt->jo_set & JO_EXIT_CB)
7840 {
7841 vim_free(job->jv_exit_cb);
7842 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7843 job->jv_exit_cb = NULL;
7844 else
7845 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7846 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007847}
7848
7849/*
7850 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7851 */
7852 void
7853job_stop_on_exit()
7854{
7855 job_T *job;
7856
7857 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007858 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007859 mch_stop_job(job, job->jv_stoponexit);
7860}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007861#endif
7862
Bram Moolenaar17a13432016-01-24 14:22:10 +01007863 static char *
7864get_var_special_name(int nr)
7865{
7866 switch (nr)
7867 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007868 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007869 case VVAL_TRUE: return "v:true";
7870 case VVAL_NONE: return "v:none";
7871 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007872 }
7873 EMSG2(_(e_intern2), "get_var_special_name()");
7874 return "42";
7875}
7876
Bram Moolenaar8c711452005-01-14 21:53:12 +00007877/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 * Return a string with the string representation of a variable.
7879 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007880 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007881 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007882 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007883 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884 */
7885 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007886echo_string(
7887 typval_T *tv,
7888 char_u **tofree,
7889 char_u *numbuf,
7890 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 static int recurse = 0;
7893 char_u *r = NULL;
7894
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007897 if (!did_echo_string_emsg)
7898 {
7899 /* Only give this message once for a recursive call to avoid
7900 * flooding the user with errors. And stop iterating over lists
7901 * and dicts. */
7902 did_echo_string_emsg = TRUE;
7903 EMSG(_("E724: variable nested too deep for displaying"));
7904 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007906 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907 }
7908 ++recurse;
7909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 switch (tv->v_type)
7911 {
7912 case VAR_FUNC:
7913 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 r = tv->vval.v_string;
7915 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007918 if (tv->vval.v_list == NULL)
7919 {
7920 *tofree = NULL;
7921 r = NULL;
7922 }
7923 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7924 {
7925 *tofree = NULL;
7926 r = (char_u *)"[...]";
7927 }
7928 else
7929 {
7930 tv->vval.v_list->lv_copyID = copyID;
7931 *tofree = list2string(tv, copyID);
7932 r = *tofree;
7933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007934 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007935
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007937 if (tv->vval.v_dict == NULL)
7938 {
7939 *tofree = NULL;
7940 r = NULL;
7941 }
7942 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7943 {
7944 *tofree = NULL;
7945 r = (char_u *)"{...}";
7946 }
7947 else
7948 {
7949 tv->vval.v_dict->dv_copyID = copyID;
7950 *tofree = dict2string(tv, copyID);
7951 r = *tofree;
7952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 case VAR_STRING:
7956 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007957 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007958 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007959 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 *tofree = NULL;
7961 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007963
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007964 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007965#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966 *tofree = NULL;
7967 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7968 r = numbuf;
7969 break;
7970#endif
7971
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007972 case VAR_SPECIAL:
7973 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007974 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007977
Bram Moolenaar8502c702014-06-17 12:51:16 +02007978 if (--recurse == 0)
7979 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007980 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981}
7982
7983/*
7984 * Return a string with the string representation of a variable.
7985 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7986 * "numbuf" is used for a number.
7987 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007988 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989 */
7990 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007991tv2string(
7992 typval_T *tv,
7993 char_u **tofree,
7994 char_u *numbuf,
7995 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996{
7997 switch (tv->v_type)
7998 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 case VAR_FUNC:
8000 *tofree = string_quote(tv->vval.v_string, TRUE);
8001 return *tofree;
8002 case VAR_STRING:
8003 *tofree = string_quote(tv->vval.v_string, FALSE);
8004 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008005 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008006#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008007 *tofree = NULL;
8008 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8009 return numbuf;
8010#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008012 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008013 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008014 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008015 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008016 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008017 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008019 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008020 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021}
8022
8023/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 * Return string "str" in ' quotes, doubling ' characters.
8025 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008026 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008027 */
8028 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008029string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030{
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008032 char_u *p, *r, *s;
8033
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 len = (function ? 13 : 3);
8035 if (str != NULL)
8036 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008037 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 for (p = str; *p != NUL; mb_ptr_adv(p))
8039 if (*p == '\'')
8040 ++len;
8041 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 s = r = alloc(len);
8043 if (r != NULL)
8044 {
8045 if (function)
8046 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008047 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008048 r += 10;
8049 }
8050 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008052 if (str != NULL)
8053 for (p = str; *p != NUL; )
8054 {
8055 if (*p == '\'')
8056 *r++ = '\'';
8057 MB_COPY_CHAR(p, r);
8058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008059 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008060 if (function)
8061 *r++ = ')';
8062 *r++ = NUL;
8063 }
8064 return s;
8065}
8066
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008067#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068/*
8069 * Convert the string "text" to a floating point number.
8070 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8071 * this always uses a decimal point.
8072 * Returns the length of the text that was consumed.
8073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008075string2float(
8076 char_u *text,
8077 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078{
8079 char *s = (char *)text;
8080 float_T f;
8081
8082 f = strtod(s, &s);
8083 *value = f;
8084 return (int)((char_u *)s - text);
8085}
8086#endif
8087
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 * Get the value of an environment variable.
8090 * "arg" is pointing to the '$'. It is advanced to after the name.
8091 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008092 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 */
8094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008095get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 char_u *string = NULL;
8098 int len;
8099 int cc;
8100 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008101 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102
8103 ++*arg;
8104 name = *arg;
8105 len = get_env_len(arg);
8106 if (evaluate)
8107 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008108 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008109 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008110
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008111 cc = name[len];
8112 name[len] = NUL;
8113 /* first try vim_getenv(), fast for normal environment vars */
8114 string = vim_getenv(name, &mustfree);
8115 if (string != NULL && *string != NUL)
8116 {
8117 if (!mustfree)
8118 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008120 else
8121 {
8122 if (mustfree)
8123 vim_free(string);
8124
8125 /* next try expanding things like $VIM and ${HOME} */
8126 string = expand_env_save(name - 1);
8127 if (string != NULL && *string == '$')
8128 {
8129 vim_free(string);
8130 string = NULL;
8131 }
8132 }
8133 name[len] = cc;
8134
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135 rettv->v_type = VAR_STRING;
8136 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138
8139 return OK;
8140}
8141
8142/*
8143 * Array with names and number of arguments of all internal functions
8144 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8145 */
8146static struct fst
8147{
8148 char *f_name; /* function name */
8149 char f_min_argc; /* minimal number of arguments */
8150 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008151 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008152 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153} functions[] =
8154{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155#ifdef FEAT_FLOAT
8156 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008157 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008158#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008159 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008160 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008161 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"append", 2, 2, f_append},
8163 {"argc", 0, 0, f_argc},
8164 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008165 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008166 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008167#ifdef FEAT_FLOAT
8168 {"asin", 1, 1, f_asin}, /* WJMc */
8169#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008170 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008171 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008172 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008173 {"assert_false", 1, 2, f_assert_false},
8174 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175#ifdef FEAT_FLOAT
8176 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008177 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008180 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"bufexists", 1, 1, f_bufexists},
8182 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8183 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8184 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8185 {"buflisted", 1, 1, f_buflisted},
8186 {"bufloaded", 1, 1, f_bufloaded},
8187 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008188 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"bufwinnr", 1, 1, f_bufwinnr},
8190 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008191 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008192 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008194#ifdef FEAT_FLOAT
8195 {"ceil", 1, 1, f_ceil},
8196#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008197#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008198 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008199 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8200 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008201 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008202 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008203 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008204 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008205 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008206 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008207 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008208 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8209 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008210 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008211 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008212#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008213 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008214 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008216 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008218#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008219 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008220 {"complete_add", 1, 1, f_complete_add},
8221 {"complete_check", 0, 0, f_complete_check},
8222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008224 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008225#ifdef FEAT_FLOAT
8226 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008227 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008228#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008231 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008232 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008233 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008235 {"diff_filler", 1, 1, f_diff_filler},
8236 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008237 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008238 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"eventhandler", 0, 0, f_eventhandler},
8242 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008243 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008245#ifdef FEAT_FLOAT
8246 {"exp", 1, 1, f_exp},
8247#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008248 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008249 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008250 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8252 {"filereadable", 1, 1, f_filereadable},
8253 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008254 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008255 {"finddir", 1, 3, f_finddir},
8256 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008257#ifdef FEAT_FLOAT
8258 {"float2nr", 1, 1, f_float2nr},
8259 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008260 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008262 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"fnamemodify", 2, 2, f_fnamemodify},
8264 {"foldclosed", 1, 1, f_foldclosed},
8265 {"foldclosedend", 1, 1, f_foldclosedend},
8266 {"foldlevel", 1, 1, f_foldlevel},
8267 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008268 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008271 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008272 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008273 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008274 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"getchar", 0, 1, f_getchar},
8276 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008277 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"getcmdline", 0, 0, f_getcmdline},
8279 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008280 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008281 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008282 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008283 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008284 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008285 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getfsize", 1, 1, f_getfsize},
8287 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008288 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008289 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008290 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008291 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008292 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008293 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008294 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008295 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008297 {"gettabvar", 2, 3, f_gettabvar},
8298 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"getwinposx", 0, 0, f_getwinposx},
8300 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008301 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008302 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008303 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008304 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008307 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008308 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8310 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8311 {"histadd", 2, 2, f_histadd},
8312 {"histdel", 1, 2, f_histdel},
8313 {"histget", 1, 2, f_histget},
8314 {"histnr", 1, 1, f_histnr},
8315 {"hlID", 1, 1, f_hlID},
8316 {"hlexists", 1, 1, f_hlexists},
8317 {"hostname", 0, 0, f_hostname},
8318 {"iconv", 3, 3, f_iconv},
8319 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008320 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008321 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008323 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"inputrestore", 0, 0, f_inputrestore},
8325 {"inputsave", 0, 0, f_inputsave},
8326 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008327 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008328 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008330 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008331#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8332 {"isnan", 1, 1, f_isnan},
8333#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008334 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008336 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008337 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008338 {"job_start", 1, 2, f_job_start},
8339 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008340 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008341#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008343 {"js_decode", 1, 1, f_js_decode},
8344 {"js_encode", 1, 1, f_js_encode},
8345 {"json_decode", 1, 1, f_json_decode},
8346 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008349 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {"libcall", 3, 3, f_libcall},
8351 {"libcallnr", 3, 3, f_libcallnr},
8352 {"line", 1, 1, f_line},
8353 {"line2byte", 1, 1, f_line2byte},
8354 {"lispindent", 1, 1, f_lispindent},
8355 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008356#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008357 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008358 {"log10", 1, 1, f_log10},
8359#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008360#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008361 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008362#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008363 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008364 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008365 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008366 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008367 {"matchadd", 2, 5, f_matchadd},
8368 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008369 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008370 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008371 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008372 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008373 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008374 {"max", 1, 1, f_max},
8375 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008376#ifdef vim_mkdir
8377 {"mkdir", 1, 3, f_mkdir},
8378#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008379 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008380#ifdef FEAT_MZSCHEME
8381 {"mzeval", 1, 1, f_mzeval},
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008384 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008385 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008386 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008387#ifdef FEAT_PERL
8388 {"perleval", 1, 1, f_perleval},
8389#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008390#ifdef FEAT_FLOAT
8391 {"pow", 2, 2, f_pow},
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008394 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008395 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008396#ifdef FEAT_PYTHON3
8397 {"py3eval", 1, 1, f_py3eval},
8398#endif
8399#ifdef FEAT_PYTHON
8400 {"pyeval", 1, 1, f_pyeval},
8401#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008402 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008403 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008404 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008405#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008406 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008407#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008408 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"remote_expr", 2, 3, f_remote_expr},
8410 {"remote_foreground", 1, 1, f_remote_foreground},
8411 {"remote_peek", 1, 2, f_remote_peek},
8412 {"remote_read", 1, 1, f_remote_read},
8413 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008414 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008416 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008418 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#ifdef FEAT_FLOAT
8420 {"round", 1, 1, f_round},
8421#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008422 {"screenattr", 2, 2, f_screenattr},
8423 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008424 {"screencol", 0, 0, f_screencol},
8425 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008426 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008427 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008428 {"searchpair", 3, 7, f_searchpair},
8429 {"searchpairpos", 3, 7, f_searchpairpos},
8430 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"server2client", 2, 2, f_server2client},
8432 {"serverlist", 0, 0, f_serverlist},
8433 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008434 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008436 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008438 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008439 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008440 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008441 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008443 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008444 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008446#ifdef FEAT_CRYPT
8447 {"sha256", 1, 1, f_sha256},
8448#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008449 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008450 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#ifdef FEAT_FLOAT
8453 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008454 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008456 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008457 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008458 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008459 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008460 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008461#ifdef FEAT_FLOAT
8462 {"sqrt", 1, 1, f_sqrt},
8463 {"str2float", 1, 1, f_str2float},
8464#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008465 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008466 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008467 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#ifdef HAVE_STRFTIME
8469 {"strftime", 1, 2, f_strftime},
8470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008471 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008472 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"strlen", 1, 1, f_strlen},
8474 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008475 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008477 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008478 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"substitute", 4, 4, f_substitute},
8480 {"synID", 3, 3, f_synID},
8481 {"synIDattr", 2, 3, f_synIDattr},
8482 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008483 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008484 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008485 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008486 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008487 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008488 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008489 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008490 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008491 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008492#ifdef FEAT_FLOAT
8493 {"tan", 1, 1, f_tan},
8494 {"tanh", 1, 1, f_tanh},
8495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008497 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"tolower", 1, 1, f_tolower},
8499 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008500 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008501#ifdef FEAT_FLOAT
8502 {"trunc", 1, 1, f_trunc},
8503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008505 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008506 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008507 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"virtcol", 1, 1, f_virtcol},
8510 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008511 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"winbufnr", 1, 1, f_winbufnr},
8513 {"wincol", 0, 0, f_wincol},
8514 {"winheight", 1, 1, f_winheight},
8515 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008516 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008518 {"winrestview", 1, 1, f_winrestview},
8519 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008521 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008522 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008523 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524};
8525
8526#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8527
8528/*
8529 * Function given to ExpandGeneric() to obtain the list of internal
8530 * or user defined function names.
8531 */
8532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008533get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 static int intidx = -1;
8536 char_u *name;
8537
8538 if (idx == 0)
8539 intidx = -1;
8540 if (intidx < 0)
8541 {
8542 name = get_user_func_name(xp, idx);
8543 if (name != NULL)
8544 return name;
8545 }
8546 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8547 {
8548 STRCPY(IObuff, functions[intidx].f_name);
8549 STRCAT(IObuff, "(");
8550 if (functions[intidx].f_max_argc == 0)
8551 STRCAT(IObuff, ")");
8552 return IObuff;
8553 }
8554
8555 return NULL;
8556}
8557
8558/*
8559 * Function given to ExpandGeneric() to obtain the list of internal or
8560 * user defined variable or function names.
8561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008563get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 static int intidx = -1;
8566 char_u *name;
8567
8568 if (idx == 0)
8569 intidx = -1;
8570 if (intidx < 0)
8571 {
8572 name = get_function_name(xp, idx);
8573 if (name != NULL)
8574 return name;
8575 }
8576 return get_user_var_name(xp, ++intidx);
8577}
8578
8579#endif /* FEAT_CMDL_COMPL */
8580
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008581#if defined(EBCDIC) || defined(PROTO)
8582/*
8583 * Compare struct fst by function name.
8584 */
8585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008586compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008587{
8588 struct fst *p1 = (struct fst *)s1;
8589 struct fst *p2 = (struct fst *)s2;
8590
8591 return STRCMP(p1->f_name, p2->f_name);
8592}
8593
8594/*
8595 * Sort the function table by function name.
8596 * The sorting of the table above is ASCII dependant.
8597 * On machines using EBCDIC we have to sort it.
8598 */
8599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8603
8604 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8605}
8606#endif
8607
8608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609/*
8610 * Find internal function in table above.
8611 * Return index, or -1 if not found
8612 */
8613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614find_internal_func(
8615 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 int first = 0;
8618 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8619 int cmp;
8620 int x;
8621
8622 /*
8623 * Find the function name in the table. Binary search.
8624 */
8625 while (first <= last)
8626 {
8627 x = first + ((unsigned)(last - first) >> 1);
8628 cmp = STRCMP(name, functions[x].f_name);
8629 if (cmp < 0)
8630 last = x - 1;
8631 else if (cmp > 0)
8632 first = x + 1;
8633 else
8634 return x;
8635 }
8636 return -1;
8637}
8638
8639/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8641 * name it contains, otherwise return "name".
8642 */
8643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008644deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008645{
Bram Moolenaar33570922005-01-25 22:26:29 +00008646 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 int cc;
8648
8649 cc = name[*lenp];
8650 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008651 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 {
8657 *lenp = 0;
8658 return (char_u *)""; /* just in case */
8659 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008660 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008661 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 }
8663
8664 return name;
8665}
8666
8667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 * Allocate a variable for the result of a function.
8669 * Return OK or FAIL.
8670 */
8671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008672get_func_tv(
8673 char_u *name, /* name of the function */
8674 int len, /* length of "name" */
8675 typval_T *rettv,
8676 char_u **arg, /* argument, pointing to the '(' */
8677 linenr_T firstline, /* first line of range */
8678 linenr_T lastline, /* last line of range */
8679 int *doesrange, /* return: function handled range */
8680 int evaluate,
8681 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 char_u *argp;
8684 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008685 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 int argcount = 0; /* number of arguments found */
8687
8688 /*
8689 * Get the arguments.
8690 */
8691 argp = *arg;
8692 while (argcount < MAX_FUNC_ARGS)
8693 {
8694 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8695 if (*argp == ')' || *argp == ',' || *argp == NUL)
8696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8698 {
8699 ret = FAIL;
8700 break;
8701 }
8702 ++argcount;
8703 if (*argp != ',')
8704 break;
8705 }
8706 if (*argp == ')')
8707 ++argp;
8708 else
8709 ret = FAIL;
8710
8711 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008713 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008715 {
8716 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008717 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008719 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 *arg = skipwhite(argp);
8726 return ret;
8727}
8728
8729
8730/*
8731 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008732 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008733 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008736call_func(
8737 char_u *funcname, /* name of the function */
8738 int len, /* length of "name" */
8739 typval_T *rettv, /* return value goes here */
8740 int argcount, /* number of "argvars" */
8741 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008742 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008743 linenr_T firstline, /* first line of range */
8744 linenr_T lastline, /* last line of range */
8745 int *doesrange, /* return: function handled range */
8746 int evaluate,
8747 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750#define ERROR_UNKNOWN 0
8751#define ERROR_TOOMANY 1
8752#define ERROR_TOOFEW 2
8753#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008754#define ERROR_DICT 4
8755#define ERROR_NONE 5
8756#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 int error = ERROR_NONE;
8758 int i;
8759 int llen;
8760 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#define FLEN_FIXED 40
8762 char_u fname_buf[FLEN_FIXED + 1];
8763 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008764 char_u *name;
8765
8766 /* Make a copy of the name, if it comes from a funcref variable it could
8767 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008768 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769 if (name == NULL)
8770 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 /*
8773 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8774 * Change <SNR>123_name() to K_SNR 123_name().
8775 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 llen = eval_fname_script(name);
8778 if (llen > 0)
8779 {
8780 fname_buf[0] = K_SPECIAL;
8781 fname_buf[1] = KS_EXTRA;
8782 fname_buf[2] = (int)KE_SNR;
8783 i = 3;
8784 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8785 {
8786 if (current_SID <= 0)
8787 error = ERROR_SCRIPT;
8788 else
8789 {
8790 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8791 i = (int)STRLEN(fname_buf);
8792 }
8793 }
8794 if (i + STRLEN(name + llen) < FLEN_FIXED)
8795 {
8796 STRCPY(fname_buf + i, name + llen);
8797 fname = fname_buf;
8798 }
8799 else
8800 {
8801 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8802 if (fname == NULL)
8803 error = ERROR_OTHER;
8804 else
8805 {
8806 mch_memmove(fname, fname_buf, (size_t)i);
8807 STRCPY(fname + i, name + llen);
8808 }
8809 }
8810 }
8811 else
8812 fname = name;
8813
8814 *doesrange = FALSE;
8815
8816
8817 /* execute the function if no errors detected and executing */
8818 if (evaluate && error == ERROR_NONE)
8819 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008820 char_u *rfname = fname;
8821
8822 /* Ignore "g:" before a function name. */
8823 if (fname[0] == 'g' && fname[1] == ':')
8824 rfname = fname + 2;
8825
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008826 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8827 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 error = ERROR_UNKNOWN;
8829
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
8832 /*
8833 * User defined function.
8834 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008835 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 /* Trigger FuncUndefined event, may load the function. */
8839 if (fp == NULL
8840 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008841 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008845 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850 {
8851 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853 }
8854
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (fp != NULL)
8856 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008857 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008859 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008861 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008863 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008864 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 else
8866 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008867 int did_save_redo = FALSE;
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 /*
8870 * Call the user function.
8871 * Save and restore search patterns, script variables and
8872 * redo buffer.
8873 */
8874 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008875#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008876 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008877#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008878 {
8879 saveRedobuff();
8880 did_save_redo = TRUE;
8881 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008882 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008883 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008884 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008885 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8886 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8887 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008888 /* Function was unreferenced while being used, free it
8889 * now. */
8890 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008891 if (did_save_redo)
8892 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 restore_search_patterns();
8894 error = ERROR_NONE;
8895 }
8896 }
8897 }
8898 else
8899 {
8900 /*
8901 * Find the function name in the table, call its implementation.
8902 */
8903 i = find_internal_func(fname);
8904 if (i >= 0)
8905 {
8906 if (argcount < functions[i].f_min_argc)
8907 error = ERROR_TOOFEW;
8908 else if (argcount > functions[i].f_max_argc)
8909 error = ERROR_TOOMANY;
8910 else
8911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008912 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 error = ERROR_NONE;
8915 }
8916 }
8917 }
8918 /*
8919 * The function call (or "FuncUndefined" autocommand sequence) might
8920 * have been aborted by an error, an interrupt, or an explicitly thrown
8921 * exception that has not been caught so far. This situation can be
8922 * tested for by calling aborting(). For an error in an internal
8923 * function or for the "E132" error in call_user_func(), however, the
8924 * throw point at which the "force_abort" flag (temporarily reset by
8925 * emsg()) is normally updated has not been reached yet. We need to
8926 * update that flag first to make aborting() reliable.
8927 */
8928 update_force_abort();
8929 }
8930 if (error == ERROR_NONE)
8931 ret = OK;
8932
8933 /*
8934 * Report an error unless the argument evaluation or function call has been
8935 * cancelled due to an aborting error, an interrupt, or an exception.
8936 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 if (!aborting())
8938 {
8939 switch (error)
8940 {
8941 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008942 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008943 break;
8944 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008945 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008946 break;
8947 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008948 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 name);
8950 break;
8951 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008952 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 name);
8954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008955 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008957 name);
8958 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008959 }
8960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 if (fname != name && fname != fname_buf)
8963 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008964 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
8966 return ret;
8967}
8968
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969/*
8970 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008971 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008972 */
8973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008974emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975{
8976 char_u *p;
8977
8978 if (*name == K_SPECIAL)
8979 p = concat_str((char_u *)"<SNR>", name + 3);
8980 else
8981 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008982 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983 if (p != name)
8984 vim_free(p);
8985}
8986
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008987/*
8988 * Return TRUE for a non-zero Number and a non-empty String.
8989 */
8990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008991non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008992{
8993 return ((argvars[0].v_type == VAR_NUMBER
8994 && argvars[0].vval.v_number != 0)
8995 || (argvars[0].v_type == VAR_STRING
8996 && argvars[0].vval.v_string != NULL
8997 && *argvars[0].vval.v_string != NUL));
8998}
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*********************************************
9001 * Implementation of the built-in functions
9002 */
9003
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009004#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009005static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009006
9007/*
9008 * Get the float value of "argvars[0]" into "f".
9009 * Returns FAIL when the argument is not a Number or Float.
9010 */
9011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009012get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009013{
9014 if (argvars[0].v_type == VAR_FLOAT)
9015 {
9016 *f = argvars[0].vval.v_float;
9017 return OK;
9018 }
9019 if (argvars[0].v_type == VAR_NUMBER)
9020 {
9021 *f = (float_T)argvars[0].vval.v_number;
9022 return OK;
9023 }
9024 EMSG(_("E808: Number or Float required"));
9025 return FAIL;
9026}
9027
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009028/*
9029 * "abs(expr)" function
9030 */
9031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009033{
9034 if (argvars[0].v_type == VAR_FLOAT)
9035 {
9036 rettv->v_type = VAR_FLOAT;
9037 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9038 }
9039 else
9040 {
9041 varnumber_T n;
9042 int error = FALSE;
9043
9044 n = get_tv_number_chk(&argvars[0], &error);
9045 if (error)
9046 rettv->vval.v_number = -1;
9047 else if (n > 0)
9048 rettv->vval.v_number = n;
9049 else
9050 rettv->vval.v_number = -n;
9051 }
9052}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053
9054/*
9055 * "acos()" function
9056 */
9057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009058f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009059{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009060 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009061
9062 rettv->v_type = VAR_FLOAT;
9063 if (get_float_arg(argvars, &f) == OK)
9064 rettv->vval.v_float = acos(f);
9065 else
9066 rettv->vval.v_float = 0.0;
9067}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009068#endif
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 */
9073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009074f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075{
Bram Moolenaar33570922005-01-25 22:26:29 +00009076 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009079 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009081 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009082 && !tv_check_lock(l->lv_lock,
9083 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009084 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 }
9087 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088 EMSG(_(e_listreq));
9089}
9090
9091/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009092 * "alloc_fail(id, countdown, repeat)" function
9093 */
9094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009096{
9097 if (argvars[0].v_type != VAR_NUMBER
9098 || argvars[0].vval.v_number <= 0
9099 || argvars[1].v_type != VAR_NUMBER
9100 || argvars[1].vval.v_number < 0
9101 || argvars[2].v_type != VAR_NUMBER)
9102 EMSG(_(e_invarg));
9103 else
9104 {
9105 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009106 if (alloc_fail_id >= aid_last)
9107 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009108 alloc_fail_countdown = argvars[1].vval.v_number;
9109 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009110 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009111 }
9112}
9113
9114/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009115 * "and(expr, expr)" function
9116 */
9117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009118f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009119{
9120 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9121 & get_tv_number_chk(&argvars[1], NULL);
9122}
9123
9124/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009125 * "append(lnum, string/list)" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129{
9130 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 list_T *l = NULL;
9133 listitem_T *li = NULL;
9134 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 long added = 0;
9136
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009137 /* When coming here from Insert mode, sync undo, so that this can be
9138 * undone separately from what was previously inserted. */
9139 if (u_sync_once == 2)
9140 {
9141 u_sync_once = 1; /* notify that u_sync() was called */
9142 u_sync(TRUE);
9143 }
9144
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 lnum = get_tv_lnum(argvars);
9146 if (lnum >= 0
9147 && lnum <= curbuf->b_ml.ml_line_count
9148 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009149 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009152 l = argvars[1].vval.v_list;
9153 if (l == NULL)
9154 return;
9155 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157 for (;;)
9158 {
9159 if (l == NULL)
9160 tv = &argvars[1]; /* append a string */
9161 else if (li == NULL)
9162 break; /* end of list */
9163 else
9164 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 line = get_tv_string_chk(tv);
9166 if (line == NULL) /* type error */
9167 {
9168 rettv->vval.v_number = 1; /* Failed */
9169 break;
9170 }
9171 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172 ++added;
9173 if (l == NULL)
9174 break;
9175 li = li->li_next;
9176 }
9177
9178 appended_lines_mark(lnum, added);
9179 if (curwin->w_cursor.lnum > lnum)
9180 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 else
9183 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
9187 * "argc()" function
9188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009190f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009192 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193}
9194
9195/*
9196 * "argidx()" function
9197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009199f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202}
9203
9204/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009205 * "arglistid()" function
9206 */
9207 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009208f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009209{
9210 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009211
9212 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009213 wp = find_tabwin(&argvars[0], &argvars[1]);
9214 if (wp != NULL)
9215 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009216}
9217
9218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 * "argv(nr)" function
9220 */
9221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009222f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 int idx;
9225
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009226 if (argvars[0].v_type != VAR_UNKNOWN)
9227 {
9228 idx = get_tv_number_chk(&argvars[0], NULL);
9229 if (idx >= 0 && idx < ARGCOUNT)
9230 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9231 else
9232 rettv->vval.v_string = NULL;
9233 rettv->v_type = VAR_STRING;
9234 }
9235 else if (rettv_list_alloc(rettv) == OK)
9236 for (idx = 0; idx < ARGCOUNT; ++idx)
9237 list_append_string(rettv->vval.v_list,
9238 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239}
9240
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009241static void prepare_assert_error(garray_T*gap);
9242static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9243static void assert_error(garray_T *gap);
9244static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245
9246/*
9247 * Prepare "gap" for an assert error and add the sourcing position.
9248 */
9249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009250prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251{
9252 char buf[NUMBUFLEN];
9253
9254 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009255 if (sourcing_name != NULL)
9256 {
9257 ga_concat(gap, sourcing_name);
9258 if (sourcing_lnum > 0)
9259 ga_concat(gap, (char_u *)" ");
9260 }
9261 if (sourcing_lnum > 0)
9262 {
9263 sprintf(buf, "line %ld", (long)sourcing_lnum);
9264 ga_concat(gap, (char_u *)buf);
9265 }
9266 if (sourcing_name != NULL || sourcing_lnum > 0)
9267 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009268}
9269
9270/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009271 * Append "str" to "gap", escaping unprintable characters.
9272 * Changes NL to \n, CR to \r, etc.
9273 */
9274 static void
9275ga_concat_esc(garray_T *gap, char_u *str)
9276{
9277 char_u *p;
9278 char_u buf[NUMBUFLEN];
9279
9280 for (p = str; *p != NUL; ++p)
9281 switch (*p)
9282 {
9283 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9284 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9285 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9286 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9287 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9288 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9289 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9290 default:
9291 if (*p < ' ')
9292 {
9293 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9294 ga_concat(gap, buf);
9295 }
9296 else
9297 ga_append(gap, *p);
9298 break;
9299 }
9300}
9301
9302/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009303 * Fill "gap" with information about an assert error.
9304 */
9305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009306fill_assert_error(
9307 garray_T *gap,
9308 typval_T *opt_msg_tv,
9309 char_u *exp_str,
9310 typval_T *exp_tv,
9311 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009312{
9313 char_u numbuf[NUMBUFLEN];
9314 char_u *tofree;
9315
9316 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9317 {
9318 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9319 vim_free(tofree);
9320 }
9321 else
9322 {
9323 ga_concat(gap, (char_u *)"Expected ");
9324 if (exp_str == NULL)
9325 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009326 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 vim_free(tofree);
9328 }
9329 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009332 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009333 vim_free(tofree);
9334 }
9335}
Bram Moolenaar43345542015-11-29 17:35:35 +01009336
9337/*
9338 * Add an assert error to v:errors.
9339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009342{
9343 struct vimvar *vp = &vimvars[VV_ERRORS];
9344
9345 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9346 /* Make sure v:errors is a list. */
9347 set_vim_var_list(VV_ERRORS, list_alloc());
9348 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9349}
9350
Bram Moolenaar43345542015-11-29 17:35:35 +01009351/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009358
9359 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9360 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009361 prepare_assert_error(&ga);
9362 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9363 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009364 ga_clear(&ga);
9365 }
9366}
9367
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009369 * "assert_exception(string[, msg])" function
9370 */
9371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009372f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009373{
9374 garray_T ga;
9375 char *error;
9376
9377 error = (char *)get_tv_string_chk(&argvars[0]);
9378 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9379 {
9380 prepare_assert_error(&ga);
9381 ga_concat(&ga, (char_u *)"v:exception is not set");
9382 assert_error(&ga);
9383 ga_clear(&ga);
9384 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009385 else if (error != NULL
9386 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387 {
9388 prepare_assert_error(&ga);
9389 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9390 &vimvars[VV_EXCEPTION].vv_tv);
9391 assert_error(&ga);
9392 ga_clear(&ga);
9393 }
9394}
9395
9396/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009397 * "assert_fails(cmd [, error])" function
9398 */
9399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009400f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009401{
9402 char_u *cmd = get_tv_string_chk(&argvars[0]);
9403 garray_T ga;
9404
9405 called_emsg = FALSE;
9406 suppress_errthrow = TRUE;
9407 emsg_silent = TRUE;
9408 do_cmdline_cmd(cmd);
9409 if (!called_emsg)
9410 {
9411 prepare_assert_error(&ga);
9412 ga_concat(&ga, (char_u *)"command did not fail: ");
9413 ga_concat(&ga, cmd);
9414 assert_error(&ga);
9415 ga_clear(&ga);
9416 }
9417 else if (argvars[1].v_type != VAR_UNKNOWN)
9418 {
9419 char_u buf[NUMBUFLEN];
9420 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9421
9422 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9423 {
9424 prepare_assert_error(&ga);
9425 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9426 &vimvars[VV_ERRMSG].vv_tv);
9427 assert_error(&ga);
9428 ga_clear(&ga);
9429 }
9430 }
9431
9432 called_emsg = FALSE;
9433 suppress_errthrow = FALSE;
9434 emsg_silent = FALSE;
9435 emsg_on_display = FALSE;
9436 set_vim_var_string(VV_ERRMSG, NULL, 0);
9437}
9438
9439/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009440 * Common for assert_true() and assert_false().
9441 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009443assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009444{
9445 int error = FALSE;
9446 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009447
Bram Moolenaar37127922016-02-06 20:29:28 +01009448 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009449 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009450 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 if (argvars[0].v_type != VAR_NUMBER
9452 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9453 || error)
9454 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 prepare_assert_error(&ga);
9456 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009457 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 NULL, &argvars[0]);
9459 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009460 ga_clear(&ga);
9461 }
9462}
9463
9464/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009465 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009466 */
9467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009468f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009469{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009470 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009471}
9472
9473/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009474 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009475 */
9476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009477f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009478{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009480}
9481
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009483/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009484 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009488{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009489 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009490
9491 rettv->v_type = VAR_FLOAT;
9492 if (get_float_arg(argvars, &f) == OK)
9493 rettv->vval.v_float = asin(f);
9494 else
9495 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496}
9497
9498/*
9499 * "atan()" function
9500 */
9501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009505
9506 rettv->v_type = VAR_FLOAT;
9507 if (get_float_arg(argvars, &f) == OK)
9508 rettv->vval.v_float = atan(f);
9509 else
9510 rettv->vval.v_float = 0.0;
9511}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009512
9513/*
9514 * "atan2()" function
9515 */
9516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009517f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009518{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009519 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009520
9521 rettv->v_type = VAR_FLOAT;
9522 if (get_float_arg(argvars, &fx) == OK
9523 && get_float_arg(&argvars[1], &fy) == OK)
9524 rettv->vval.v_float = atan2(fx, fy);
9525 else
9526 rettv->vval.v_float = 0.0;
9527}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009528#endif
9529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530/*
9531 * "browse(save, title, initdir, default)" function
9532 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536#ifdef FEAT_BROWSE
9537 int save;
9538 char_u *title;
9539 char_u *initdir;
9540 char_u *defname;
9541 char_u buf[NUMBUFLEN];
9542 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009543 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009545 save = get_tv_number_chk(&argvars[0], &error);
9546 title = get_tv_string_chk(&argvars[1]);
9547 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9548 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 if (error || title == NULL || initdir == NULL || defname == NULL)
9551 rettv->vval.v_string = NULL;
9552 else
9553 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554 do_browse(save ? BROWSE_SAVE : 0,
9555 title, defname, NULL, initdir, NULL, curbuf);
9556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009560}
9561
9562/*
9563 * "browsedir(title, initdir)" function
9564 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009566f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567{
9568#ifdef FEAT_BROWSE
9569 char_u *title;
9570 char_u *initdir;
9571 char_u buf[NUMBUFLEN];
9572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009573 title = get_tv_string_chk(&argvars[0]);
9574 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 if (title == NULL || initdir == NULL)
9577 rettv->vval.v_string = NULL;
9578 else
9579 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585}
9586
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009587static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589/*
9590 * Find a buffer by number or exact name.
9591 */
9592 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009593find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009597 if (avar->v_type == VAR_NUMBER)
9598 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009599 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009602 if (buf == NULL)
9603 {
9604 /* No full path name match, try a match with a URL or a "nofile"
9605 * buffer, these don't use the full path. */
9606 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9607 if (buf->b_fname != NULL
9608 && (path_with_url(buf->b_fname)
9609#ifdef FEAT_QUICKFIX
9610 || bt_nofile(buf)
9611#endif
9612 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009614 break;
9615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 }
9617 return buf;
9618}
9619
9620/*
9621 * "bufexists(expr)" function
9622 */
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "buflisted(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
9636
9637 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
9641/*
9642 * "bufloaded(expr)" function
9643 */
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645f_bufloaded(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_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 static buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009654buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 int save_magic;
9657 char_u *save_cpo;
9658 buf_T *buf;
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9661 save_magic = p_magic;
9662 p_magic = TRUE;
9663 save_cpo = p_cpo;
9664 p_cpo = (char_u *)"";
9665
9666 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009667 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
9669 p_magic = save_magic;
9670 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009671 return buf;
9672}
9673
9674/*
9675 * Get buffer by number or pattern.
9676 */
9677 static buf_T *
9678get_buf_tv(typval_T *tv, int curtab_only)
9679{
9680 char_u *name = tv->vval.v_string;
9681 buf_T *buf;
9682
9683 if (tv->v_type == VAR_NUMBER)
9684 return buflist_findnr((int)tv->vval.v_number);
9685 if (tv->v_type != VAR_STRING)
9686 return NULL;
9687 if (name == NULL || *name == NUL)
9688 return curbuf;
9689 if (name[0] == '$' && name[1] == NUL)
9690 return lastbuf;
9691
9692 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 /* If not found, try expanding the name, like done for bufexists(). */
9695 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697
9698 return buf;
9699}
9700
9701/*
9702 * "bufname(expr)" function
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 buf_T *buf;
9708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009711 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 --emsg_off;
9718}
9719
9720/*
9721 * "bufnr(expr)" function
9722 */
9723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009724f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 int error = FALSE;
9728 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009732 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 --emsg_off;
9734
9735 /* If the buffer isn't found and the second argument is not zero create a
9736 * new buffer. */
9737 if (buf == NULL
9738 && argvars[1].v_type != VAR_UNKNOWN
9739 && get_tv_number_chk(&argvars[1], &error) != 0
9740 && !error
9741 && (name = get_tv_string_chk(&argvars[0])) != NULL
9742 && !error)
9743 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9744
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749}
9750
9751/*
9752 * "bufwinnr(nr)" function
9753 */
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757#ifdef FEAT_WINDOWS
9758 win_T *wp;
9759 int winnr = 0;
9760#endif
9761 buf_T *buf;
9762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009765 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#ifdef FEAT_WINDOWS
9767 for (wp = firstwin; wp; wp = wp->w_next)
9768 {
9769 ++winnr;
9770 if (wp->w_buffer == buf)
9771 break;
9772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776#endif
9777 --emsg_off;
9778}
9779
9780/*
9781 * "byte2line(byte)" function
9782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009784f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#else
9789 long boff = 0;
9790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 (linenr_T)0, &boff);
9797#endif
9798}
9799
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009801byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009802{
9803#ifdef FEAT_MBYTE
9804 char_u *t;
9805#endif
9806 char_u *str;
9807 long idx;
9808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 str = get_tv_string_chk(&argvars[0]);
9810 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009813 return;
9814
9815#ifdef FEAT_MBYTE
9816 t = str;
9817 for ( ; idx > 0; idx--)
9818 {
9819 if (*t == NUL) /* EOL reached */
9820 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009821 if (enc_utf8 && comp)
9822 t += utf_ptr2len(t);
9823 else
9824 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009825 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009826 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009827#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009828 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009830#endif
9831}
9832
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009833/*
9834 * "byteidx()" function
9835 */
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009838{
9839 byteidx(argvars, rettv, FALSE);
9840}
9841
9842/*
9843 * "byteidxcomp()" function
9844 */
9845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009847{
9848 byteidx(argvars, rettv, TRUE);
9849}
9850
Bram Moolenaardb913952012-06-29 12:54:53 +02009851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852func_call(
9853 char_u *name,
9854 typval_T *args,
9855 dict_T *selfdict,
9856 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009857{
9858 listitem_T *item;
9859 typval_T argv[MAX_FUNC_ARGS + 1];
9860 int argc = 0;
9861 int dummy;
9862 int r = 0;
9863
9864 for (item = args->vval.v_list->lv_first; item != NULL;
9865 item = item->li_next)
9866 {
9867 if (argc == MAX_FUNC_ARGS)
9868 {
9869 EMSG(_("E699: Too many arguments"));
9870 break;
9871 }
9872 /* Make a copy of each argument. This is needed to be able to set
9873 * v_lock to VAR_FIXED in the copy without changing the original list.
9874 */
9875 copy_tv(&item->li_tv, &argv[argc++]);
9876 }
9877
9878 if (item == NULL)
9879 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9881 &dummy, TRUE, selfdict);
9882
9883 /* Free the arguments. */
9884 while (argc > 0)
9885 clear_tv(&argv[--argc]);
9886
9887 return r;
9888}
9889
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009890/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891 * "call(func, arglist)" function
9892 */
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895{
9896 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009898
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899 if (argvars[1].v_type != VAR_LIST)
9900 {
9901 EMSG(_(e_listreq));
9902 return;
9903 }
9904 if (argvars[1].vval.v_list == NULL)
9905 return;
9906
9907 if (argvars[0].v_type == VAR_FUNC)
9908 func = argvars[0].vval.v_string;
9909 else
9910 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 if (*func == NUL)
9912 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 if (argvars[2].v_type != VAR_UNKNOWN)
9915 {
9916 if (argvars[2].v_type != VAR_DICT)
9917 {
9918 EMSG(_(e_dictreq));
9919 return;
9920 }
9921 selfdict = argvars[2].vval.v_dict;
9922 }
9923
Bram Moolenaardb913952012-06-29 12:54:53 +02009924 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925}
9926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927#ifdef FEAT_FLOAT
9928/*
9929 * "ceil({float})" function
9930 */
9931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009932f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009934 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = ceil(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
9942#endif
9943
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009944#if defined(FEAT_JOB_CHANNEL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009945/*
9946 * Get a callback from "arg". It can be a Funcref or a function name.
9947 * When "arg" is zero return an empty string.
9948 * Return NULL for an invalid argument.
9949 */
9950 static char_u *
9951get_callback(typval_T *arg)
9952{
9953 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9954 return arg->vval.v_string;
9955 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9956 return (char_u *)"";
9957 EMSG(_("E999: Invalid callback argument"));
9958 return NULL;
9959}
9960
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009961 static int
9962handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9963{
9964 char_u *val = get_tv_string(item);
9965
9966 opt->jo_set |= jo;
9967 if (STRCMP(val, "nl") == 0)
9968 *modep = MODE_NL;
9969 else if (STRCMP(val, "raw") == 0)
9970 *modep = MODE_RAW;
9971 else if (STRCMP(val, "js") == 0)
9972 *modep = MODE_JS;
9973 else if (STRCMP(val, "json") == 0)
9974 *modep = MODE_JSON;
9975 else
9976 {
9977 EMSG2(_(e_invarg2), val);
9978 return FAIL;
9979 }
9980 return OK;
9981}
9982
Bram Moolenaar187db502016-02-27 14:44:26 +01009983 static int
9984handle_io(typval_T *item, int part, jobopt_T *opt)
9985{
9986 char_u *val = get_tv_string(item);
9987
9988 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9989 if (STRCMP(val, "null") == 0)
9990 opt->jo_io[part] = JIO_NULL;
9991 else if (STRCMP(val, "pipe") == 0)
9992 opt->jo_io[part] = JIO_PIPE;
9993 else if (STRCMP(val, "file") == 0)
9994 opt->jo_io[part] = JIO_FILE;
9995 else if (STRCMP(val, "buffer") == 0)
9996 opt->jo_io[part] = JIO_BUFFER;
9997 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9998 opt->jo_io[part] = JIO_OUT;
9999 else
10000 {
10001 EMSG2(_(e_invarg2), val);
10002 return FAIL;
10003 }
10004 return OK;
10005}
10006
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010007 static void
10008clear_job_options(jobopt_T *opt)
10009{
10010 vim_memset(opt, 0, sizeof(jobopt_T));
10011}
10012
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010013/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010014 * Get the PART_ number from the first character of an option name.
10015 */
10016 static int
10017part_from_char(int c)
10018{
10019 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10020}
10021
10022/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010023 * Get the option entries from the dict in "tv", parse them and put the result
10024 * in "opt".
10025 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010026 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010027 */
10028 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010029get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010030{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010031 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010032 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010033 dict_T *dict;
10034 int todo;
10035 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010036 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010037
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010038 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010039 if (tv->v_type == VAR_UNKNOWN)
10040 return OK;
10041 if (tv->v_type != VAR_DICT)
10042 {
10043 EMSG(_(e_invarg));
10044 return FAIL;
10045 }
10046 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010047 if (dict == NULL)
10048 return OK;
10049
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010050 todo = (int)dict->dv_hashtab.ht_used;
10051 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10052 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010053 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010054 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010055
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010056 if (STRCMP(hi->hi_key, "mode") == 0)
10057 {
10058 if (!(supported & JO_MODE))
10059 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010060 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010061 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010062 }
10063 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10064 {
10065 if (!(supported & JO_IN_MODE))
10066 break;
10067 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10068 == FAIL)
10069 return FAIL;
10070 }
10071 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10072 {
10073 if (!(supported & JO_OUT_MODE))
10074 break;
10075 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10076 == FAIL)
10077 return FAIL;
10078 }
10079 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10080 {
10081 if (!(supported & JO_ERR_MODE))
10082 break;
10083 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10084 == FAIL)
10085 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010086 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010087 else if (STRCMP(hi->hi_key, "in-io") == 0
10088 || STRCMP(hi->hi_key, "out-io") == 0
10089 || STRCMP(hi->hi_key, "err-io") == 0)
10090 {
10091 if (!(supported & JO_OUT_IO))
10092 break;
10093 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10094 return FAIL;
10095 }
10096 else if (STRCMP(hi->hi_key, "in-name") == 0
10097 || STRCMP(hi->hi_key, "out-name") == 0
10098 || STRCMP(hi->hi_key, "err-name") == 0)
10099 {
10100 part = part_from_char(*hi->hi_key);
10101
10102 if (!(supported & JO_OUT_IO))
10103 break;
10104 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10105 opt->jo_io_name[part] =
10106 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10107 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010010108 else if (STRCMP(hi->hi_key, "in-buf") == 0
10109 || STRCMP(hi->hi_key, "out-buf") == 0
10110 || STRCMP(hi->hi_key, "err-buf") == 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_BUF << (part - PART_OUT);
10117 opt->jo_io_buf[part] = get_tv_number(item);
10118 if (opt->jo_io_buf[part] <= 0)
10119 {
10120 EMSG2(_(e_invarg2), get_tv_string(item));
10121 return FAIL;
10122 }
10123 if (buflist_findnr(opt->jo_io_buf[part]) == NULL)
10124 {
10125 EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[part]);
10126 return FAIL;
10127 }
10128 }
Bram Moolenaar014069a2016-03-03 22:51:40 +010010129 else if (STRCMP(hi->hi_key, "in-top") == 0
10130 || STRCMP(hi->hi_key, "in-bot") == 0)
10131 {
10132 linenr_T *lp;
10133
10134 if (!(supported & JO_OUT_IO))
10135 break;
10136 if (hi->hi_key[3] == 't')
10137 {
10138 lp = &opt->jo_in_top;
10139 opt->jo_set |= JO_IN_TOP;
10140 }
10141 else
10142 {
10143 lp = &opt->jo_in_bot;
10144 opt->jo_set |= JO_IN_BOT;
10145 }
10146 *lp = get_tv_number(item);
10147 if (*lp < 0)
10148 {
10149 EMSG2(_(e_invarg2), get_tv_string(item));
10150 return FAIL;
10151 }
10152 }
Bram Moolenaarde279892016-03-11 22:19:44 +010010153 else if (STRCMP(hi->hi_key, "channel") == 0)
10154 {
10155 if (!(supported & JO_OUT_IO))
10156 break;
10157 opt->jo_set |= JO_CHANNEL;
10158 if (item->v_type != VAR_CHANNEL)
10159 {
10160 EMSG2(_(e_invarg2), "channel");
10161 return FAIL;
10162 }
10163 opt->jo_channel = item->vval.v_channel;
10164 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010165 else if (STRCMP(hi->hi_key, "callback") == 0)
10166 {
10167 if (!(supported & JO_CALLBACK))
10168 break;
10169 opt->jo_set |= JO_CALLBACK;
10170 opt->jo_callback = get_callback(item);
10171 if (opt->jo_callback == NULL)
10172 {
10173 EMSG2(_(e_invarg2), "callback");
10174 return FAIL;
10175 }
10176 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010177 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10178 {
10179 if (!(supported & JO_OUT_CALLBACK))
10180 break;
10181 opt->jo_set |= JO_OUT_CALLBACK;
10182 opt->jo_out_cb = get_callback(item);
10183 if (opt->jo_out_cb == NULL)
10184 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010185 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010186 return FAIL;
10187 }
10188 }
10189 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10190 {
10191 if (!(supported & JO_ERR_CALLBACK))
10192 break;
10193 opt->jo_set |= JO_ERR_CALLBACK;
10194 opt->jo_err_cb = get_callback(item);
10195 if (opt->jo_err_cb == NULL)
10196 {
10197 EMSG2(_(e_invarg2), "err-cb");
10198 return FAIL;
10199 }
10200 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010201 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10202 {
10203 if (!(supported & JO_CLOSE_CALLBACK))
10204 break;
10205 opt->jo_set |= JO_CLOSE_CALLBACK;
10206 opt->jo_close_cb = get_callback(item);
10207 if (opt->jo_close_cb == NULL)
10208 {
10209 EMSG2(_(e_invarg2), "close-cb");
10210 return FAIL;
10211 }
10212 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010213 else if (STRCMP(hi->hi_key, "waittime") == 0)
10214 {
10215 if (!(supported & JO_WAITTIME))
10216 break;
10217 opt->jo_set |= JO_WAITTIME;
10218 opt->jo_waittime = get_tv_number(item);
10219 }
10220 else if (STRCMP(hi->hi_key, "timeout") == 0)
10221 {
10222 if (!(supported & JO_TIMEOUT))
10223 break;
10224 opt->jo_set |= JO_TIMEOUT;
10225 opt->jo_timeout = get_tv_number(item);
10226 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010227 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10228 {
10229 if (!(supported & JO_OUT_TIMEOUT))
10230 break;
10231 opt->jo_set |= JO_OUT_TIMEOUT;
10232 opt->jo_out_timeout = get_tv_number(item);
10233 }
10234 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10235 {
10236 if (!(supported & JO_ERR_TIMEOUT))
10237 break;
10238 opt->jo_set |= JO_ERR_TIMEOUT;
10239 opt->jo_err_timeout = get_tv_number(item);
10240 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010241 else if (STRCMP(hi->hi_key, "part") == 0)
10242 {
10243 if (!(supported & JO_PART))
10244 break;
10245 opt->jo_set |= JO_PART;
10246 val = get_tv_string(item);
10247 if (STRCMP(val, "err") == 0)
10248 opt->jo_part = PART_ERR;
10249 else
10250 {
10251 EMSG2(_(e_invarg2), val);
10252 return FAIL;
10253 }
10254 }
10255 else if (STRCMP(hi->hi_key, "id") == 0)
10256 {
10257 if (!(supported & JO_ID))
10258 break;
10259 opt->jo_set |= JO_ID;
10260 opt->jo_id = get_tv_number(item);
10261 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010262 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10263 {
10264 if (!(supported & JO_STOPONEXIT))
10265 break;
10266 opt->jo_set |= JO_STOPONEXIT;
10267 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10268 opt->jo_soe_buf);
10269 if (opt->jo_stoponexit == NULL)
10270 {
10271 EMSG2(_(e_invarg2), "stoponexit");
10272 return FAIL;
10273 }
10274 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010275 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10276 {
10277 if (!(supported & JO_EXIT_CB))
10278 break;
10279 opt->jo_set |= JO_EXIT_CB;
10280 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010281 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010282 {
10283 EMSG2(_(e_invarg2), "exit-cb");
10284 return FAIL;
10285 }
10286 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010287 else
10288 break;
10289 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010290 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010291 if (todo > 0)
10292 {
10293 EMSG2(_(e_invarg2), hi->hi_key);
10294 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010295 }
10296
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010297 return OK;
10298}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010299#endif
10300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010302/*
10303 * Get the channel from the argument.
10304 * Returns NULL if the handle is invalid.
10305 */
10306 static channel_T *
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010307get_channel_arg(typval_T *tv, int check_open)
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010308{
Bram Moolenaar151f6562016-03-07 21:19:38 +010010309 channel_T *channel = NULL;
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010310
Bram Moolenaar151f6562016-03-07 21:19:38 +010010311 if (tv->v_type == VAR_JOB)
10312 {
10313 if (tv->vval.v_job != NULL)
10314 channel = tv->vval.v_job->jv_channel;
10315 }
10316 else if (tv->v_type == VAR_CHANNEL)
10317 {
10318 channel = tv->vval.v_channel;
10319 }
10320 else
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010321 {
10322 EMSG2(_(e_invarg2), get_tv_string(tv));
10323 return NULL;
10324 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010325
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010326 if (check_open && (channel == NULL || !channel_is_open(channel)))
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010327 {
10328 EMSG(_("E906: not an open channel"));
10329 return NULL;
10330 }
10331 return channel;
10332}
10333
10334/*
10335 * "ch_close()" function
10336 */
10337 static void
10338f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10339{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010340 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010341
10342 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010343 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010344 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010345 channel_clear(channel);
10346 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010347}
10348
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010349/*
10350 * "ch_getbufnr()" function
10351 */
10352 static void
10353f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10354{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010355 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010356
10357 rettv->vval.v_number = -1;
10358 if (channel != NULL)
10359 {
10360 char_u *what = get_tv_string(&argvars[1]);
10361 int part;
10362
10363 if (STRCMP(what, "err") == 0)
10364 part = PART_ERR;
10365 else if (STRCMP(what, "out") == 0)
10366 part = PART_OUT;
10367 else if (STRCMP(what, "in") == 0)
10368 part = PART_IN;
10369 else
10370 part = PART_SOCK;
10371 if (channel->ch_part[part].ch_buffer != NULL)
10372 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10373 }
10374}
10375
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010376/*
10377 * "ch_getjob()" function
10378 */
10379 static void
10380f_ch_getjob(typval_T *argvars, typval_T *rettv)
10381{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010382 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010383
10384 if (channel != NULL)
10385 {
10386 rettv->v_type = VAR_JOB;
10387 rettv->vval.v_job = channel->ch_job;
10388 if (channel->ch_job != NULL)
10389 ++channel->ch_job->jv_refcount;
10390 }
10391}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010392
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010393/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010394 * "ch_log()" function
10395 */
10396 static void
10397f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10398{
10399 char_u *msg = get_tv_string(&argvars[0]);
10400 channel_T *channel = NULL;
10401
10402 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010403 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010404
10405 ch_log(channel, (char *)msg);
10406}
10407
10408/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010409 * "ch_logfile()" function
10410 */
10411 static void
10412f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10413{
10414 char_u *fname;
10415 char_u *opt = (char_u *)"";
10416 char_u buf[NUMBUFLEN];
10417 FILE *file = NULL;
10418
10419 fname = get_tv_string(&argvars[0]);
10420 if (argvars[1].v_type == VAR_STRING)
10421 opt = get_tv_string_buf(&argvars[1], buf);
10422 if (*fname != NUL)
10423 {
10424 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10425 if (file == NULL)
10426 {
10427 EMSG2(_(e_notopen), fname);
10428 return;
10429 }
10430 }
10431 ch_logfile(file);
10432}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010433
10434/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010435 * "ch_open()" function
10436 */
10437 static void
10438f_ch_open(typval_T *argvars, typval_T *rettv)
10439{
10440 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010441 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010442 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010443 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010444 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010445 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010446
10447 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010448 rettv->v_type = VAR_CHANNEL;
10449 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010450
10451 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010452 if (argvars[1].v_type != VAR_UNKNOWN
10453 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010454 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010455 EMSG(_(e_invarg));
10456 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010457 }
10458
10459 /* parse address */
10460 p = vim_strchr(address, ':');
10461 if (p == NULL)
10462 {
10463 EMSG2(_(e_invarg2), address);
10464 return;
10465 }
10466 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010467 port = strtol((char *)p, &rest, 10);
10468 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010469 {
10470 p[-1] = ':';
10471 EMSG2(_(e_invarg2), address);
10472 return;
10473 }
10474
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010475 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010476 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010477 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010478 opt.jo_timeout = 2000;
10479 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010480 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010481 return;
10482 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010483 {
10484 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010485 return;
10486 }
10487
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010488 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010489 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010490 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010491 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010492 opt.jo_set = JO_ALL;
10493 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010494 }
10495}
10496
10497/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010498 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010499 */
10500 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010501common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010502{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010503 channel_T *channel;
10504 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010505 jobopt_T opt;
10506 int mode;
10507 int timeout;
10508 int id = -1;
10509 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010510
10511 /* return an empty string by default */
10512 rettv->v_type = VAR_STRING;
10513 rettv->vval.v_string = NULL;
10514
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010515 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010516 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10517 == FAIL)
10518 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010519
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010520 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010521 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010522 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010523 if (opt.jo_set & JO_PART)
10524 part = opt.jo_part;
10525 else
10526 part = channel_part_read(channel);
10527 mode = channel_get_mode(channel, part);
10528 timeout = channel_get_timeout(channel, part);
10529 if (opt.jo_set & JO_TIMEOUT)
10530 timeout = opt.jo_timeout;
10531
10532 if (raw || mode == MODE_RAW || mode == MODE_NL)
10533 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10534 else
10535 {
10536 if (opt.jo_set & JO_ID)
10537 id = opt.jo_id;
10538 channel_read_json_block(channel, part, timeout, id, &listtv);
10539 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010540 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010541 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010542 vim_free(listtv);
10543 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010544 else
10545 {
10546 rettv->v_type = VAR_SPECIAL;
10547 rettv->vval.v_number = VVAL_NONE;
10548 }
10549 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010550 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010551}
10552
10553/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010554 * "ch_read()" function
10555 */
10556 static void
10557f_ch_read(typval_T *argvars, typval_T *rettv)
10558{
10559 common_channel_read(argvars, rettv, FALSE);
10560}
10561
10562/*
10563 * "ch_readraw()" function
10564 */
10565 static void
10566f_ch_readraw(typval_T *argvars, typval_T *rettv)
10567{
10568 common_channel_read(argvars, rettv, TRUE);
10569}
10570
10571/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010572 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010573 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010574 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010575 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010576 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010577 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010578send_common(
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010579 typval_T *argvars,
10580 char_u *text,
10581 int id,
10582 int eval,
10583 jobopt_T *opt,
10584 char *fun,
10585 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010586{
Bram Moolenaar77073442016-02-13 23:23:53 +010010587 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010588 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010589
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010590 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010591 if (channel == NULL)
10592 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010593 part_send = channel_part_send(channel);
10594 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010595
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010596 clear_job_options(opt);
10597 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010598 return NULL;
10599
Bram Moolenaara07fec92016-02-05 21:04:08 +010010600 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010601 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10602 * allowed. */
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010603 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010604 {
10605 if (eval)
10606 {
10607 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10608 return NULL;
10609 }
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010610 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010611 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010612
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010613 if (channel_send(channel, part_send, text, fun) == OK
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010614 && opt->jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010615 return channel;
10616 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010617}
10618
10619/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010620 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010621 */
10622 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010623ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010624{
10625 char_u *text;
10626 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010627 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010628 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010629 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010630 int part_send;
10631 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010632 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010633 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010634
10635 /* return an empty string by default */
10636 rettv->v_type = VAR_STRING;
10637 rettv->vval.v_string = NULL;
10638
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010639 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010640 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010641 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010642 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010643
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010644 ch_mode = channel_get_mode(channel, part_send);
10645 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010646 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010647 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010648 return;
10649 }
10650
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010651 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010652 text = json_encode_nr_expr(id, &argvars[1],
10653 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010654 if (text == NULL)
10655 return;
10656
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010657 channel = send_common(argvars, text, id, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010658 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010659 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010660 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010661 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010662 if (opt.jo_set & JO_TIMEOUT)
10663 timeout = opt.jo_timeout;
10664 else
10665 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010666 timeout = channel_get_timeout(channel, part_read);
10667 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10668 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010669 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010670 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010671
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010672 /* Move the item from the list and then change the type to
10673 * avoid the value being freed. */
10674 *rettv = list->lv_last->li_tv;
10675 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010676 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010677 }
10678 }
10679}
10680
10681/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010682 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010683 */
10684 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010685f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10686{
10687 ch_expr_common(argvars, rettv, TRUE);
10688}
10689
10690/*
10691 * "ch_sendexpr()" function
10692 */
10693 static void
10694f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10695{
10696 ch_expr_common(argvars, rettv, FALSE);
10697}
10698
10699/*
10700 * common for "ch_evalraw()" and "ch_sendraw()"
10701 */
10702 static void
10703ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010704{
10705 char_u buf[NUMBUFLEN];
10706 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010707 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010708 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010709 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010710 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010711
10712 /* return an empty string by default */
10713 rettv->v_type = VAR_STRING;
10714 rettv->vval.v_string = NULL;
10715
10716 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010717 channel = send_common(argvars, text, 0, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010718 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10719 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010720 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010721 if (opt.jo_set & JO_TIMEOUT)
10722 timeout = opt.jo_timeout;
10723 else
10724 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010725 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10726 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010727}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010728
10729/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010730 * "ch_evalraw()" function
10731 */
10732 static void
10733f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10734{
10735 ch_raw_common(argvars, rettv, TRUE);
10736}
10737
10738/*
10739 * "ch_sendraw()" function
10740 */
10741 static void
10742f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10743{
10744 ch_raw_common(argvars, rettv, FALSE);
10745}
10746
10747/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010748 * "ch_setoptions()" function
10749 */
10750 static void
10751f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10752{
10753 channel_T *channel;
10754 jobopt_T opt;
10755
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010756 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010757 if (channel == NULL)
10758 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010759 clear_job_options(&opt);
10760 if (get_job_options(&argvars[1], &opt,
10761 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010762 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010763 channel_set_options(channel, &opt);
10764}
10765
10766/*
10767 * "ch_status()" function
10768 */
10769 static void
10770f_ch_status(typval_T *argvars, typval_T *rettv)
10771{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010772 channel_T *channel;
10773
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010774 /* return an empty string by default */
10775 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010776 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010777
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010778 channel = get_channel_arg(&argvars[0], FALSE);
10779 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010780}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010781#endif
10782
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010783/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010784 * "changenr()" function
10785 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010787f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010788{
10789 rettv->vval.v_number = curbuf->b_u_seq_cur;
10790}
10791
10792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 * "char2nr(string)" function
10794 */
10795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010796f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797{
10798#ifdef FEAT_MBYTE
10799 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010800 {
10801 int utf8 = 0;
10802
10803 if (argvars[1].v_type != VAR_UNKNOWN)
10804 utf8 = get_tv_number_chk(&argvars[1], NULL);
10805
10806 if (utf8)
10807 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10808 else
10809 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 else
10812#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814}
10815
10816/*
10817 * "cindent(lnum)" function
10818 */
10819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821{
10822#ifdef FEAT_CINDENT
10823 pos_T pos;
10824 linenr_T lnum;
10825
10826 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10829 {
10830 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 curwin->w_cursor = pos;
10833 }
10834 else
10835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837}
10838
10839/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010840 * "clearmatches()" function
10841 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010843f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010844{
10845#ifdef FEAT_SEARCH_EXTRA
10846 clear_matches(curwin);
10847#endif
10848}
10849
10850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 * "col(string)" function
10852 */
10853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010854f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855{
10856 colnr_T col = 0;
10857 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010858 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010860 fp = var2fpos(&argvars[0], FALSE, &fnum);
10861 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 {
10863 if (fp->col == MAXCOL)
10864 {
10865 /* '> can be MAXCOL, get the length of the line then */
10866 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010867 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 else
10869 col = MAXCOL;
10870 }
10871 else
10872 {
10873 col = fp->col + 1;
10874#ifdef FEAT_VIRTUALEDIT
10875 /* col(".") when the cursor is on the NUL at the end of the line
10876 * because of "coladd" can be seen as an extra column. */
10877 if (virtual_active() && fp == &curwin->w_cursor)
10878 {
10879 char_u *p = ml_get_cursor();
10880
10881 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10882 curwin->w_virtcol - curwin->w_cursor.coladd))
10883 {
10884# ifdef FEAT_MBYTE
10885 int l;
10886
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010887 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 col += l;
10889# else
10890 if (*p != NUL && p[1] == NUL)
10891 ++col;
10892# endif
10893 }
10894 }
10895#endif
10896 }
10897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010898 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899}
10900
Bram Moolenaar572cb562005-08-05 21:35:02 +000010901#if defined(FEAT_INS_EXPAND)
10902/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010903 * "complete()" function
10904 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010906f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010907{
10908 int startcol;
10909
10910 if ((State & INSERT) == 0)
10911 {
10912 EMSG(_("E785: complete() can only be used in Insert mode"));
10913 return;
10914 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010915
10916 /* Check for undo allowed here, because if something was already inserted
10917 * the line was already saved for undo and this check isn't done. */
10918 if (!undo_allowed())
10919 return;
10920
Bram Moolenaarade00832006-03-10 21:46:58 +000010921 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10922 {
10923 EMSG(_(e_invarg));
10924 return;
10925 }
10926
10927 startcol = get_tv_number_chk(&argvars[0], NULL);
10928 if (startcol <= 0)
10929 return;
10930
10931 set_completion(startcol - 1, argvars[1].vval.v_list);
10932}
10933
10934/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010935 * "complete_add()" function
10936 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010938f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010939{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010940 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010941}
10942
10943/*
10944 * "complete_check()" function
10945 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010947f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010948{
10949 int saved = RedrawingDisabled;
10950
10951 RedrawingDisabled = 0;
10952 ins_compl_check_keys(0);
10953 rettv->vval.v_number = compl_interrupted;
10954 RedrawingDisabled = saved;
10955}
10956#endif
10957
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958/*
10959 * "confirm(message, buttons[, default [, type]])" function
10960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010962f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963{
10964#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10965 char_u *message;
10966 char_u *buttons = NULL;
10967 char_u buf[NUMBUFLEN];
10968 char_u buf2[NUMBUFLEN];
10969 int def = 1;
10970 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 char_u *typestr;
10972 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 message = get_tv_string_chk(&argvars[0]);
10975 if (message == NULL)
10976 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010977 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10980 if (buttons == NULL)
10981 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010982 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010984 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010985 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10988 if (typestr == NULL)
10989 error = TRUE;
10990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010992 switch (TOUPPER_ASC(*typestr))
10993 {
10994 case 'E': type = VIM_ERROR; break;
10995 case 'Q': type = VIM_QUESTION; break;
10996 case 'I': type = VIM_INFO; break;
10997 case 'W': type = VIM_WARNING; break;
10998 case 'G': type = VIM_GENERIC; break;
10999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 }
11001 }
11002 }
11003 }
11004
11005 if (buttons == NULL || *buttons == NUL)
11006 buttons = (char_u *)_("&Ok");
11007
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011008 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010011010 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011#endif
11012}
11013
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014/*
11015 * "copy()" function
11016 */
11017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011018f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011019{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011020 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011021}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011023#ifdef FEAT_FLOAT
11024/*
11025 * "cos()" function
11026 */
11027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011028f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011029{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011030 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011031
11032 rettv->v_type = VAR_FLOAT;
11033 if (get_float_arg(argvars, &f) == OK)
11034 rettv->vval.v_float = cos(f);
11035 else
11036 rettv->vval.v_float = 0.0;
11037}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011038
11039/*
11040 * "cosh()" function
11041 */
11042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011043f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011044{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011045 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011046
11047 rettv->v_type = VAR_FLOAT;
11048 if (get_float_arg(argvars, &f) == OK)
11049 rettv->vval.v_float = cosh(f);
11050 else
11051 rettv->vval.v_float = 0.0;
11052}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011053#endif
11054
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011056 * "count()" function
11057 */
11058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011059f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011060{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011061 long n = 0;
11062 int ic = FALSE;
11063
Bram Moolenaare9a41262005-01-15 22:18:47 +000011064 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011065 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011066 listitem_T *li;
11067 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011068 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011069
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 if ((l = argvars[0].vval.v_list) != NULL)
11071 {
11072 li = l->lv_first;
11073 if (argvars[2].v_type != VAR_UNKNOWN)
11074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011075 int error = FALSE;
11076
11077 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 if (argvars[3].v_type != VAR_UNKNOWN)
11079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 idx = get_tv_number_chk(&argvars[3], &error);
11081 if (!error)
11082 {
11083 li = list_find(l, idx);
11084 if (li == NULL)
11085 EMSGN(_(e_listidx), idx);
11086 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011087 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 if (error)
11089 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011090 }
11091
11092 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011093 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 ++n;
11095 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011096 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011097 else if (argvars[0].v_type == VAR_DICT)
11098 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 int todo;
11100 dict_T *d;
11101 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011102
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011103 if ((d = argvars[0].vval.v_dict) != NULL)
11104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105 int error = FALSE;
11106
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 if (argvars[2].v_type != VAR_UNKNOWN)
11108 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 if (argvars[3].v_type != VAR_UNKNOWN)
11111 EMSG(_(e_invarg));
11112 }
11113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011114 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011115 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011116 {
11117 if (!HASHITEM_EMPTY(hi))
11118 {
11119 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011120 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011121 ++n;
11122 }
11123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011124 }
11125 }
11126 else
11127 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011128 rettv->vval.v_number = n;
11129}
11130
11131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11133 *
11134 * Checks the existence of a cscope connection.
11135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011137f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
11139#ifdef FEAT_CSCOPE
11140 int num = 0;
11141 char_u *dbpath = NULL;
11142 char_u *prepend = NULL;
11143 char_u buf[NUMBUFLEN];
11144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011145 if (argvars[0].v_type != VAR_UNKNOWN
11146 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 num = (int)get_tv_number(&argvars[0]);
11149 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 }
11153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011154 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155#endif
11156}
11157
11158/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011159 * "cursor(lnum, col)" function, or
11160 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011162 * Moves the cursor to the specified line and column.
11163 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011166f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011169#ifdef FEAT_VIRTUALEDIT
11170 long coladd = 0;
11171#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011172 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011174 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011175 if (argvars[1].v_type == VAR_UNKNOWN)
11176 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011177 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011178 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011179
Bram Moolenaar493c1782014-05-28 14:34:46 +020011180 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011181 {
11182 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011183 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011184 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011185 line = pos.lnum;
11186 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011187#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011188 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011189#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011190 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011191 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011192 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011193 set_curswant = FALSE;
11194 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011195 }
11196 else
11197 {
11198 line = get_tv_lnum(argvars);
11199 col = get_tv_number_chk(&argvars[1], NULL);
11200#ifdef FEAT_VIRTUALEDIT
11201 if (argvars[2].v_type != VAR_UNKNOWN)
11202 coladd = get_tv_number_chk(&argvars[2], NULL);
11203#endif
11204 }
11205 if (line < 0 || col < 0
11206#ifdef FEAT_VIRTUALEDIT
11207 || coladd < 0
11208#endif
11209 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 if (line > 0)
11212 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213 if (col > 0)
11214 curwin->w_cursor.col = col - 1;
11215#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011216 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217#endif
11218
11219 /* Make sure the cursor is in a valid position. */
11220 check_cursor();
11221#ifdef FEAT_MBYTE
11222 /* Correct cursor for multi-byte character. */
11223 if (has_mbyte)
11224 mb_adjust_cursor();
11225#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011226
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011227 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011228 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229}
11230
11231/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011232 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233 */
11234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011235f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011237 int noref = 0;
11238
11239 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011241 if (noref < 0 || noref > 1)
11242 EMSG(_(e_invarg));
11243 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011244 {
11245 current_copyID += COPYID_INC;
11246 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248}
11249
11250/*
11251 * "delete()" function
11252 */
11253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011254f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011256 char_u nbuf[NUMBUFLEN];
11257 char_u *name;
11258 char_u *flags;
11259
11260 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011262 return;
11263
11264 name = get_tv_string(&argvars[0]);
11265 if (name == NULL || *name == NUL)
11266 {
11267 EMSG(_(e_invarg));
11268 return;
11269 }
11270
11271 if (argvars[1].v_type != VAR_UNKNOWN)
11272 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011274 flags = (char_u *)"";
11275
11276 if (*flags == NUL)
11277 /* delete a file */
11278 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11279 else if (STRCMP(flags, "d") == 0)
11280 /* delete an empty directory */
11281 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11282 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011283 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011284 rettv->vval.v_number = delete_recursive(name);
11285 else
11286 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287}
11288
11289/*
11290 * "did_filetype()" function
11291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011293f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294{
11295#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297#endif
11298}
11299
11300/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011301 * "diff_filler()" function
11302 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011304f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011305{
11306#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011308#endif
11309}
11310
11311/*
11312 * "diff_hlID()" function
11313 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011315f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011316{
11317#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011319 static linenr_T prev_lnum = 0;
11320 static int changedtick = 0;
11321 static int fnum = 0;
11322 static int change_start = 0;
11323 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011324 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011325 int filler_lines;
11326 int col;
11327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328 if (lnum < 0) /* ignore type error in {lnum} arg */
11329 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011330 if (lnum != prev_lnum
11331 || changedtick != curbuf->b_changedtick
11332 || fnum != curbuf->b_fnum)
11333 {
11334 /* New line, buffer, change: need to get the values. */
11335 filler_lines = diff_check(curwin, lnum);
11336 if (filler_lines < 0)
11337 {
11338 if (filler_lines == -1)
11339 {
11340 change_start = MAXCOL;
11341 change_end = -1;
11342 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11343 hlID = HLF_ADD; /* added line */
11344 else
11345 hlID = HLF_CHD; /* changed line */
11346 }
11347 else
11348 hlID = HLF_ADD; /* added line */
11349 }
11350 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011351 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011352 prev_lnum = lnum;
11353 changedtick = curbuf->b_changedtick;
11354 fnum = curbuf->b_fnum;
11355 }
11356
11357 if (hlID == HLF_CHD || hlID == HLF_TXD)
11358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011360 if (col >= change_start && col <= change_end)
11361 hlID = HLF_TXD; /* changed text */
11362 else
11363 hlID = HLF_CHD; /* changed line */
11364 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011365 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011366#endif
11367}
11368
11369/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011370 * "disable_char_avail_for_testing({expr})" function
11371 */
11372 static void
11373f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11374{
11375 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11376}
11377
11378/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011379 * "empty({expr})" function
11380 */
11381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011382f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011383{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011384 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011385
11386 switch (argvars[0].v_type)
11387 {
11388 case VAR_STRING:
11389 case VAR_FUNC:
11390 n = argvars[0].vval.v_string == NULL
11391 || *argvars[0].vval.v_string == NUL;
11392 break;
11393 case VAR_NUMBER:
11394 n = argvars[0].vval.v_number == 0;
11395 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011396 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011397#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011398 n = argvars[0].vval.v_float == 0.0;
11399 break;
11400#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011401 case VAR_LIST:
11402 n = argvars[0].vval.v_list == NULL
11403 || argvars[0].vval.v_list->lv_first == NULL;
11404 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011405 case VAR_DICT:
11406 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011407 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011408 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011409 case VAR_SPECIAL:
11410 n = argvars[0].vval.v_number != VVAL_TRUE;
11411 break;
11412
Bram Moolenaar835dc632016-02-07 14:27:38 +010011413 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011414#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011415 n = argvars[0].vval.v_job == NULL
11416 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11417 break;
11418#endif
11419 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011420#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011421 n = argvars[0].vval.v_channel == NULL
11422 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011423 break;
11424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011425 case VAR_UNKNOWN:
11426 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11427 n = TRUE;
11428 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011429 }
11430
11431 rettv->vval.v_number = n;
11432}
11433
11434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 * "escape({string}, {chars})" function
11436 */
11437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011438f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440 char_u buf[NUMBUFLEN];
11441
Bram Moolenaar758711c2005-02-02 23:11:38 +000011442 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11443 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445}
11446
11447/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011448 * "eval()" function
11449 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011451f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011452{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011453 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 s = get_tv_string_chk(&argvars[0]);
11456 if (s != NULL)
11457 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011458
Bram Moolenaar615b9972015-01-14 17:15:05 +010011459 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11461 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011462 if (p != NULL && !aborting())
11463 EMSG2(_(e_invexpr2), p);
11464 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011466 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011467 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011468 else if (*s != NUL)
11469 EMSG(_(e_trailing));
11470}
11471
11472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 * "eventhandler()" function
11474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011476f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479}
11480
11481/*
11482 * "executable()" function
11483 */
11484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011485f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011487 char_u *name = get_tv_string(&argvars[0]);
11488
11489 /* Check in $PATH and also check directly if there is a directory name. */
11490 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11491 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011492}
11493
11494/*
11495 * "exepath()" function
11496 */
11497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011498f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011499{
11500 char_u *p = NULL;
11501
Bram Moolenaarb5971142015-03-21 17:32:19 +010011502 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011503 rettv->v_type = VAR_STRING;
11504 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505}
11506
11507/*
11508 * "exists()" function
11509 */
11510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011511f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
11513 char_u *p;
11514 char_u *name;
11515 int n = FALSE;
11516 int len = 0;
11517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 if (*p == '$') /* environment variable */
11520 {
11521 /* first try "normal" environment variables (fast) */
11522 if (mch_getenv(p + 1) != NULL)
11523 n = TRUE;
11524 else
11525 {
11526 /* try expanding things like $VIM and ${HOME} */
11527 p = expand_env_save(p);
11528 if (p != NULL && *p != '$')
11529 n = TRUE;
11530 vim_free(p);
11531 }
11532 }
11533 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011534 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011536 if (*skipwhite(p) != NUL)
11537 n = FALSE; /* trailing garbage */
11538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539 else if (*p == '*') /* internal or user defined function */
11540 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011541 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543 else if (*p == ':')
11544 {
11545 n = cmd_exists(p + 1);
11546 }
11547 else if (*p == '#')
11548 {
11549#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011550 if (p[1] == '#')
11551 n = autocmd_supported(p + 2);
11552 else
11553 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554#endif
11555 }
11556 else /* internal variable */
11557 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011558 char_u *tofree;
11559 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011560
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011561 /* get_name_len() takes care of expanding curly braces */
11562 name = p;
11563 len = get_name_len(&p, &tofree, TRUE, FALSE);
11564 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011566 if (tofree != NULL)
11567 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011568 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011569 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011571 /* handle d.key, l[idx], f(expr) */
11572 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11573 if (n)
11574 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 }
11576 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011577 if (*p != NUL)
11578 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011580 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 }
11582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584}
11585
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011586#ifdef FEAT_FLOAT
11587/*
11588 * "exp()" function
11589 */
11590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011591f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011592{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011593 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011594
11595 rettv->v_type = VAR_FLOAT;
11596 if (get_float_arg(argvars, &f) == OK)
11597 rettv->vval.v_float = exp(f);
11598 else
11599 rettv->vval.v_float = 0.0;
11600}
11601#endif
11602
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603/*
11604 * "expand()" function
11605 */
11606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011607f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011608{
11609 char_u *s;
11610 int len;
11611 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011612 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011614 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011615 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011618 if (argvars[1].v_type != VAR_UNKNOWN
11619 && argvars[2].v_type != VAR_UNKNOWN
11620 && get_tv_number_chk(&argvars[2], &error)
11621 && !error)
11622 {
11623 rettv->v_type = VAR_LIST;
11624 rettv->vval.v_list = NULL;
11625 }
11626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 if (*s == '%' || *s == '#' || *s == '<')
11629 {
11630 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011631 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011633 if (rettv->v_type == VAR_LIST)
11634 {
11635 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11636 list_append_string(rettv->vval.v_list, result, -1);
11637 else
11638 vim_free(result);
11639 }
11640 else
11641 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 }
11643 else
11644 {
11645 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011646 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 if (argvars[1].v_type != VAR_UNKNOWN
11648 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011649 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011650 if (!error)
11651 {
11652 ExpandInit(&xpc);
11653 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011654 if (p_wic)
11655 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011656 if (rettv->v_type == VAR_STRING)
11657 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11658 options, WILD_ALL);
11659 else if (rettv_list_alloc(rettv) != FAIL)
11660 {
11661 int i;
11662
11663 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11664 for (i = 0; i < xpc.xp_numfiles; i++)
11665 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11666 ExpandCleanup(&xpc);
11667 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 }
11669 else
11670 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 }
11672}
11673
11674/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011675 * Go over all entries in "d2" and add them to "d1".
11676 * When "action" is "error" then a duplicate key is an error.
11677 * When "action" is "force" then a duplicate key is overwritten.
11678 * Otherwise duplicate keys are ignored ("action" is "keep").
11679 */
11680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011681dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011682{
11683 dictitem_T *di1;
11684 hashitem_T *hi2;
11685 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011686 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011687
11688 todo = (int)d2->dv_hashtab.ht_used;
11689 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11690 {
11691 if (!HASHITEM_EMPTY(hi2))
11692 {
11693 --todo;
11694 di1 = dict_find(d1, hi2->hi_key, -1);
11695 if (d1->dv_scope != 0)
11696 {
11697 /* Disallow replacing a builtin function in l: and g:.
11698 * Check the key to be valid when adding to any
11699 * scope. */
11700 if (d1->dv_scope == VAR_DEF_SCOPE
11701 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11702 && var_check_func_name(hi2->hi_key,
11703 di1 == NULL))
11704 break;
11705 if (!valid_varname(hi2->hi_key))
11706 break;
11707 }
11708 if (di1 == NULL)
11709 {
11710 di1 = dictitem_copy(HI2DI(hi2));
11711 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11712 dictitem_free(di1);
11713 }
11714 else if (*action == 'e')
11715 {
11716 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11717 break;
11718 }
11719 else if (*action == 'f' && HI2DI(hi2) != di1)
11720 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011721 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11722 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011723 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011724 clear_tv(&di1->di_tv);
11725 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11726 }
11727 }
11728 }
11729}
11730
11731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011732 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011734 */
11735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011736f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011737{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011738 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011739
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011741 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011742 list_T *l1, *l2;
11743 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011746
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 l1 = argvars[0].vval.v_list;
11748 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011749 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011750 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 {
11752 if (argvars[2].v_type != VAR_UNKNOWN)
11753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 before = get_tv_number_chk(&argvars[2], &error);
11755 if (error)
11756 return; /* type error; errmsg already given */
11757
Bram Moolenaar758711c2005-02-02 23:11:38 +000011758 if (before == l1->lv_len)
11759 item = NULL;
11760 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011762 item = list_find(l1, before);
11763 if (item == NULL)
11764 {
11765 EMSGN(_(e_listidx), before);
11766 return;
11767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011768 }
11769 }
11770 else
11771 item = NULL;
11772 list_extend(l1, l2, item);
11773
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 copy_tv(&argvars[0], rettv);
11775 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011776 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011777 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11778 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011779 dict_T *d1, *d2;
11780 char_u *action;
11781 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782
11783 d1 = argvars[0].vval.v_dict;
11784 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011785 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011786 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011787 {
11788 /* Check the third argument. */
11789 if (argvars[2].v_type != VAR_UNKNOWN)
11790 {
11791 static char *(av[]) = {"keep", "force", "error"};
11792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793 action = get_tv_string_chk(&argvars[2]);
11794 if (action == NULL)
11795 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011796 for (i = 0; i < 3; ++i)
11797 if (STRCMP(action, av[i]) == 0)
11798 break;
11799 if (i == 3)
11800 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011801 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011802 return;
11803 }
11804 }
11805 else
11806 action = (char_u *)"force";
11807
Bram Moolenaara9922d62013-05-30 13:01:18 +020011808 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011809
Bram Moolenaare9a41262005-01-15 22:18:47 +000011810 copy_tv(&argvars[0], rettv);
11811 }
11812 }
11813 else
11814 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011815}
11816
11817/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011818 * "feedkeys()" function
11819 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011821f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011822{
11823 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011824 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011825 char_u *keys, *flags;
11826 char_u nbuf[NUMBUFLEN];
11827 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011828 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011829 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011830
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011831 /* This is not allowed in the sandbox. If the commands would still be
11832 * executed in the sandbox it would be OK, but it probably happens later,
11833 * when "sandbox" is no longer set. */
11834 if (check_secure())
11835 return;
11836
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011837 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011838
11839 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011840 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011841 flags = get_tv_string_buf(&argvars[1], nbuf);
11842 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011843 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011844 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011845 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011846 case 'n': remap = FALSE; break;
11847 case 'm': remap = TRUE; break;
11848 case 't': typed = TRUE; break;
11849 case 'i': insert = TRUE; break;
11850 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011851 }
11852 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011853 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011854
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011855 if (*keys != NUL || execute)
11856 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011857 /* Need to escape K_SPECIAL and CSI before putting the string in the
11858 * typeahead buffer. */
11859 keys_esc = vim_strsave_escape_csi(keys);
11860 if (keys_esc != NULL)
11861 {
11862 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011863 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011864 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011865 if (vgetc_busy)
11866 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011867 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011868 {
11869 int save_msg_scroll = msg_scroll;
11870
11871 /* Avoid a 1 second delay when the keys start Insert mode. */
11872 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011873 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011874 msg_scroll |= save_msg_scroll;
11875 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011876 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011877 }
11878}
11879
11880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 * "filereadable()" function
11882 */
11883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011884f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011886 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887 char_u *p;
11888 int n;
11889
Bram Moolenaarc236c162008-07-13 17:41:49 +000011890#ifndef O_NONBLOCK
11891# define O_NONBLOCK 0
11892#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011893 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011894 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11895 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896 {
11897 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011898 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 }
11900 else
11901 n = FALSE;
11902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904}
11905
11906/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011907 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908 * rights to write into.
11909 */
11910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011911f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011913 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914}
11915
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011916 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011917findfilendir(
11918 typval_T *argvars UNUSED,
11919 typval_T *rettv,
11920 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011921{
11922#ifdef FEAT_SEARCHPATH
11923 char_u *fname;
11924 char_u *fresult = NULL;
11925 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11926 char_u *p;
11927 char_u pathbuf[NUMBUFLEN];
11928 int count = 1;
11929 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011930 int error = FALSE;
11931#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011932
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011933 rettv->vval.v_string = NULL;
11934 rettv->v_type = VAR_STRING;
11935
11936#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011937 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011938
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011939 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11942 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011943 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 else
11945 {
11946 if (*p != NUL)
11947 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011950 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011952 }
11953
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011954 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11955 error = TRUE;
11956
11957 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 do
11960 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011961 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011962 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 fresult = find_file_in_path_option(first ? fname : NULL,
11964 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011965 0, first, path,
11966 find_what,
11967 curbuf->b_ffname,
11968 find_what == FINDFILE_DIR
11969 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011971
11972 if (fresult != NULL && rettv->v_type == VAR_LIST)
11973 list_append_string(rettv->vval.v_list, fresult, -1);
11974
11975 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011977
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011978 if (rettv->v_type == VAR_STRING)
11979 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011980#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011981}
11982
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011983static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11984static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011985
11986/*
11987 * Implementation of map() and filter().
11988 */
11989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011990filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011991{
11992 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 listitem_T *li, *nli;
11995 list_T *l = NULL;
11996 dictitem_T *di;
11997 hashtab_T *ht;
11998 hashitem_T *hi;
11999 dict_T *d = NULL;
12000 typval_T save_val;
12001 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012003 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012004 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012005 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012006 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012007 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012008 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009
Bram Moolenaare9a41262005-01-15 22:18:47 +000012010 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012011 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012012 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012013 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012014 return;
12015 }
12016 else if (argvars[0].v_type == VAR_DICT)
12017 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012018 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012019 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012020 return;
12021 }
12022 else
12023 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012024 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012025 return;
12026 }
12027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 expr = get_tv_string_buf_chk(&argvars[1], buf);
12029 /* On type errors, the preceding call has already displayed an error
12030 * message. Avoid a misleading error message for an empty string that
12031 * was not passed as argument. */
12032 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 prepare_vimvar(VV_VAL, &save_val);
12035 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012036
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012037 /* We reset "did_emsg" to be able to detect whether an error
12038 * occurred during evaluation of the expression. */
12039 save_did_emsg = did_emsg;
12040 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012041
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012042 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012043 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 vimvars[VV_KEY].vv_type = VAR_STRING;
12046
12047 ht = &d->dv_hashtab;
12048 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012049 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012051 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 if (!HASHITEM_EMPTY(hi))
12053 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012054 int r;
12055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056 --todo;
12057 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012058 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012059 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12060 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 break;
12062 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012063 r = filter_map_one(&di->di_tv, expr, map, &rem);
12064 clear_tv(&vimvars[VV_KEY].vv_tv);
12065 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 break;
12067 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012068 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012069 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12070 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012071 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 }
12075 }
12076 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 }
12078 else
12079 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012080 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 for (li = l->lv_first; li != NULL; li = nli)
12083 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012084 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012085 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012086 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012087 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012088 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012089 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012090 break;
12091 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012092 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012093 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012094 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012096
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012097 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012099
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012100 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012102
12103 copy_tv(&argvars[0], rettv);
12104}
12105
12106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012107filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108{
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012111 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012112
Bram Moolenaar33570922005-01-25 22:26:29 +000012113 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 s = expr;
12115 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012116 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012117 if (*s != NUL) /* check for trailing chars after expr */
12118 {
12119 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012120 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012121 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 }
12123 if (map)
12124 {
12125 /* map(): replace the list item value */
12126 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012127 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 *tv = rettv;
12129 }
12130 else
12131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012132 int error = FALSE;
12133
Bram Moolenaare9a41262005-01-15 22:18:47 +000012134 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012136 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 /* On type error, nothing has been removed; return FAIL to stop the
12138 * loop. The error message was given by get_tv_number_chk(). */
12139 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012140 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012141 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012142 retval = OK;
12143theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012144 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012145 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012146}
12147
12148/*
12149 * "filter()" function
12150 */
12151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012152f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012153{
12154 filter_map(argvars, rettv, FALSE);
12155}
12156
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158 * "finddir({fname}[, {path}[, {count}]])" function
12159 */
12160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012161f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012162{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012163 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164}
12165
12166/*
12167 * "findfile({fname}[, {path}[, {count}]])" function
12168 */
12169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012172 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012173}
12174
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012175#ifdef FEAT_FLOAT
12176/*
12177 * "float2nr({float})" function
12178 */
12179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012181{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012182 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012183
12184 if (get_float_arg(argvars, &f) == OK)
12185 {
12186 if (f < -0x7fffffff)
12187 rettv->vval.v_number = -0x7fffffff;
12188 else if (f > 0x7fffffff)
12189 rettv->vval.v_number = 0x7fffffff;
12190 else
12191 rettv->vval.v_number = (varnumber_T)f;
12192 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012193}
12194
12195/*
12196 * "floor({float})" function
12197 */
12198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012199f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012200{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012201 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012202
12203 rettv->v_type = VAR_FLOAT;
12204 if (get_float_arg(argvars, &f) == OK)
12205 rettv->vval.v_float = floor(f);
12206 else
12207 rettv->vval.v_float = 0.0;
12208}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012209
12210/*
12211 * "fmod()" function
12212 */
12213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012215{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012216 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012217
12218 rettv->v_type = VAR_FLOAT;
12219 if (get_float_arg(argvars, &fx) == OK
12220 && get_float_arg(&argvars[1], &fy) == OK)
12221 rettv->vval.v_float = fmod(fx, fy);
12222 else
12223 rettv->vval.v_float = 0.0;
12224}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012225#endif
12226
Bram Moolenaar0d660222005-01-07 21:51:51 +000012227/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012228 * "fnameescape({string})" function
12229 */
12230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012231f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012232{
12233 rettv->vval.v_string = vim_strsave_fnameescape(
12234 get_tv_string(&argvars[0]), FALSE);
12235 rettv->v_type = VAR_STRING;
12236}
12237
12238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 * "fnamemodify({fname}, {mods})" function
12240 */
12241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012242f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243{
12244 char_u *fname;
12245 char_u *mods;
12246 int usedlen = 0;
12247 int len;
12248 char_u *fbuf = NULL;
12249 char_u buf[NUMBUFLEN];
12250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012251 fname = get_tv_string_chk(&argvars[0]);
12252 mods = get_tv_string_buf_chk(&argvars[1], buf);
12253 if (fname == NULL || mods == NULL)
12254 fname = NULL;
12255 else
12256 {
12257 len = (int)STRLEN(fname);
12258 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 vim_free(fbuf);
12267}
12268
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012269static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270
12271/*
12272 * "foldclosed()" function
12273 */
12274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012275foldclosed_both(
12276 typval_T *argvars UNUSED,
12277 typval_T *rettv,
12278 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279{
12280#ifdef FEAT_FOLDING
12281 linenr_T lnum;
12282 linenr_T first, last;
12283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12286 {
12287 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12288 {
12289 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012292 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 return;
12294 }
12295 }
12296#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298}
12299
12300/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012301 * "foldclosed()" function
12302 */
12303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012304f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012305{
12306 foldclosed_both(argvars, rettv, FALSE);
12307}
12308
12309/*
12310 * "foldclosedend()" function
12311 */
12312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012313f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012314{
12315 foldclosed_both(argvars, rettv, TRUE);
12316}
12317
12318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012319 * "foldlevel()" function
12320 */
12321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012322f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324#ifdef FEAT_FOLDING
12325 linenr_T lnum;
12326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331}
12332
12333/*
12334 * "foldtext()" function
12335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012337f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338{
12339#ifdef FEAT_FOLDING
12340 linenr_T lnum;
12341 char_u *s;
12342 char_u *r;
12343 int len;
12344 char *txt;
12345#endif
12346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012347 rettv->v_type = VAR_STRING;
12348 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012350 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12351 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12352 <= curbuf->b_ml.ml_line_count
12353 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 {
12355 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012356 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12357 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 {
12359 if (!linewhite(lnum))
12360 break;
12361 ++lnum;
12362 }
12363
12364 /* Find interesting text in this line. */
12365 s = skipwhite(ml_get(lnum));
12366 /* skip C comment-start */
12367 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012370 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012371 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012372 {
12373 s = skipwhite(ml_get(lnum + 1));
12374 if (*s == '*')
12375 s = skipwhite(s + 1);
12376 }
12377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 txt = _("+-%s%3ld lines: ");
12379 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012380 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 + 20 /* for %3ld */
12382 + STRLEN(s))); /* concatenated */
12383 if (r != NULL)
12384 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012385 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12386 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12387 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 len = (int)STRLEN(r);
12389 STRCAT(r, s);
12390 /* remove 'foldmarker' and 'commentstring' */
12391 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 }
12394 }
12395#endif
12396}
12397
12398/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012399 * "foldtextresult(lnum)" function
12400 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012402f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012403{
12404#ifdef FEAT_FOLDING
12405 linenr_T lnum;
12406 char_u *text;
12407 char_u buf[51];
12408 foldinfo_T foldinfo;
12409 int fold_count;
12410#endif
12411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012412 rettv->v_type = VAR_STRING;
12413 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012414#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012416 /* treat illegal types and illegal string values for {lnum} the same */
12417 if (lnum < 0)
12418 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012419 fold_count = foldedCount(curwin, lnum, &foldinfo);
12420 if (fold_count > 0)
12421 {
12422 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12423 &foldinfo, buf);
12424 if (text == buf)
12425 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012427 }
12428#endif
12429}
12430
12431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 * "foreground()" function
12433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012435f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#ifdef FEAT_GUI
12438 if (gui.in_use)
12439 gui_mch_set_foreground();
12440#else
12441# ifdef WIN32
12442 win32_set_foreground();
12443# endif
12444#endif
12445}
12446
12447/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448 * "function()" function
12449 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012451f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012452{
12453 char_u *s;
12454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012456 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012457 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012458 /* Don't check an autoload name for existence here. */
12459 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012460 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012461 else
12462 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012463 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012464 {
12465 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012466 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012467
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012468 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12469 * also be called from another script. Using trans_function_name()
12470 * would also work, but some plugins depend on the name being
12471 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012472 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012473 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012474 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012475 if (rettv->vval.v_string != NULL)
12476 {
12477 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012478 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012479 }
12480 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012481 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012482 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012484 }
12485}
12486
12487/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012488 * "garbagecollect()" function
12489 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012491f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012492{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012493 /* This is postponed until we are back at the toplevel, because we may be
12494 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12495 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012496
12497 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12498 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012499}
12500
12501/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502 * "get()" function
12503 */
12504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012505f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506{
Bram Moolenaar33570922005-01-25 22:26:29 +000012507 listitem_T *li;
12508 list_T *l;
12509 dictitem_T *di;
12510 dict_T *d;
12511 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512
Bram Moolenaare9a41262005-01-15 22:18:47 +000012513 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012514 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012515 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517 int error = FALSE;
12518
12519 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12520 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012521 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012524 else if (argvars[0].v_type == VAR_DICT)
12525 {
12526 if ((d = argvars[0].vval.v_dict) != NULL)
12527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012528 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012529 if (di != NULL)
12530 tv = &di->di_tv;
12531 }
12532 }
12533 else
12534 EMSG2(_(e_listdictarg), "get()");
12535
12536 if (tv == NULL)
12537 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012539 copy_tv(&argvars[2], rettv);
12540 }
12541 else
12542 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012543}
12544
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012545static 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 +000012546
12547/*
12548 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012549 * Return a range (from start to end) of lines in rettv from the specified
12550 * buffer.
12551 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012552 */
12553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012554get_buffer_lines(
12555 buf_T *buf,
12556 linenr_T start,
12557 linenr_T end,
12558 int retlist,
12559 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012560{
12561 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012562
Bram Moolenaar959a1432013-12-14 12:17:38 +010012563 rettv->v_type = VAR_STRING;
12564 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012565 if (retlist && rettv_list_alloc(rettv) == FAIL)
12566 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012567
12568 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12569 return;
12570
12571 if (!retlist)
12572 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012573 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12574 p = ml_get_buf(buf, start, FALSE);
12575 else
12576 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012577 rettv->vval.v_string = vim_strsave(p);
12578 }
12579 else
12580 {
12581 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012582 return;
12583
12584 if (start < 1)
12585 start = 1;
12586 if (end > buf->b_ml.ml_line_count)
12587 end = buf->b_ml.ml_line_count;
12588 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012589 if (list_append_string(rettv->vval.v_list,
12590 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012591 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012592 }
12593}
12594
12595/*
12596 * "getbufline()" function
12597 */
12598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012599f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012600{
12601 linenr_T lnum;
12602 linenr_T end;
12603 buf_T *buf;
12604
12605 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12606 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012607 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012608 --emsg_off;
12609
Bram Moolenaar661b1822005-07-28 22:36:45 +000012610 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012611 if (argvars[2].v_type == VAR_UNKNOWN)
12612 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012613 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012614 end = get_tv_lnum_buf(&argvars[2], buf);
12615
Bram Moolenaar342337a2005-07-21 21:11:17 +000012616 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012617}
12618
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619/*
12620 * "getbufvar()" function
12621 */
12622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012623f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012624{
12625 buf_T *buf;
12626 buf_T *save_curbuf;
12627 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012628 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012629 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12632 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012633 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012634 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012636 rettv->v_type = VAR_STRING;
12637 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012638
12639 if (buf != NULL && varname != NULL)
12640 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012641 /* set curbuf to be our buf, temporarily */
12642 save_curbuf = curbuf;
12643 curbuf = buf;
12644
Bram Moolenaar0d660222005-01-07 21:51:51 +000012645 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012646 {
12647 if (get_option_tv(&varname, rettv, TRUE) == OK)
12648 done = TRUE;
12649 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012650 else if (STRCMP(varname, "changedtick") == 0)
12651 {
12652 rettv->v_type = VAR_NUMBER;
12653 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012654 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012655 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656 else
12657 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012658 /* Look up the variable. */
12659 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12660 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12661 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012662 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012663 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012664 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012665 done = TRUE;
12666 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012668
12669 /* restore previous notion of curbuf */
12670 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012671 }
12672
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012673 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12674 /* use the default value */
12675 copy_tv(&argvars[2], rettv);
12676
Bram Moolenaar0d660222005-01-07 21:51:51 +000012677 --emsg_off;
12678}
12679
12680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 * "getchar()" function
12682 */
12683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012684f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685{
12686 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012689 /* Position the cursor. Needed after a message that ends in a space. */
12690 windgoto(msg_row, msg_col);
12691
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 ++no_mapping;
12693 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012694 for (;;)
12695 {
12696 if (argvars[0].v_type == VAR_UNKNOWN)
12697 /* getchar(): blocking wait. */
12698 n = safe_vgetc();
12699 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12700 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012701 n = vpeekc_any();
12702 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012703 /* illegal argument or getchar(0) and no char avail: return zero */
12704 n = 0;
12705 else
12706 /* getchar(0) and char avail: return char */
12707 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012708
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012709 if (n == K_IGNORE)
12710 continue;
12711 break;
12712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 --no_mapping;
12714 --allow_keys;
12715
Bram Moolenaar219b8702006-11-01 14:32:36 +000012716 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12717 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12718 vimvars[VV_MOUSE_COL].vv_nr = 0;
12719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721 if (IS_SPECIAL(n) || mod_mask != 0)
12722 {
12723 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12724 int i = 0;
12725
12726 /* Turn a special key into three bytes, plus modifier. */
12727 if (mod_mask != 0)
12728 {
12729 temp[i++] = K_SPECIAL;
12730 temp[i++] = KS_MODIFIER;
12731 temp[i++] = mod_mask;
12732 }
12733 if (IS_SPECIAL(n))
12734 {
12735 temp[i++] = K_SPECIAL;
12736 temp[i++] = K_SECOND(n);
12737 temp[i++] = K_THIRD(n);
12738 }
12739#ifdef FEAT_MBYTE
12740 else if (has_mbyte)
12741 i += (*mb_char2bytes)(n, temp + i);
12742#endif
12743 else
12744 temp[i++] = n;
12745 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->v_type = VAR_STRING;
12747 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012748
12749#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012750 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012751 {
12752 int row = mouse_row;
12753 int col = mouse_col;
12754 win_T *win;
12755 linenr_T lnum;
12756# ifdef FEAT_WINDOWS
12757 win_T *wp;
12758# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012759 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012760
12761 if (row >= 0 && col >= 0)
12762 {
12763 /* Find the window at the mouse coordinates and compute the
12764 * text position. */
12765 win = mouse_find_win(&row, &col);
12766 (void)mouse_comp_pos(win, &row, &col, &lnum);
12767# ifdef FEAT_WINDOWS
12768 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012769 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012770# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012771 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012772 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12773 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12774 }
12775 }
12776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 }
12778}
12779
12780/*
12781 * "getcharmod()" function
12782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012784f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787}
12788
12789/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012790 * "getcharsearch()" function
12791 */
12792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012793f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012794{
12795 if (rettv_dict_alloc(rettv) != FAIL)
12796 {
12797 dict_T *dict = rettv->vval.v_dict;
12798
12799 dict_add_nr_str(dict, "char", 0L, last_csearch());
12800 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12801 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12802 }
12803}
12804
12805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 * "getcmdline()" function
12807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->v_type = VAR_STRING;
12812 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813}
12814
12815/*
12816 * "getcmdpos()" function
12817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012819f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822}
12823
12824/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012825 * "getcmdtype()" function
12826 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012828f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012829{
12830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = alloc(2);
12832 if (rettv->vval.v_string != NULL)
12833 {
12834 rettv->vval.v_string[0] = get_cmdline_type();
12835 rettv->vval.v_string[1] = NUL;
12836 }
12837}
12838
12839/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012840 * "getcmdwintype()" function
12841 */
12842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012843f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012844{
12845 rettv->v_type = VAR_STRING;
12846 rettv->vval.v_string = NULL;
12847#ifdef FEAT_CMDWIN
12848 rettv->vval.v_string = alloc(2);
12849 if (rettv->vval.v_string != NULL)
12850 {
12851 rettv->vval.v_string[0] = cmdwin_type;
12852 rettv->vval.v_string[1] = NUL;
12853 }
12854#endif
12855}
12856
12857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 * "getcwd()" function
12859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012861f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012863 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012864 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012867 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012868
12869 wp = find_tabwin(&argvars[0], &argvars[1]);
12870 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012872 if (wp->w_localdir != NULL)
12873 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12874 else if(globaldir != NULL)
12875 rettv->vval.v_string = vim_strsave(globaldir);
12876 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012877 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012878 cwd = alloc(MAXPATHL);
12879 if (cwd != NULL)
12880 {
12881 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12882 rettv->vval.v_string = vim_strsave(cwd);
12883 vim_free(cwd);
12884 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012885 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012886#ifdef BACKSLASH_IN_FILENAME
12887 if (rettv->vval.v_string != NULL)
12888 slash_adjust(rettv->vval.v_string);
12889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890 }
12891}
12892
12893/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012894 * "getfontname()" function
12895 */
12896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012897f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->v_type = VAR_STRING;
12900 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012901#ifdef FEAT_GUI
12902 if (gui.in_use)
12903 {
12904 GuiFont font;
12905 char_u *name = NULL;
12906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012907 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012908 {
12909 /* Get the "Normal" font. Either the name saved by
12910 * hl_set_font_name() or from the font ID. */
12911 font = gui.norm_font;
12912 name = hl_get_font_name();
12913 }
12914 else
12915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012917 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12918 return;
12919 font = gui_mch_get_font(name, FALSE);
12920 if (font == NOFONT)
12921 return; /* Invalid font name, return empty string. */
12922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012924 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012925 gui_mch_free_font(font);
12926 }
12927#endif
12928}
12929
12930/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012931 * "getfperm({fname})" function
12932 */
12933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012934f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012935{
12936 char_u *fname;
12937 struct stat st;
12938 char_u *perm = NULL;
12939 char_u flags[] = "rwx";
12940 int i;
12941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012945 if (mch_stat((char *)fname, &st) >= 0)
12946 {
12947 perm = vim_strsave((char_u *)"---------");
12948 if (perm != NULL)
12949 {
12950 for (i = 0; i < 9; i++)
12951 {
12952 if (st.st_mode & (1 << (8 - i)))
12953 perm[i] = flags[i % 3];
12954 }
12955 }
12956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012958}
12959
12960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 * "getfsize({fname})" function
12962 */
12963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012964f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965{
12966 char_u *fname;
12967 struct stat st;
12968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012971 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972
12973 if (mch_stat((char *)fname, &st) >= 0)
12974 {
12975 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012980
12981 /* non-perfect check for overflow */
12982 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12983 rettv->vval.v_number = -2;
12984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985 }
12986 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988}
12989
12990/*
12991 * "getftime({fname})" function
12992 */
12993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012994f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996 char_u *fname;
12997 struct stat st;
12998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000
13001 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013004 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005}
13006
13007/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013008 * "getftype({fname})" function
13009 */
13010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013011f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013012{
13013 char_u *fname;
13014 struct stat st;
13015 char_u *type = NULL;
13016 char *t;
13017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013021 if (mch_lstat((char *)fname, &st) >= 0)
13022 {
13023#ifdef S_ISREG
13024 if (S_ISREG(st.st_mode))
13025 t = "file";
13026 else if (S_ISDIR(st.st_mode))
13027 t = "dir";
13028# ifdef S_ISLNK
13029 else if (S_ISLNK(st.st_mode))
13030 t = "link";
13031# endif
13032# ifdef S_ISBLK
13033 else if (S_ISBLK(st.st_mode))
13034 t = "bdev";
13035# endif
13036# ifdef S_ISCHR
13037 else if (S_ISCHR(st.st_mode))
13038 t = "cdev";
13039# endif
13040# ifdef S_ISFIFO
13041 else if (S_ISFIFO(st.st_mode))
13042 t = "fifo";
13043# endif
13044# ifdef S_ISSOCK
13045 else if (S_ISSOCK(st.st_mode))
13046 t = "fifo";
13047# endif
13048 else
13049 t = "other";
13050#else
13051# ifdef S_IFMT
13052 switch (st.st_mode & S_IFMT)
13053 {
13054 case S_IFREG: t = "file"; break;
13055 case S_IFDIR: t = "dir"; break;
13056# ifdef S_IFLNK
13057 case S_IFLNK: t = "link"; break;
13058# endif
13059# ifdef S_IFBLK
13060 case S_IFBLK: t = "bdev"; break;
13061# endif
13062# ifdef S_IFCHR
13063 case S_IFCHR: t = "cdev"; break;
13064# endif
13065# ifdef S_IFIFO
13066 case S_IFIFO: t = "fifo"; break;
13067# endif
13068# ifdef S_IFSOCK
13069 case S_IFSOCK: t = "socket"; break;
13070# endif
13071 default: t = "other";
13072 }
13073# else
13074 if (mch_isdir(fname))
13075 t = "dir";
13076 else
13077 t = "file";
13078# endif
13079#endif
13080 type = vim_strsave((char_u *)t);
13081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013083}
13084
13085/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013086 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013087 */
13088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013089f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013090{
13091 linenr_T lnum;
13092 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013093 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013094
13095 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013096 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013097 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013098 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013099 retlist = FALSE;
13100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013101 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013102 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013103 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013104 retlist = TRUE;
13105 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013106
Bram Moolenaar342337a2005-07-21 21:11:17 +000013107 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013108}
13109
13110/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013111 * "getmatches()" function
13112 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013114f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013115{
13116#ifdef FEAT_SEARCH_EXTRA
13117 dict_T *dict;
13118 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013119 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013120
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013121 if (rettv_list_alloc(rettv) == OK)
13122 {
13123 while (cur != NULL)
13124 {
13125 dict = dict_alloc();
13126 if (dict == NULL)
13127 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013128 if (cur->match.regprog == NULL)
13129 {
13130 /* match added with matchaddpos() */
13131 for (i = 0; i < MAXPOSMATCH; ++i)
13132 {
13133 llpos_T *llpos;
13134 char buf[6];
13135 list_T *l;
13136
13137 llpos = &cur->pos.pos[i];
13138 if (llpos->lnum == 0)
13139 break;
13140 l = list_alloc();
13141 if (l == NULL)
13142 break;
13143 list_append_number(l, (varnumber_T)llpos->lnum);
13144 if (llpos->col > 0)
13145 {
13146 list_append_number(l, (varnumber_T)llpos->col);
13147 list_append_number(l, (varnumber_T)llpos->len);
13148 }
13149 sprintf(buf, "pos%d", i + 1);
13150 dict_add_list(dict, buf, l);
13151 }
13152 }
13153 else
13154 {
13155 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13156 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013157 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013158 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13159 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013160# ifdef FEAT_CONCEAL
13161 if (cur->conceal_char)
13162 {
13163 char_u buf[MB_MAXBYTES + 1];
13164
13165 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13166 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13167 }
13168# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013169 list_append_dict(rettv->vval.v_list, dict);
13170 cur = cur->next;
13171 }
13172 }
13173#endif
13174}
13175
13176/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013177 * "getpid()" function
13178 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013180f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013181{
13182 rettv->vval.v_number = mch_get_pid();
13183}
13184
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013185static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013186
13187/*
13188 * "getcurpos()" function
13189 */
13190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013191f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013192{
13193 getpos_both(argvars, rettv, TRUE);
13194}
13195
Bram Moolenaar18081e32008-02-20 19:11:07 +000013196/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013197 * "getpos(string)" function
13198 */
13199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013200f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013201{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013202 getpos_both(argvars, rettv, FALSE);
13203}
13204
13205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013206getpos_both(
13207 typval_T *argvars,
13208 typval_T *rettv,
13209 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013210{
Bram Moolenaara5525202006-03-02 22:52:09 +000013211 pos_T *fp;
13212 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013213 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013214
13215 if (rettv_list_alloc(rettv) == OK)
13216 {
13217 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013218 if (getcurpos)
13219 fp = &curwin->w_cursor;
13220 else
13221 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013222 if (fnum != -1)
13223 list_append_number(l, (varnumber_T)fnum);
13224 else
13225 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013226 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13227 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013228 list_append_number(l, (fp != NULL)
13229 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013230 : (varnumber_T)0);
13231 list_append_number(l,
13232#ifdef FEAT_VIRTUALEDIT
13233 (fp != NULL) ? (varnumber_T)fp->coladd :
13234#endif
13235 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013236 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013237 {
13238 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013239 list_append_number(l, curwin->w_curswant == MAXCOL ?
13240 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013241 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013242 }
13243 else
13244 rettv->vval.v_number = FALSE;
13245}
13246
13247/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013248 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013249 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013251f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013252{
13253#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013254 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013255#endif
13256
Bram Moolenaar2641f772005-03-25 21:58:17 +000013257#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013258 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013259 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013260 wp = NULL;
13261 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13262 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013263 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013264 if (wp == NULL)
13265 return;
13266 }
13267
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013268 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013269 }
13270#endif
13271}
13272
13273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 * "getreg()" function
13275 */
13276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013277f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278{
13279 char_u *strregname;
13280 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013281 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013282 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013285 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 strregname = get_tv_string_chk(&argvars[0]);
13288 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013289 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013291 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013292 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13293 return_list = get_tv_number_chk(&argvars[2], &error);
13294 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013297 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013298
13299 if (error)
13300 return;
13301
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 regname = (strregname == NULL ? '"' : *strregname);
13303 if (regname == 0)
13304 regname = '"';
13305
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013306 if (return_list)
13307 {
13308 rettv->v_type = VAR_LIST;
13309 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13310 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013311 if (rettv->vval.v_list != NULL)
13312 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013313 }
13314 else
13315 {
13316 rettv->v_type = VAR_STRING;
13317 rettv->vval.v_string = get_reg_contents(regname,
13318 arg2 ? GREG_EXPR_SRC : 0);
13319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320}
13321
13322/*
13323 * "getregtype()" function
13324 */
13325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013326f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327{
13328 char_u *strregname;
13329 int regname;
13330 char_u buf[NUMBUFLEN + 2];
13331 long reglen = 0;
13332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013333 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 {
13335 strregname = get_tv_string_chk(&argvars[0]);
13336 if (strregname == NULL) /* type error; errmsg already given */
13337 {
13338 rettv->v_type = VAR_STRING;
13339 rettv->vval.v_string = NULL;
13340 return;
13341 }
13342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 else
13344 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013345 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346
13347 regname = (strregname == NULL ? '"' : *strregname);
13348 if (regname == 0)
13349 regname = '"';
13350
13351 buf[0] = NUL;
13352 buf[1] = NUL;
13353 switch (get_reg_type(regname, &reglen))
13354 {
13355 case MLINE: buf[0] = 'V'; break;
13356 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 case MBLOCK:
13358 buf[0] = Ctrl_V;
13359 sprintf((char *)buf + 1, "%ld", reglen + 1);
13360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013362 rettv->v_type = VAR_STRING;
13363 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364}
13365
13366/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013367 * "gettabvar()" function
13368 */
13369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013370f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013371{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013372 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013373 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013374 dictitem_T *v;
13375 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013376 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013377
13378 rettv->v_type = VAR_STRING;
13379 rettv->vval.v_string = NULL;
13380
13381 varname = get_tv_string_chk(&argvars[1]);
13382 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13383 if (tp != NULL && varname != NULL)
13384 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013385 /* Set tp to be our tabpage, temporarily. Also set the window to the
13386 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013387 if (switch_win(&oldcurwin, &oldtabpage,
13388 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013389 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013390 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013391 /* look up the variable */
13392 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13393 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13394 if (v != NULL)
13395 {
13396 copy_tv(&v->di_tv, rettv);
13397 done = TRUE;
13398 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013399 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013400
13401 /* restore previous notion of curwin */
13402 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013403 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013404
13405 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013406 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013407}
13408
13409/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013410 * "gettabwinvar()" function
13411 */
13412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013413f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013414{
13415 getwinvar(argvars, rettv, 1);
13416}
13417
13418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 * "getwinposx()" function
13420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013422f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425#ifdef FEAT_GUI
13426 if (gui.in_use)
13427 {
13428 int x, y;
13429
13430 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013431 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 }
13433#endif
13434}
13435
13436/*
13437 * "getwinposy()" function
13438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013440f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443#ifdef FEAT_GUI
13444 if (gui.in_use)
13445 {
13446 int x, y;
13447
13448 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 }
13451#endif
13452}
13453
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013454/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013455 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013456 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013457 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458find_win_by_nr(
13459 typval_T *vp,
13460 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013461{
13462#ifdef FEAT_WINDOWS
13463 win_T *wp;
13464#endif
13465 int nr;
13466
13467 nr = get_tv_number_chk(vp, NULL);
13468
13469#ifdef FEAT_WINDOWS
13470 if (nr < 0)
13471 return NULL;
13472 if (nr == 0)
13473 return curwin;
13474
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013475 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13476 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013477 if (--nr <= 0)
13478 break;
13479 return wp;
13480#else
13481 if (nr == 0 || nr == 1)
13482 return curwin;
13483 return NULL;
13484#endif
13485}
13486
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013488 * Find window specified by "wvp" in tabpage "tvp".
13489 */
13490 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013491find_tabwin(
13492 typval_T *wvp, /* VAR_UNKNOWN for current window */
13493 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013494{
13495 win_T *wp = NULL;
13496 tabpage_T *tp = NULL;
13497 long n;
13498
13499 if (wvp->v_type != VAR_UNKNOWN)
13500 {
13501 if (tvp->v_type != VAR_UNKNOWN)
13502 {
13503 n = get_tv_number(tvp);
13504 if (n >= 0)
13505 tp = find_tabpage(n);
13506 }
13507 else
13508 tp = curtab;
13509
13510 if (tp != NULL)
13511 wp = find_win_by_nr(wvp, tp);
13512 }
13513 else
13514 wp = curwin;
13515
13516 return wp;
13517}
13518
13519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 * "getwinvar()" function
13521 */
13522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013523f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013525 getwinvar(argvars, rettv, 0);
13526}
13527
13528/*
13529 * getwinvar() and gettabwinvar()
13530 */
13531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013532getwinvar(
13533 typval_T *argvars,
13534 typval_T *rettv,
13535 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013536{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013537 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013540 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013541 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013542#ifdef FEAT_WINDOWS
13543 win_T *oldcurwin;
13544 tabpage_T *oldtabpage;
13545 int need_switch_win;
13546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548#ifdef FEAT_WINDOWS
13549 if (off == 1)
13550 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13551 else
13552 tp = curtab;
13553#endif
13554 win = find_win_by_nr(&argvars[off], tp);
13555 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013558 rettv->v_type = VAR_STRING;
13559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
13561 if (win != NULL && varname != NULL)
13562 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013563#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013564 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013565 * otherwise the window is not valid. Only do this when needed,
13566 * autocommands get blocked. */
13567 need_switch_win = !(tp == curtab && win == curwin);
13568 if (!need_switch_win
13569 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13570#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013571 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013572 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013573 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013574 if (get_option_tv(&varname, rettv, 1) == OK)
13575 done = TRUE;
13576 }
13577 else
13578 {
13579 /* Look up the variable. */
13580 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13581 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13582 varname, FALSE);
13583 if (v != NULL)
13584 {
13585 copy_tv(&v->di_tv, rettv);
13586 done = TRUE;
13587 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013590
Bram Moolenaarba117c22015-09-29 16:53:22 +020013591#ifdef FEAT_WINDOWS
13592 if (need_switch_win)
13593 /* restore previous notion of curwin */
13594 restore_win(oldcurwin, oldtabpage, TRUE);
13595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 }
13597
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013598 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13599 /* use the default return value */
13600 copy_tv(&argvars[off + 2], rettv);
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 --emsg_off;
13603}
13604
13605/*
13606 * "glob()" function
13607 */
13608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013609f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013611 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013613 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013615 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013616 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013618 if (argvars[1].v_type != VAR_UNKNOWN)
13619 {
13620 if (get_tv_number_chk(&argvars[1], &error))
13621 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013623 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013624 if (get_tv_number_chk(&argvars[2], &error))
13625 {
13626 rettv->v_type = VAR_LIST;
13627 rettv->vval.v_list = NULL;
13628 }
13629 if (argvars[3].v_type != VAR_UNKNOWN
13630 && get_tv_number_chk(&argvars[3], &error))
13631 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013632 }
13633 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 if (!error)
13635 {
13636 ExpandInit(&xpc);
13637 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013638 if (p_wic)
13639 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013640 if (rettv->v_type == VAR_STRING)
13641 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013642 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013643 else if (rettv_list_alloc(rettv) != FAIL)
13644 {
13645 int i;
13646
13647 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13648 NULL, options, WILD_ALL_KEEP);
13649 for (i = 0; i < xpc.xp_numfiles; i++)
13650 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13651
13652 ExpandCleanup(&xpc);
13653 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013654 }
13655 else
13656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657}
13658
13659/*
13660 * "globpath()" function
13661 */
13662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013663f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013665 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013668 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013669 garray_T ga;
13670 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013672 /* When the optional second argument is non-zero, don't remove matches
13673 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013675 if (argvars[2].v_type != VAR_UNKNOWN)
13676 {
13677 if (get_tv_number_chk(&argvars[2], &error))
13678 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013679 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013680 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013681 if (get_tv_number_chk(&argvars[3], &error))
13682 {
13683 rettv->v_type = VAR_LIST;
13684 rettv->vval.v_list = NULL;
13685 }
13686 if (argvars[4].v_type != VAR_UNKNOWN
13687 && get_tv_number_chk(&argvars[4], &error))
13688 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013689 }
13690 }
13691 if (file != NULL && !error)
13692 {
13693 ga_init2(&ga, (int)sizeof(char_u *), 10);
13694 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13695 if (rettv->v_type == VAR_STRING)
13696 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13697 else if (rettv_list_alloc(rettv) != FAIL)
13698 for (i = 0; i < ga.ga_len; ++i)
13699 list_append_string(rettv->vval.v_list,
13700 ((char_u **)(ga.ga_data))[i], -1);
13701 ga_clear_strings(&ga);
13702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
13707/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013708 * "glob2regpat()" function
13709 */
13710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013711f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013712{
13713 char_u *pat = get_tv_string_chk(&argvars[0]);
13714
13715 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013716 rettv->vval.v_string = (pat == NULL)
13717 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013718}
13719
13720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 * "has()" function
13722 */
13723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013724f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725{
13726 int i;
13727 char_u *name;
13728 int n = FALSE;
13729 static char *(has_list[]) =
13730 {
13731#ifdef AMIGA
13732 "amiga",
13733# ifdef FEAT_ARP
13734 "arp",
13735# endif
13736#endif
13737#ifdef __BEOS__
13738 "beos",
13739#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013740#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 "mac",
13742#endif
13743#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013744 "macunix", /* built with 'darwin' enabled */
13745#endif
13746#if defined(__APPLE__) && __APPLE__ == 1
13747 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef __QNX__
13750 "qnx",
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef UNIX
13753 "unix",
13754#endif
13755#ifdef VMS
13756 "vms",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef WIN32
13759 "win32",
13760#endif
13761#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13762 "win32unix",
13763#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013764#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 "win64",
13766#endif
13767#ifdef EBCDIC
13768 "ebcdic",
13769#endif
13770#ifndef CASE_INSENSITIVE_FILENAME
13771 "fname_case",
13772#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013773#ifdef HAVE_ACL
13774 "acl",
13775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_ARABIC
13777 "arabic",
13778#endif
13779#ifdef FEAT_AUTOCMD
13780 "autocmd",
13781#endif
13782#ifdef FEAT_BEVAL
13783 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013784# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13785 "balloon_multiline",
13786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#endif
13788#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13789 "builtin_terms",
13790# ifdef ALL_BUILTIN_TCAPS
13791 "all_builtin_terms",
13792# endif
13793#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013794#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13795 || defined(FEAT_GUI_W32) \
13796 || defined(FEAT_GUI_MOTIF))
13797 "browsefilter",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_BYTEOFF
13800 "byte_offset",
13801#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013803 "channel",
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#ifdef FEAT_CINDENT
13806 "cindent",
13807#endif
13808#ifdef FEAT_CLIENTSERVER
13809 "clientserver",
13810#endif
13811#ifdef FEAT_CLIPBOARD
13812 "clipboard",
13813#endif
13814#ifdef FEAT_CMDL_COMPL
13815 "cmdline_compl",
13816#endif
13817#ifdef FEAT_CMDHIST
13818 "cmdline_hist",
13819#endif
13820#ifdef FEAT_COMMENTS
13821 "comments",
13822#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013823#ifdef FEAT_CONCEAL
13824 "conceal",
13825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#ifdef FEAT_CRYPT
13827 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013828 "crypt-blowfish",
13829 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#endif
13831#ifdef FEAT_CSCOPE
13832 "cscope",
13833#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013834#ifdef FEAT_CURSORBIND
13835 "cursorbind",
13836#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013837#ifdef CURSOR_SHAPE
13838 "cursorshape",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef DEBUG
13841 "debug",
13842#endif
13843#ifdef FEAT_CON_DIALOG
13844 "dialog_con",
13845#endif
13846#ifdef FEAT_GUI_DIALOG
13847 "dialog_gui",
13848#endif
13849#ifdef FEAT_DIFF
13850 "diff",
13851#endif
13852#ifdef FEAT_DIGRAPHS
13853 "digraphs",
13854#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013855#ifdef FEAT_DIRECTX
13856 "directx",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_DND
13859 "dnd",
13860#endif
13861#ifdef FEAT_EMACS_TAGS
13862 "emacs_tags",
13863#endif
13864 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013865 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#ifdef FEAT_SEARCH_EXTRA
13867 "extra_search",
13868#endif
13869#ifdef FEAT_FKMAP
13870 "farsi",
13871#endif
13872#ifdef FEAT_SEARCHPATH
13873 "file_in_path",
13874#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013875#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013876 "filterpipe",
13877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878#ifdef FEAT_FIND_ID
13879 "find_in_path",
13880#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881#ifdef FEAT_FLOAT
13882 "float",
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_FOLDING
13885 "folding",
13886#endif
13887#ifdef FEAT_FOOTER
13888 "footer",
13889#endif
13890#if !defined(USE_SYSTEM) && defined(UNIX)
13891 "fork",
13892#endif
13893#ifdef FEAT_GETTEXT
13894 "gettext",
13895#endif
13896#ifdef FEAT_GUI
13897 "gui",
13898#endif
13899#ifdef FEAT_GUI_ATHENA
13900# ifdef FEAT_GUI_NEXTAW
13901 "gui_neXtaw",
13902# else
13903 "gui_athena",
13904# endif
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906#ifdef FEAT_GUI_GTK
13907 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013908# ifdef USE_GTK3
13909 "gui_gtk3",
13910# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013914#ifdef FEAT_GUI_GNOME
13915 "gui_gnome",
13916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917#ifdef FEAT_GUI_MAC
13918 "gui_mac",
13919#endif
13920#ifdef FEAT_GUI_MOTIF
13921 "gui_motif",
13922#endif
13923#ifdef FEAT_GUI_PHOTON
13924 "gui_photon",
13925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926#ifdef FEAT_GUI_W32
13927 "gui_win32",
13928#endif
13929#ifdef FEAT_HANGULIN
13930 "hangul_input",
13931#endif
13932#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13933 "iconv",
13934#endif
13935#ifdef FEAT_INS_EXPAND
13936 "insert_expand",
13937#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013939 "job",
13940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#ifdef FEAT_JUMPLIST
13942 "jumplist",
13943#endif
13944#ifdef FEAT_KEYMAP
13945 "keymap",
13946#endif
13947#ifdef FEAT_LANGMAP
13948 "langmap",
13949#endif
13950#ifdef FEAT_LIBCALL
13951 "libcall",
13952#endif
13953#ifdef FEAT_LINEBREAK
13954 "linebreak",
13955#endif
13956#ifdef FEAT_LISP
13957 "lispindent",
13958#endif
13959#ifdef FEAT_LISTCMDS
13960 "listcmds",
13961#endif
13962#ifdef FEAT_LOCALMAP
13963 "localmap",
13964#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013965#ifdef FEAT_LUA
13966# ifndef DYNAMIC_LUA
13967 "lua",
13968# endif
13969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970#ifdef FEAT_MENU
13971 "menu",
13972#endif
13973#ifdef FEAT_SESSION
13974 "mksession",
13975#endif
13976#ifdef FEAT_MODIFY_FNAME
13977 "modify_fname",
13978#endif
13979#ifdef FEAT_MOUSE
13980 "mouse",
13981#endif
13982#ifdef FEAT_MOUSESHAPE
13983 "mouseshape",
13984#endif
13985#if defined(UNIX) || defined(VMS)
13986# ifdef FEAT_MOUSE_DEC
13987 "mouse_dec",
13988# endif
13989# ifdef FEAT_MOUSE_GPM
13990 "mouse_gpm",
13991# endif
13992# ifdef FEAT_MOUSE_JSB
13993 "mouse_jsbterm",
13994# endif
13995# ifdef FEAT_MOUSE_NET
13996 "mouse_netterm",
13997# endif
13998# ifdef FEAT_MOUSE_PTERM
13999 "mouse_pterm",
14000# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014001# ifdef FEAT_MOUSE_SGR
14002 "mouse_sgr",
14003# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014004# ifdef FEAT_SYSMOUSE
14005 "mouse_sysmouse",
14006# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014007# ifdef FEAT_MOUSE_URXVT
14008 "mouse_urxvt",
14009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010# ifdef FEAT_MOUSE_XTERM
14011 "mouse_xterm",
14012# endif
14013#endif
14014#ifdef FEAT_MBYTE
14015 "multi_byte",
14016#endif
14017#ifdef FEAT_MBYTE_IME
14018 "multi_byte_ime",
14019#endif
14020#ifdef FEAT_MULTI_LANG
14021 "multi_lang",
14022#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014023#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014024#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014025 "mzscheme",
14026#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028#ifdef FEAT_OLE
14029 "ole",
14030#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014031 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef FEAT_PATH_EXTRA
14033 "path_extra",
14034#endif
14035#ifdef FEAT_PERL
14036#ifndef DYNAMIC_PERL
14037 "perl",
14038#endif
14039#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014040#ifdef FEAT_PERSISTENT_UNDO
14041 "persistent_undo",
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef FEAT_PYTHON
14044#ifndef DYNAMIC_PYTHON
14045 "python",
14046#endif
14047#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014048#ifdef FEAT_PYTHON3
14049#ifndef DYNAMIC_PYTHON3
14050 "python3",
14051#endif
14052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053#ifdef FEAT_POSTSCRIPT
14054 "postscript",
14055#endif
14056#ifdef FEAT_PRINTER
14057 "printer",
14058#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014059#ifdef FEAT_PROFILE
14060 "profile",
14061#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014062#ifdef FEAT_RELTIME
14063 "reltime",
14064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065#ifdef FEAT_QUICKFIX
14066 "quickfix",
14067#endif
14068#ifdef FEAT_RIGHTLEFT
14069 "rightleft",
14070#endif
14071#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14072 "ruby",
14073#endif
14074#ifdef FEAT_SCROLLBIND
14075 "scrollbind",
14076#endif
14077#ifdef FEAT_CMDL_INFO
14078 "showcmd",
14079 "cmdline_info",
14080#endif
14081#ifdef FEAT_SIGNS
14082 "signs",
14083#endif
14084#ifdef FEAT_SMARTINDENT
14085 "smartindent",
14086#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014087#ifdef STARTUPTIME
14088 "startuptime",
14089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef FEAT_STL_OPT
14091 "statusline",
14092#endif
14093#ifdef FEAT_SUN_WORKSHOP
14094 "sun_workshop",
14095#endif
14096#ifdef FEAT_NETBEANS_INTG
14097 "netbeans_intg",
14098#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014099#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014100 "spell",
14101#endif
14102#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 "syntax",
14104#endif
14105#if defined(USE_SYSTEM) || !defined(UNIX)
14106 "system",
14107#endif
14108#ifdef FEAT_TAG_BINS
14109 "tag_binary",
14110#endif
14111#ifdef FEAT_TAG_OLDSTATIC
14112 "tag_old_static",
14113#endif
14114#ifdef FEAT_TAG_ANYWHITE
14115 "tag_any_white",
14116#endif
14117#ifdef FEAT_TCL
14118# ifndef DYNAMIC_TCL
14119 "tcl",
14120# endif
14121#endif
14122#ifdef TERMINFO
14123 "terminfo",
14124#endif
14125#ifdef FEAT_TERMRESPONSE
14126 "termresponse",
14127#endif
14128#ifdef FEAT_TEXTOBJ
14129 "textobjects",
14130#endif
14131#ifdef HAVE_TGETENT
14132 "tgetent",
14133#endif
14134#ifdef FEAT_TITLE
14135 "title",
14136#endif
14137#ifdef FEAT_TOOLBAR
14138 "toolbar",
14139#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014140#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14141 "unnamedplus",
14142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#ifdef FEAT_USR_CMDS
14144 "user-commands", /* was accidentally included in 5.4 */
14145 "user_commands",
14146#endif
14147#ifdef FEAT_VIMINFO
14148 "viminfo",
14149#endif
14150#ifdef FEAT_VERTSPLIT
14151 "vertsplit",
14152#endif
14153#ifdef FEAT_VIRTUALEDIT
14154 "virtualedit",
14155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157#ifdef FEAT_VISUALEXTRA
14158 "visualextra",
14159#endif
14160#ifdef FEAT_VREPLACE
14161 "vreplace",
14162#endif
14163#ifdef FEAT_WILDIGN
14164 "wildignore",
14165#endif
14166#ifdef FEAT_WILDMENU
14167 "wildmenu",
14168#endif
14169#ifdef FEAT_WINDOWS
14170 "windows",
14171#endif
14172#ifdef FEAT_WAK
14173 "winaltkeys",
14174#endif
14175#ifdef FEAT_WRITEBACKUP
14176 "writebackup",
14177#endif
14178#ifdef FEAT_XIM
14179 "xim",
14180#endif
14181#ifdef FEAT_XFONTSET
14182 "xfontset",
14183#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014184#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014185 "xpm",
14186 "xpm_w32", /* for backward compatibility */
14187#else
14188# if defined(HAVE_XPM)
14189 "xpm",
14190# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192#ifdef USE_XSMP
14193 "xsmp",
14194#endif
14195#ifdef USE_XSMP_INTERACT
14196 "xsmp_interact",
14197#endif
14198#ifdef FEAT_XCLIPBOARD
14199 "xterm_clipboard",
14200#endif
14201#ifdef FEAT_XTERM_SAVE
14202 "xterm_save",
14203#endif
14204#if defined(UNIX) && defined(FEAT_X11)
14205 "X11",
14206#endif
14207 NULL
14208 };
14209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 for (i = 0; has_list[i] != NULL; ++i)
14212 if (STRICMP(name, has_list[i]) == 0)
14213 {
14214 n = TRUE;
14215 break;
14216 }
14217
14218 if (n == FALSE)
14219 {
14220 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014221 {
14222 if (name[5] == '-'
14223 && STRLEN(name) > 11
14224 && vim_isdigit(name[6])
14225 && vim_isdigit(name[8])
14226 && vim_isdigit(name[10]))
14227 {
14228 int major = atoi((char *)name + 6);
14229 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014230
14231 /* Expect "patch-9.9.01234". */
14232 n = (major < VIM_VERSION_MAJOR
14233 || (major == VIM_VERSION_MAJOR
14234 && (minor < VIM_VERSION_MINOR
14235 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014236 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014237 }
14238 else
14239 n = has_patch(atoi((char *)name + 5));
14240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241 else if (STRICMP(name, "vim_starting") == 0)
14242 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014243#ifdef FEAT_MBYTE
14244 else if (STRICMP(name, "multi_byte_encoding") == 0)
14245 n = has_mbyte;
14246#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014247#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14248 else if (STRICMP(name, "balloon_multiline") == 0)
14249 n = multiline_balloon_available();
14250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251#ifdef DYNAMIC_TCL
14252 else if (STRICMP(name, "tcl") == 0)
14253 n = tcl_enabled(FALSE);
14254#endif
14255#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14256 else if (STRICMP(name, "iconv") == 0)
14257 n = iconv_enabled(FALSE);
14258#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014259#ifdef DYNAMIC_LUA
14260 else if (STRICMP(name, "lua") == 0)
14261 n = lua_enabled(FALSE);
14262#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014263#ifdef DYNAMIC_MZSCHEME
14264 else if (STRICMP(name, "mzscheme") == 0)
14265 n = mzscheme_enabled(FALSE);
14266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267#ifdef DYNAMIC_RUBY
14268 else if (STRICMP(name, "ruby") == 0)
14269 n = ruby_enabled(FALSE);
14270#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014271#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272#ifdef DYNAMIC_PYTHON
14273 else if (STRICMP(name, "python") == 0)
14274 n = python_enabled(FALSE);
14275#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014276#endif
14277#ifdef FEAT_PYTHON3
14278#ifdef DYNAMIC_PYTHON3
14279 else if (STRICMP(name, "python3") == 0)
14280 n = python3_enabled(FALSE);
14281#endif
14282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283#ifdef DYNAMIC_PERL
14284 else if (STRICMP(name, "perl") == 0)
14285 n = perl_enabled(FALSE);
14286#endif
14287#ifdef FEAT_GUI
14288 else if (STRICMP(name, "gui_running") == 0)
14289 n = (gui.in_use || gui.starting);
14290# ifdef FEAT_GUI_W32
14291 else if (STRICMP(name, "gui_win32s") == 0)
14292 n = gui_is_win32s();
14293# endif
14294# ifdef FEAT_BROWSE
14295 else if (STRICMP(name, "browse") == 0)
14296 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14297# endif
14298#endif
14299#ifdef FEAT_SYN_HL
14300 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014301 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302#endif
14303#if defined(WIN3264)
14304 else if (STRICMP(name, "win95") == 0)
14305 n = mch_windows95();
14306#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014307#ifdef FEAT_NETBEANS_INTG
14308 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014309 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 }
14312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314}
14315
14316/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014317 * "has_key()" function
14318 */
14319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014320f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014321{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014322 if (argvars[0].v_type != VAR_DICT)
14323 {
14324 EMSG(_(e_dictreq));
14325 return;
14326 }
14327 if (argvars[0].vval.v_dict == NULL)
14328 return;
14329
14330 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014331 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014332}
14333
14334/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014335 * "haslocaldir()" function
14336 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014338f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014339{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014340 win_T *wp = NULL;
14341
14342 wp = find_tabwin(&argvars[0], &argvars[1]);
14343 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014344}
14345
14346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 * "hasmapto()" function
14348 */
14349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014350f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351{
14352 char_u *name;
14353 char_u *mode;
14354 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014355 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014358 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 mode = (char_u *)"nvo";
14360 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014363 if (argvars[2].v_type != VAR_UNKNOWN)
14364 abbr = get_tv_number(&argvars[2]);
14365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
Bram Moolenaar2c932302006-03-18 21:42:09 +000014367 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371}
14372
14373/*
14374 * "histadd()" function
14375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014377f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379#ifdef FEAT_CMDHIST
14380 int histype;
14381 char_u *str;
14382 char_u buf[NUMBUFLEN];
14383#endif
14384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014385 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 if (check_restricted() || check_secure())
14387 return;
14388#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14390 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 if (histype >= 0)
14392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 if (*str != NUL)
14395 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014396 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014398 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 return;
14400 }
14401 }
14402#endif
14403}
14404
14405/*
14406 * "histdel()" function
14407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014409f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411#ifdef FEAT_CMDHIST
14412 int n;
14413 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14417 if (str == NULL)
14418 n = 0;
14419 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 else
14427 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014428 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 get_tv_string_buf(&argvars[1], buf));
14430 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431#endif
14432}
14433
14434/*
14435 * "histget()" function
14436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014438f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439{
14440#ifdef FEAT_CMDHIST
14441 int type;
14442 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014443 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14446 if (str == NULL)
14447 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 {
14450 type = get_histtype(str);
14451 if (argvars[1].v_type == VAR_UNKNOWN)
14452 idx = get_history_idx(type);
14453 else
14454 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14455 /* -1 on type error */
14456 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462}
14463
14464/*
14465 * "histnr()" function
14466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014468f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469{
14470 int i;
14471
14472#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 char_u *history = get_tv_string_chk(&argvars[0]);
14474
14475 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 if (i >= HIST_CMD && i < HIST_COUNT)
14477 i = get_history_idx(i);
14478 else
14479#endif
14480 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482}
14483
14484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 * "highlightID(name)" function
14486 */
14487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014488f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491}
14492
14493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494 * "highlight_exists()" function
14495 */
14496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014497f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014498{
14499 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14500}
14501
14502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 * "hostname()" function
14504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014506f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507{
14508 char_u hostname[256];
14509
14510 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014511 rettv->v_type = VAR_STRING;
14512 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513}
14514
14515/*
14516 * iconv() function
14517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014519f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
14521#ifdef FEAT_MBYTE
14522 char_u buf1[NUMBUFLEN];
14523 char_u buf2[NUMBUFLEN];
14524 char_u *from, *to, *str;
14525 vimconv_T vimconv;
14526#endif
14527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->v_type = VAR_STRING;
14529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530
14531#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014532 str = get_tv_string(&argvars[0]);
14533 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14534 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 vimconv.vc_type = CONV_NONE;
14536 convert_setup(&vimconv, from, to);
14537
14538 /* If the encodings are equal, no conversion needed. */
14539 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014540 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014542 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543
14544 convert_setup(&vimconv, NULL, NULL);
14545 vim_free(from);
14546 vim_free(to);
14547#endif
14548}
14549
14550/*
14551 * "indent()" function
14552 */
14553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014554f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555{
14556 linenr_T lnum;
14557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563}
14564
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014565/*
14566 * "index()" function
14567 */
14568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014569f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014570{
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 list_T *l;
14572 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014573 long idx = 0;
14574 int ic = FALSE;
14575
14576 rettv->vval.v_number = -1;
14577 if (argvars[0].v_type != VAR_LIST)
14578 {
14579 EMSG(_(e_listreq));
14580 return;
14581 }
14582 l = argvars[0].vval.v_list;
14583 if (l != NULL)
14584 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014585 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014586 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 int error = FALSE;
14589
Bram Moolenaar758711c2005-02-02 23:11:38 +000014590 /* Start at specified item. Use the cached index that list_find()
14591 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014593 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014594 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 ic = get_tv_number_chk(&argvars[3], &error);
14596 if (error)
14597 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014598 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014599
Bram Moolenaar758711c2005-02-02 23:11:38 +000014600 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014601 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014602 {
14603 rettv->vval.v_number = idx;
14604 break;
14605 }
14606 }
14607}
14608
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609static int inputsecret_flag = 0;
14610
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014611static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014612
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014614 * This function is used by f_input() and f_inputdialog() functions. The third
14615 * argument to f_input() specifies the type of completion to use at the
14616 * prompt. The third argument to f_inputdialog() specifies the value to return
14617 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 */
14619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014620get_user_input(
14621 typval_T *argvars,
14622 typval_T *rettv,
14623 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014625 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 char_u *p = NULL;
14627 int c;
14628 char_u buf[NUMBUFLEN];
14629 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014631 int xp_type = EXPAND_NOTHING;
14632 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014634 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014635 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636
14637#ifdef NO_CONSOLE_INPUT
14638 /* While starting up, there is no place to enter text. */
14639 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641#endif
14642
14643 cmd_silent = FALSE; /* Want to see the prompt. */
14644 if (prompt != NULL)
14645 {
14646 /* Only the part of the message after the last NL is considered as
14647 * prompt for the command line */
14648 p = vim_strrchr(prompt, '\n');
14649 if (p == NULL)
14650 p = prompt;
14651 else
14652 {
14653 ++p;
14654 c = *p;
14655 *p = NUL;
14656 msg_start();
14657 msg_clr_eos();
14658 msg_puts_attr(prompt, echo_attr);
14659 msg_didout = FALSE;
14660 msg_starthere();
14661 *p = c;
14662 }
14663 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 if (argvars[1].v_type != VAR_UNKNOWN)
14666 {
14667 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14668 if (defstr != NULL)
14669 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014671 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014672 {
14673 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014674 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014675 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014676
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014677 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014678 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014679
Bram Moolenaar4463f292005-09-25 22:20:24 +000014680 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14681 if (xp_name == NULL)
14682 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014684 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014685
Bram Moolenaar4463f292005-09-25 22:20:24 +000014686 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14687 &xp_arg) == FAIL)
14688 return;
14689 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014690 }
14691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014693 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014694 int save_ex_normal_busy = ex_normal_busy;
14695 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014696 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014697 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14698 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014699 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014700 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014701 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014702 && argvars[1].v_type != VAR_UNKNOWN
14703 && argvars[2].v_type != VAR_UNKNOWN)
14704 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14705 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014706
14707 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014709 /* since the user typed this, no need to wait for return */
14710 need_wait_return = FALSE;
14711 msg_didout = FALSE;
14712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 cmd_silent = cmd_silent_save;
14714}
14715
14716/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014717 * "input()" function
14718 * Also handles inputsecret() when inputsecret is set.
14719 */
14720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014721f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014722{
14723 get_user_input(argvars, rettv, FALSE);
14724}
14725
14726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 * "inputdialog()" function
14728 */
14729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014730f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731{
14732#if defined(FEAT_GUI_TEXTDIALOG)
14733 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14734 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14735 {
14736 char_u *message;
14737 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 message = get_tv_string_chk(&argvars[0]);
14741 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014742 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014743 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 else
14745 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 if (message != NULL && defstr != NULL
14747 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014748 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014749 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
14751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 if (message != NULL && defstr != NULL
14753 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014754 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_string = vim_strsave(
14756 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 }
14762 else
14763#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014764 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765}
14766
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014767/*
14768 * "inputlist()" function
14769 */
14770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014771f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014772{
14773 listitem_T *li;
14774 int selected;
14775 int mouse_used;
14776
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014777#ifdef NO_CONSOLE_INPUT
14778 /* While starting up, there is no place to enter text. */
14779 if (no_console_input())
14780 return;
14781#endif
14782 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14783 {
14784 EMSG2(_(e_listarg), "inputlist()");
14785 return;
14786 }
14787
14788 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014789 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014790 lines_left = Rows; /* avoid more prompt */
14791 msg_scroll = TRUE;
14792 msg_clr_eos();
14793
14794 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14795 {
14796 msg_puts(get_tv_string(&li->li_tv));
14797 msg_putchar('\n');
14798 }
14799
14800 /* Ask for choice. */
14801 selected = prompt_for_number(&mouse_used);
14802 if (mouse_used)
14803 selected -= lines_left;
14804
14805 rettv->vval.v_number = selected;
14806}
14807
14808
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14810
14811/*
14812 * "inputrestore()" function
14813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014815f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816{
14817 if (ga_userinput.ga_len > 0)
14818 {
14819 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14821 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014822 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014823 }
14824 else if (p_verbose > 1)
14825 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014826 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828 }
14829}
14830
14831/*
14832 * "inputsave()" function
14833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014837 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 if (ga_grow(&ga_userinput, 1) == OK)
14839 {
14840 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14841 + ga_userinput.ga_len);
14842 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014843 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 }
14845 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847}
14848
14849/*
14850 * "inputsecret()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854{
14855 ++cmdline_star;
14856 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014857 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 --cmdline_star;
14859 --inputsecret_flag;
14860}
14861
14862/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863 * "insert()" function
14864 */
14865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014866f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014867{
14868 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014869 listitem_T *item;
14870 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014871 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014872
14873 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014874 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014875 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014876 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014877 {
14878 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014879 before = get_tv_number_chk(&argvars[2], &error);
14880 if (error)
14881 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014882
Bram Moolenaar758711c2005-02-02 23:11:38 +000014883 if (before == l->lv_len)
14884 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014885 else
14886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014887 item = list_find(l, before);
14888 if (item == NULL)
14889 {
14890 EMSGN(_(e_listidx), before);
14891 l = NULL;
14892 }
14893 }
14894 if (l != NULL)
14895 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014896 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014897 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014898 }
14899 }
14900}
14901
14902/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014903 * "invert(expr)" function
14904 */
14905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014906f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014907{
14908 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14909}
14910
14911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 * "isdirectory()" function
14913 */
14914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014915f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014917 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918}
14919
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014920/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014921 * "islocked()" function
14922 */
14923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014924f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014925{
14926 lval_T lv;
14927 char_u *end;
14928 dictitem_T *di;
14929
14930 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014931 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14932 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014933 if (end != NULL && lv.ll_name != NULL)
14934 {
14935 if (*end != NUL)
14936 EMSG(_(e_trailing));
14937 else
14938 {
14939 if (lv.ll_tv == NULL)
14940 {
14941 if (check_changedtick(lv.ll_name))
14942 rettv->vval.v_number = 1; /* always locked */
14943 else
14944 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014945 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014946 if (di != NULL)
14947 {
14948 /* Consider a variable locked when:
14949 * 1. the variable itself is locked
14950 * 2. the value of the variable is locked.
14951 * 3. the List or Dict value is locked.
14952 */
14953 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14954 || tv_islocked(&di->di_tv));
14955 }
14956 }
14957 }
14958 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014959 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014960 else if (lv.ll_newkey != NULL)
14961 EMSG2(_(e_dictkey), lv.ll_newkey);
14962 else if (lv.ll_list != NULL)
14963 /* List item. */
14964 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14965 else
14966 /* Dictionary item. */
14967 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14968 }
14969 }
14970
14971 clear_lval(&lv);
14972}
14973
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014974#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14975/*
14976 * "isnan()" function
14977 */
14978 static void
14979f_isnan(typval_T *argvars, typval_T *rettv)
14980{
14981 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14982 && isnan(argvars[0].vval.v_float);
14983}
14984#endif
14985
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014986static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014987
14988/*
14989 * Turn a dict into a list:
14990 * "what" == 0: list of keys
14991 * "what" == 1: list of values
14992 * "what" == 2: list of items
14993 */
14994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014995dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014996{
Bram Moolenaar33570922005-01-25 22:26:29 +000014997 list_T *l2;
14998 dictitem_T *di;
14999 hashitem_T *hi;
15000 listitem_T *li;
15001 listitem_T *li2;
15002 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015003 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004
Bram Moolenaar8c711452005-01-14 21:53:12 +000015005 if (argvars[0].v_type != VAR_DICT)
15006 {
15007 EMSG(_(e_dictreq));
15008 return;
15009 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015010 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011 return;
15012
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015014 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015015
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015016 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015017 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015018 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015019 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015021 --todo;
15022 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015024 li = listitem_alloc();
15025 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015026 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015027 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015028
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015029 if (what == 0)
15030 {
15031 /* keys() */
15032 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015033 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015034 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15035 }
15036 else if (what == 1)
15037 {
15038 /* values() */
15039 copy_tv(&di->di_tv, &li->li_tv);
15040 }
15041 else
15042 {
15043 /* items() */
15044 l2 = list_alloc();
15045 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015046 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015047 li->li_tv.vval.v_list = l2;
15048 if (l2 == NULL)
15049 break;
15050 ++l2->lv_refcount;
15051
15052 li2 = listitem_alloc();
15053 if (li2 == NULL)
15054 break;
15055 list_append(l2, li2);
15056 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015057 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015058 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15059
15060 li2 = listitem_alloc();
15061 if (li2 == NULL)
15062 break;
15063 list_append(l2, li2);
15064 copy_tv(&di->di_tv, &li2->li_tv);
15065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015066 }
15067 }
15068}
15069
15070/*
15071 * "items(dict)" function
15072 */
15073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015074f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015075{
15076 dict_list(argvars, rettv, 2);
15077}
15078
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015079#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015080/*
15081 * Get the job from the argument.
15082 * Returns NULL if the job is invalid.
15083 */
15084 static job_T *
15085get_job_arg(typval_T *tv)
15086{
15087 job_T *job;
15088
15089 if (tv->v_type != VAR_JOB)
15090 {
15091 EMSG2(_(e_invarg2), get_tv_string(tv));
15092 return NULL;
15093 }
15094 job = tv->vval.v_job;
15095
15096 if (job == NULL)
15097 EMSG(_("E916: not a valid job"));
15098 return job;
15099}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015100
Bram Moolenaar835dc632016-02-07 14:27:38 +010015101/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015102 * "job_getchannel()" function
15103 */
15104 static void
15105f_job_getchannel(typval_T *argvars, typval_T *rettv)
15106{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015107 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015108
Bram Moolenaar65edff82016-02-21 16:40:11 +010015109 if (job != NULL)
15110 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015111 rettv->v_type = VAR_CHANNEL;
15112 rettv->vval.v_channel = job->jv_channel;
15113 if (job->jv_channel != NULL)
15114 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015115 }
15116}
15117
15118/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015119 * "job_setoptions()" function
15120 */
15121 static void
15122f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15123{
15124 job_T *job = get_job_arg(&argvars[0]);
15125 jobopt_T opt;
15126
15127 if (job == NULL)
15128 return;
15129 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015130 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015131 return;
15132 job_set_options(job, &opt);
15133}
15134
15135/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015136 * "job_start()" function
15137 */
15138 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015139f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015140{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015141 job_T *job;
15142 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015143#if defined(UNIX)
15144# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015145 char **argv = NULL;
15146 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015148 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015149#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015150 jobopt_T opt;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015151 int part;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015152
15153 rettv->v_type = VAR_JOB;
15154 job = job_alloc();
15155 rettv->vval.v_job = job;
15156 if (job == NULL)
15157 return;
15158
15159 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015160
15161 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015162 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015163 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015164 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015165 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015166 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015167 return;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015168
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015169 /* Check that when io is "file" that there is a file name. */
15170 for (part = PART_OUT; part <= PART_IN; ++part)
15171 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
15172 && opt.jo_io[part] == JIO_FILE
15173 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
15174 || *opt.jo_io_name[part] == NUL))
15175 {
15176 EMSG(_("E920: -io file requires -name to be set"));
15177 return;
15178 }
15179
Bram Moolenaar014069a2016-03-03 22:51:40 +010015180 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15181 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015182 buf_T *buf = NULL;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015183
15184 /* check that we can find the buffer before starting the job */
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015185 if (opt.jo_set & JO_IN_BUF)
Bram Moolenaar014069a2016-03-03 22:51:40 +010015186 {
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015187 buf = buflist_findnr(opt.jo_io_buf[PART_IN]);
15188 if (buf == NULL)
15189 EMSGN(_(e_nobufnr), (long)opt.jo_io_buf[PART_IN]);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015190 }
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015191 else if (!(opt.jo_set & JO_IN_NAME))
15192 {
15193 EMSG(_("E915: in-io buffer requires in-buf or in-name to be set"));
15194 }
15195 else
15196 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015197 if (buf == NULL)
15198 return;
15199 if (buf->b_ml.ml_mfp == NULL)
15200 {
Bram Moolenaar846cdb22016-03-11 18:52:22 +010015201 char_u numbuf[NUMBUFLEN];
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015202 char_u *s;
15203
15204 if (opt.jo_set & JO_IN_BUF)
15205 {
Bram Moolenaar846cdb22016-03-11 18:52:22 +010015206 sprintf((char *)numbuf, "%d", opt.jo_io_buf[PART_IN]);
15207 s = numbuf;
Bram Moolenaar29fd0382016-03-09 23:14:07 +010015208 }
15209 else
15210 s = opt.jo_io_name[PART_IN];
15211 EMSG2(_("E918: buffer must be loaded: %s"), s);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015212 return;
15213 }
15214 job->jv_in_buf = buf;
15215 }
15216
Bram Moolenaar65edff82016-02-21 16:40:11 +010015217 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015218
Bram Moolenaar835dc632016-02-07 14:27:38 +010015219#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015220 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015221#endif
15222
15223 if (argvars[0].v_type == VAR_STRING)
15224 {
15225 /* Command is a string. */
15226 cmd = argvars[0].vval.v_string;
15227#ifdef USE_ARGV
15228 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15229 return;
15230 argv[argc] = NULL;
15231#endif
15232 }
15233 else if (argvars[0].v_type != VAR_LIST
15234 || argvars[0].vval.v_list == NULL
15235 || argvars[0].vval.v_list->lv_len < 1)
15236 {
15237 EMSG(_(e_invarg));
15238 return;
15239 }
15240 else
15241 {
15242 list_T *l = argvars[0].vval.v_list;
15243 listitem_T *li;
15244 char_u *s;
15245
15246#ifdef USE_ARGV
15247 /* Pass argv[] to mch_call_shell(). */
15248 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15249 if (argv == NULL)
15250 return;
15251#endif
15252 for (li = l->lv_first; li != NULL; li = li->li_next)
15253 {
15254 s = get_tv_string_chk(&li->li_tv);
15255 if (s == NULL)
15256 goto theend;
15257#ifdef USE_ARGV
15258 argv[argc++] = (char *)s;
15259#else
Bram Moolenaara86f14a2016-02-29 21:05:48 +010015260 /* Only escape when needed, double quotes are not always allowed. */
15261 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015262 {
15263 s = vim_strsave_shellescape(s, FALSE, TRUE);
15264 if (s == NULL)
15265 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015266 ga_concat(&ga, s);
15267 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015268 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015269 else
15270 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015271 if (li->li_next != NULL)
15272 ga_append(&ga, ' ');
15273#endif
15274 }
15275#ifdef USE_ARGV
15276 argv[argc] = NULL;
15277#else
15278 cmd = ga.ga_data;
15279#endif
15280 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015281
Bram Moolenaar835dc632016-02-07 14:27:38 +010015282#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015283 if (ch_log_active())
15284 {
15285 garray_T ga;
15286 int i;
15287
15288 ga_init2(&ga, (int)sizeof(char), 200);
15289 for (i = 0; i < argc; ++i)
15290 {
15291 if (i > 0)
15292 ga_concat(&ga, (char_u *)" ");
15293 ga_concat(&ga, (char_u *)argv[i]);
15294 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015295 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015296 ga_clear(&ga);
15297 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015298 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015299#else
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015300 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015301 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015302#endif
15303
Bram Moolenaar839fd112016-03-06 21:34:03 +010015304 /* If the channel is reading from a buffer, write lines now. */
Bram Moolenaar4e329fc2016-03-07 15:24:03 +010015305 if (job->jv_channel != NULL)
15306 channel_write_in(job->jv_channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015307
Bram Moolenaar835dc632016-02-07 14:27:38 +010015308theend:
15309#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015310 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015311#else
15312 vim_free(ga.ga_data);
15313#endif
15314}
15315
15316/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015317 * Get the status of "job" and invoke the exit callback when needed.
15318 * The returned string is not allocated.
15319 */
15320 static char *
15321job_status(job_T *job)
15322{
15323 char *result;
15324
15325 if (job->jv_status == JOB_ENDED)
15326 /* No need to check, dead is dead. */
15327 result = "dead";
15328 else if (job->jv_status == JOB_FAILED)
15329 result = "fail";
15330 else
15331 {
15332 result = mch_job_status(job);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015333 if (job->jv_status == JOB_ENDED)
15334 ch_log(job->jv_channel, "Job ended");
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015335 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15336 {
15337 typval_T argv[3];
15338 typval_T rettv;
15339 int dummy;
15340
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015341 /* invoke the exit callback; make sure the refcount is > 0 */
15342 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015343 argv[0].v_type = VAR_JOB;
15344 argv[0].vval.v_job = job;
15345 argv[1].v_type = VAR_NUMBER;
15346 argv[1].vval.v_number = job->jv_exitval;
15347 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15348 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15349 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015350 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015351 }
15352 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15353 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015354 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015355 * freed. Careful: caller must not use "job" after this! */
15356 job_free(job);
15357 }
15358 }
15359 return result;
15360}
15361
15362/*
15363 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15364 */
15365 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015366job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015367{
15368 static time_t last_check = 0;
15369 time_t now;
15370 job_T *job;
15371 job_T *next;
15372
15373 /* Only do this once in 10 seconds. */
15374 now = time(NULL);
15375 if (last_check + 10 < now)
15376 {
15377 last_check = now;
15378 for (job = first_job; job != NULL; job = next)
15379 {
15380 next = job->jv_next;
15381 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15382 job_status(job); /* may free "job" */
15383 }
15384 }
15385}
15386
15387/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015388 * "job_status()" function
15389 */
15390 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015391f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015392{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015393 job_T *job = get_job_arg(&argvars[0]);
15394 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015395
Bram Moolenaar65edff82016-02-21 16:40:11 +010015396 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015397 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015398 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015399 rettv->v_type = VAR_STRING;
15400 rettv->vval.v_string = vim_strsave((char_u *)result);
15401 }
15402}
15403
15404/*
15405 * "job_stop()" function
15406 */
15407 static void
15408f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15409{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015410 job_T *job = get_job_arg(&argvars[0]);
15411
15412 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015413 {
15414 char_u *arg;
15415
15416 if (argvars[1].v_type == VAR_UNKNOWN)
15417 arg = (char_u *)"";
15418 else
15419 {
15420 arg = get_tv_string_chk(&argvars[1]);
15421 if (arg == NULL)
15422 {
15423 EMSG(_(e_invarg));
15424 return;
15425 }
15426 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015427 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015428 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015429 rettv->vval.v_number = 0;
15430 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015431 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015432 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015433 /* Assume that "hup" does not kill the job. */
15434 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15435 job->jv_channel->ch_job_killed = TRUE;
15436 }
15437 /* We don't try freeing the job, obviously the caller still has a
15438 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015439 }
15440}
15441#endif
15442
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015444 * "join()" function
15445 */
15446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015447f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015448{
15449 garray_T ga;
15450 char_u *sep;
15451
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015452 if (argvars[0].v_type != VAR_LIST)
15453 {
15454 EMSG(_(e_listreq));
15455 return;
15456 }
15457 if (argvars[0].vval.v_list == NULL)
15458 return;
15459 if (argvars[1].v_type == VAR_UNKNOWN)
15460 sep = (char_u *)" ";
15461 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015462 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015463
15464 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015465
15466 if (sep != NULL)
15467 {
15468 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015469 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 ga_append(&ga, NUL);
15471 rettv->vval.v_string = (char_u *)ga.ga_data;
15472 }
15473 else
15474 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015475}
15476
15477/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015478 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015479 */
15480 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015481f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015482{
15483 js_read_T reader;
15484
15485 reader.js_buf = get_tv_string(&argvars[0]);
15486 reader.js_fill = NULL;
15487 reader.js_used = 0;
15488 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15489 EMSG(_(e_invarg));
15490}
15491
15492/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015493 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015494 */
15495 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015496f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015497{
15498 rettv->v_type = VAR_STRING;
15499 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15500}
15501
15502/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015503 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015504 */
15505 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015506f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015507{
15508 js_read_T reader;
15509
15510 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015511 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015512 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015513 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015514 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015515}
15516
15517/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015518 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015519 */
15520 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015521f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015522{
15523 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015524 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015525}
15526
15527/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015528 * "keys()" function
15529 */
15530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015531f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015532{
15533 dict_list(argvars, rettv, 0);
15534}
15535
15536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 * "last_buffer_nr()" function.
15538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015540f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541{
15542 int n = 0;
15543 buf_T *buf;
15544
15545 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15546 if (n < buf->b_fnum)
15547 n = buf->b_fnum;
15548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015550}
15551
15552/*
15553 * "len()" function
15554 */
15555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015556f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015557{
15558 switch (argvars[0].v_type)
15559 {
15560 case VAR_STRING:
15561 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->vval.v_number = (varnumber_T)STRLEN(
15563 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015564 break;
15565 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015566 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015567 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015568 case VAR_DICT:
15569 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15570 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015571 case VAR_UNKNOWN:
15572 case VAR_SPECIAL:
15573 case VAR_FLOAT:
15574 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015575 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015576 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015577 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 break;
15579 }
15580}
15581
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015582static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015583
15584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586{
15587#ifdef FEAT_LIBCALL
15588 char_u *string_in;
15589 char_u **string_result;
15590 int nr_result;
15591#endif
15592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015593 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015594 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015596
15597 if (check_restricted() || check_secure())
15598 return;
15599
15600#ifdef FEAT_LIBCALL
15601 /* The first two args must be strings, otherwise its meaningless */
15602 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15603 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015604 string_in = NULL;
15605 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015606 string_in = argvars[2].vval.v_string;
15607 if (type == VAR_NUMBER)
15608 string_result = NULL;
15609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015611 if (mch_libcall(argvars[0].vval.v_string,
15612 argvars[1].vval.v_string,
15613 string_in,
15614 argvars[2].vval.v_number,
15615 string_result,
15616 &nr_result) == OK
15617 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015618 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015619 }
15620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621}
15622
15623/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015624 * "libcall()" function
15625 */
15626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015627f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628{
15629 libcall_common(argvars, rettv, VAR_STRING);
15630}
15631
15632/*
15633 * "libcallnr()" function
15634 */
15635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015636f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637{
15638 libcall_common(argvars, rettv, VAR_NUMBER);
15639}
15640
15641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 * "line(string)" function
15643 */
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646{
15647 linenr_T lnum = 0;
15648 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015649 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015651 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 if (fp != NULL)
15653 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015654 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655}
15656
15657/*
15658 * "line2byte(lnum)" function
15659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015661f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662{
15663#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015664 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665#else
15666 linenr_T lnum;
15667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015668 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15673 if (rettv->vval.v_number >= 0)
15674 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675#endif
15676}
15677
15678/*
15679 * "lispindent(lnum)" function
15680 */
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683{
15684#ifdef FEAT_LISP
15685 pos_T pos;
15686 linenr_T lnum;
15687
15688 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015689 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15691 {
15692 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 curwin->w_cursor = pos;
15695 }
15696 else
15697#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699}
15700
15701/*
15702 * "localtime()" function
15703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015707 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708}
15709
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015710static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711
15712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714{
15715 char_u *keys;
15716 char_u *which;
15717 char_u buf[NUMBUFLEN];
15718 char_u *keys_buf = NULL;
15719 char_u *rhs;
15720 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015721 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015722 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015723 mapblock_T *mp;
15724 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725
15726 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->v_type = VAR_STRING;
15728 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 if (*keys == NUL)
15732 return;
15733
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015734 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015735 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015736 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015737 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015738 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015739 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015740 if (argvars[3].v_type != VAR_UNKNOWN)
15741 get_dict = get_tv_number(&argvars[3]);
15742 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 else
15745 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015746 if (which == NULL)
15747 return;
15748
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 mode = get_map_mode(&which, 0);
15750
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015751 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015752 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015754
15755 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015757 /* Return a string. */
15758 if (rhs != NULL)
15759 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760
Bram Moolenaarbd743252010-10-20 21:23:33 +020015761 }
15762 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15763 {
15764 /* Return a dictionary. */
15765 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15766 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15767 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768
Bram Moolenaarbd743252010-10-20 21:23:33 +020015769 dict_add_nr_str(dict, "lhs", 0L, lhs);
15770 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15771 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15772 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15773 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15774 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15775 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015776 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015777 dict_add_nr_str(dict, "mode", 0L, mapmode);
15778
15779 vim_free(lhs);
15780 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781 }
15782}
15783
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015784#ifdef FEAT_FLOAT
15785/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015786 * "log()" function
15787 */
15788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015789f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015790{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015791 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015792
15793 rettv->v_type = VAR_FLOAT;
15794 if (get_float_arg(argvars, &f) == OK)
15795 rettv->vval.v_float = log(f);
15796 else
15797 rettv->vval.v_float = 0.0;
15798}
15799
15800/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015801 * "log10()" function
15802 */
15803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015804f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015805{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015806 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015807
15808 rettv->v_type = VAR_FLOAT;
15809 if (get_float_arg(argvars, &f) == OK)
15810 rettv->vval.v_float = log10(f);
15811 else
15812 rettv->vval.v_float = 0.0;
15813}
15814#endif
15815
Bram Moolenaar1dced572012-04-05 16:54:08 +020015816#ifdef FEAT_LUA
15817/*
15818 * "luaeval()" function
15819 */
15820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015821f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015822{
15823 char_u *str;
15824 char_u buf[NUMBUFLEN];
15825
15826 str = get_tv_string_buf(&argvars[0], buf);
15827 do_luaeval(str, argvars + 1, rettv);
15828}
15829#endif
15830
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015832 * "map()" function
15833 */
15834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015835f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015836{
15837 filter_map(argvars, rettv, TRUE);
15838}
15839
15840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015841 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015846 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847}
15848
15849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015850 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856}
15857
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015858static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859
15860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015861find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015863 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015864 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015865 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866 char_u *pat;
15867 regmatch_T regmatch;
15868 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015869 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 char_u *save_cpo;
15871 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015872 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015873 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015874 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015875 list_T *l = NULL;
15876 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015877 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015878 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879
15880 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15881 save_cpo = p_cpo;
15882 p_cpo = (char_u *)"";
15883
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015884 rettv->vval.v_number = -1;
15885 if (type == 3)
15886 {
15887 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015888 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015889 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015890 }
15891 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015893 rettv->v_type = VAR_STRING;
15894 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015897 if (argvars[0].v_type == VAR_LIST)
15898 {
15899 if ((l = argvars[0].vval.v_list) == NULL)
15900 goto theend;
15901 li = l->lv_first;
15902 }
15903 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015904 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015905 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015906 len = (long)STRLEN(str);
15907 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015909 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15910 if (pat == NULL)
15911 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015912
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915 int error = FALSE;
15916
15917 start = get_tv_number_chk(&argvars[2], &error);
15918 if (error)
15919 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015920 if (l != NULL)
15921 {
15922 li = list_find(l, start);
15923 if (li == NULL)
15924 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015925 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015926 }
15927 else
15928 {
15929 if (start < 0)
15930 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015931 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015932 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015933 /* When "count" argument is there ignore matches before "start",
15934 * otherwise skip part of the string. Differs when pattern is "^"
15935 * or "\<". */
15936 if (argvars[3].v_type != VAR_UNKNOWN)
15937 startcol = start;
15938 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015939 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015940 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015941 len -= start;
15942 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015943 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015944
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015945 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015946 nth = get_tv_number_chk(&argvars[3], &error);
15947 if (error)
15948 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 }
15950
15951 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15952 if (regmatch.regprog != NULL)
15953 {
15954 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015955
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015956 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015957 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015958 if (l != NULL)
15959 {
15960 if (li == NULL)
15961 {
15962 match = FALSE;
15963 break;
15964 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015965 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015966 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015967 if (str == NULL)
15968 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015969 }
15970
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015971 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015972
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015973 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015974 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015975 if (l == NULL && !match)
15976 break;
15977
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015978 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015979 if (l != NULL)
15980 {
15981 li = li->li_next;
15982 ++idx;
15983 }
15984 else
15985 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015986#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015987 startcol = (colnr_T)(regmatch.startp[0]
15988 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015989#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015990 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015991#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015992 if (startcol > (colnr_T)len
15993 || str + startcol <= regmatch.startp[0])
15994 {
15995 match = FALSE;
15996 break;
15997 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015998 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015999 }
16000
16001 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016003 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016004 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016005 int i;
16006
16007 /* return list with matched string and submatches */
16008 for (i = 0; i < NSUBEXP; ++i)
16009 {
16010 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016011 {
16012 if (list_append_string(rettv->vval.v_list,
16013 (char_u *)"", 0) == FAIL)
16014 break;
16015 }
16016 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016017 regmatch.startp[i],
16018 (int)(regmatch.endp[i] - regmatch.startp[i]))
16019 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016020 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016021 }
16022 }
16023 else if (type == 2)
16024 {
16025 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016026 if (l != NULL)
16027 copy_tv(&li->li_tv, rettv);
16028 else
16029 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016031 }
16032 else if (l != NULL)
16033 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 else
16035 {
16036 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016037 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 (varnumber_T)(regmatch.startp[0] - str);
16039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016040 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016042 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043 }
16044 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016045 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016046 }
16047
16048theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016049 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 p_cpo = save_cpo;
16051}
16052
16053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016054 * "match()" function
16055 */
16056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016057f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058{
16059 find_some_match(argvars, rettv, 1);
16060}
16061
16062/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016063 * "matchadd()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016067{
16068#ifdef FEAT_SEARCH_EXTRA
16069 char_u buf[NUMBUFLEN];
16070 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16071 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16072 int prio = 10; /* default priority */
16073 int id = -1;
16074 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016075 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076
16077 rettv->vval.v_number = -1;
16078
16079 if (grp == NULL || pat == NULL)
16080 return;
16081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016082 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016083 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016084 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016085 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016086 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016087 if (argvars[4].v_type != VAR_UNKNOWN)
16088 {
16089 if (argvars[4].v_type != VAR_DICT)
16090 {
16091 EMSG(_(e_dictreq));
16092 return;
16093 }
16094 if (dict_find(argvars[4].vval.v_dict,
16095 (char_u *)"conceal", -1) != NULL)
16096 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16097 (char_u *)"conceal", FALSE);
16098 }
16099 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016100 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016101 if (error == TRUE)
16102 return;
16103 if (id >= 1 && id <= 3)
16104 {
16105 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16106 return;
16107 }
16108
Bram Moolenaar6561d522015-07-21 15:48:27 +020016109 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16110 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016111#endif
16112}
16113
16114/*
16115 * "matchaddpos()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016119{
16120#ifdef FEAT_SEARCH_EXTRA
16121 char_u buf[NUMBUFLEN];
16122 char_u *group;
16123 int prio = 10;
16124 int id = -1;
16125 int error = FALSE;
16126 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016127 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016128
16129 rettv->vval.v_number = -1;
16130
16131 group = get_tv_string_buf_chk(&argvars[0], buf);
16132 if (group == NULL)
16133 return;
16134
16135 if (argvars[1].v_type != VAR_LIST)
16136 {
16137 EMSG2(_(e_listarg), "matchaddpos()");
16138 return;
16139 }
16140 l = argvars[1].vval.v_list;
16141 if (l == NULL)
16142 return;
16143
16144 if (argvars[2].v_type != VAR_UNKNOWN)
16145 {
16146 prio = get_tv_number_chk(&argvars[2], &error);
16147 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016148 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016149 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016150 if (argvars[4].v_type != VAR_UNKNOWN)
16151 {
16152 if (argvars[4].v_type != VAR_DICT)
16153 {
16154 EMSG(_(e_dictreq));
16155 return;
16156 }
16157 if (dict_find(argvars[4].vval.v_dict,
16158 (char_u *)"conceal", -1) != NULL)
16159 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16160 (char_u *)"conceal", FALSE);
16161 }
16162 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016163 }
16164 if (error == TRUE)
16165 return;
16166
16167 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16168 if (id == 1 || id == 2)
16169 {
16170 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16171 return;
16172 }
16173
Bram Moolenaar6561d522015-07-21 15:48:27 +020016174 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16175 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016176#endif
16177}
16178
16179/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016180 * "matcharg()" function
16181 */
16182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016184{
16185 if (rettv_list_alloc(rettv) == OK)
16186 {
16187#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016188 int id = get_tv_number(&argvars[0]);
16189 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016190
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016191 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016192 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016193 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16194 {
16195 list_append_string(rettv->vval.v_list,
16196 syn_id2name(m->hlg_id), -1);
16197 list_append_string(rettv->vval.v_list, m->pattern, -1);
16198 }
16199 else
16200 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016201 list_append_string(rettv->vval.v_list, NULL, -1);
16202 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016203 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016204 }
16205#endif
16206 }
16207}
16208
16209/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016210 * "matchdelete()" function
16211 */
16212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016213f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016214{
16215#ifdef FEAT_SEARCH_EXTRA
16216 rettv->vval.v_number = match_delete(curwin,
16217 (int)get_tv_number(&argvars[0]), TRUE);
16218#endif
16219}
16220
16221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222 * "matchend()" function
16223 */
16224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226{
16227 find_some_match(argvars, rettv, 0);
16228}
16229
16230/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 * "matchlist()" function
16232 */
16233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016234f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235{
16236 find_some_match(argvars, rettv, 3);
16237}
16238
16239/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240 * "matchstr()" function
16241 */
16242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244{
16245 find_some_match(argvars, rettv, 2);
16246}
16247
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016248static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016249
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016252{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016253 long n = 0;
16254 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016255 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016256
16257 if (argvars[0].v_type == VAR_LIST)
16258 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016259 list_T *l;
16260 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016261
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016262 l = argvars[0].vval.v_list;
16263 if (l != NULL)
16264 {
16265 li = l->lv_first;
16266 if (li != NULL)
16267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016268 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016269 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016270 {
16271 li = li->li_next;
16272 if (li == NULL)
16273 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016274 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016275 if (domax ? i > n : i < n)
16276 n = i;
16277 }
16278 }
16279 }
16280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016281 else if (argvars[0].v_type == VAR_DICT)
16282 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016283 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016284 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016286 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016287
16288 d = argvars[0].vval.v_dict;
16289 if (d != NULL)
16290 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016291 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016293 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016294 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016295 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016296 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016297 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016298 if (first)
16299 {
16300 n = i;
16301 first = FALSE;
16302 }
16303 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016304 n = i;
16305 }
16306 }
16307 }
16308 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016309 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016310 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016311 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016312}
16313
16314/*
16315 * "max()" function
16316 */
16317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016318f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016319{
16320 max_min(argvars, rettv, TRUE);
16321}
16322
16323/*
16324 * "min()" function
16325 */
16326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016327f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016328{
16329 max_min(argvars, rettv, FALSE);
16330}
16331
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016332static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016333
16334/*
16335 * Create the directory in which "dir" is located, and higher levels when
16336 * needed.
16337 */
16338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016340{
16341 char_u *p;
16342 char_u *updir;
16343 int r = FAIL;
16344
16345 /* Get end of directory name in "dir".
16346 * We're done when it's "/" or "c:/". */
16347 p = gettail_sep(dir);
16348 if (p <= get_past_head(dir))
16349 return OK;
16350
16351 /* If the directory exists we're done. Otherwise: create it.*/
16352 updir = vim_strnsave(dir, (int)(p - dir));
16353 if (updir == NULL)
16354 return FAIL;
16355 if (mch_isdir(updir))
16356 r = OK;
16357 else if (mkdir_recurse(updir, prot) == OK)
16358 r = vim_mkdir_emsg(updir, prot);
16359 vim_free(updir);
16360 return r;
16361}
16362
16363#ifdef vim_mkdir
16364/*
16365 * "mkdir()" function
16366 */
16367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016369{
16370 char_u *dir;
16371 char_u buf[NUMBUFLEN];
16372 int prot = 0755;
16373
16374 rettv->vval.v_number = FAIL;
16375 if (check_restricted() || check_secure())
16376 return;
16377
16378 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016379 if (*dir == NUL)
16380 rettv->vval.v_number = FAIL;
16381 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016382 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016383 if (*gettail(dir) == NUL)
16384 /* remove trailing slashes */
16385 *gettail_sep(dir) = NUL;
16386
16387 if (argvars[1].v_type != VAR_UNKNOWN)
16388 {
16389 if (argvars[2].v_type != VAR_UNKNOWN)
16390 prot = get_tv_number_chk(&argvars[2], NULL);
16391 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16392 mkdir_recurse(dir, prot);
16393 }
16394 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016395 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016396}
16397#endif
16398
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 * "mode()" function
16401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016405 char_u buf[3];
16406
16407 buf[1] = NUL;
16408 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 if (VIsual_active)
16411 {
16412 if (VIsual_select)
16413 buf[0] = VIsual_mode + 's' - 'v';
16414 else
16415 buf[0] = VIsual_mode;
16416 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016417 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016418 || State == CONFIRM)
16419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016421 if (State == ASKMORE)
16422 buf[1] = 'm';
16423 else if (State == CONFIRM)
16424 buf[1] = '?';
16425 }
16426 else if (State == EXTERNCMD)
16427 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 else if (State & INSERT)
16429 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016430#ifdef FEAT_VREPLACE
16431 if (State & VREPLACE_FLAG)
16432 {
16433 buf[0] = 'R';
16434 buf[1] = 'v';
16435 }
16436 else
16437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 if (State & REPLACE_FLAG)
16439 buf[0] = 'R';
16440 else
16441 buf[0] = 'i';
16442 }
16443 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016446 if (exmode_active)
16447 buf[1] = 'v';
16448 }
16449 else if (exmode_active)
16450 {
16451 buf[0] = 'c';
16452 buf[1] = 'e';
16453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016457 if (finish_op)
16458 buf[1] = 'o';
16459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016461 /* Clear out the minor mode when the argument is not a non-zero number or
16462 * non-empty string. */
16463 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016464 buf[1] = NUL;
16465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016466 rettv->vval.v_string = vim_strsave(buf);
16467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468}
16469
Bram Moolenaar429fa852013-04-15 12:27:36 +020016470#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016471/*
16472 * "mzeval()" function
16473 */
16474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016476{
16477 char_u *str;
16478 char_u buf[NUMBUFLEN];
16479
16480 str = get_tv_string_buf(&argvars[0], buf);
16481 do_mzeval(str, rettv);
16482}
Bram Moolenaar75676462013-01-30 14:55:42 +010016483
16484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016486{
16487 typval_T argvars[3];
16488
16489 argvars[0].v_type = VAR_STRING;
16490 argvars[0].vval.v_string = name;
16491 copy_tv(args, &argvars[1]);
16492 argvars[2].v_type = VAR_UNKNOWN;
16493 f_call(argvars, rettv);
16494 clear_tv(&argvars[1]);
16495}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016496#endif
16497
Bram Moolenaar071d4272004-06-13 20:20:40 +000016498/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499 * "nextnonblank()" function
16500 */
16501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016502f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503{
16504 linenr_T lnum;
16505
16506 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016508 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016509 {
16510 lnum = 0;
16511 break;
16512 }
16513 if (*skipwhite(ml_get(lnum)) != NUL)
16514 break;
16515 }
16516 rettv->vval.v_number = lnum;
16517}
16518
16519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 * "nr2char()" function
16521 */
16522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016524{
16525 char_u buf[NUMBUFLEN];
16526
16527#ifdef FEAT_MBYTE
16528 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016529 {
16530 int utf8 = 0;
16531
16532 if (argvars[1].v_type != VAR_UNKNOWN)
16533 utf8 = get_tv_number_chk(&argvars[1], NULL);
16534 if (utf8)
16535 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16536 else
16537 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 else
16540#endif
16541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016542 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 buf[1] = NUL;
16544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016545 rettv->v_type = VAR_STRING;
16546 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016547}
16548
16549/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016550 * "or(expr, expr)" function
16551 */
16552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016553f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016554{
16555 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16556 | get_tv_number_chk(&argvars[1], NULL);
16557}
16558
16559/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016560 * "pathshorten()" function
16561 */
16562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016563f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016564{
16565 char_u *p;
16566
16567 rettv->v_type = VAR_STRING;
16568 p = get_tv_string_chk(&argvars[0]);
16569 if (p == NULL)
16570 rettv->vval.v_string = NULL;
16571 else
16572 {
16573 p = vim_strsave(p);
16574 rettv->vval.v_string = p;
16575 if (p != NULL)
16576 shorten_dir(p);
16577 }
16578}
16579
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016580#ifdef FEAT_PERL
16581/*
16582 * "perleval()" function
16583 */
16584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016585f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016586{
16587 char_u *str;
16588 char_u buf[NUMBUFLEN];
16589
16590 str = get_tv_string_buf(&argvars[0], buf);
16591 do_perleval(str, rettv);
16592}
16593#endif
16594
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016595#ifdef FEAT_FLOAT
16596/*
16597 * "pow()" function
16598 */
16599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016600f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016601{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016602 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016603
16604 rettv->v_type = VAR_FLOAT;
16605 if (get_float_arg(argvars, &fx) == OK
16606 && get_float_arg(&argvars[1], &fy) == OK)
16607 rettv->vval.v_float = pow(fx, fy);
16608 else
16609 rettv->vval.v_float = 0.0;
16610}
16611#endif
16612
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016613/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016614 * "prevnonblank()" function
16615 */
16616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016617f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618{
16619 linenr_T lnum;
16620
16621 lnum = get_tv_lnum(argvars);
16622 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16623 lnum = 0;
16624 else
16625 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16626 --lnum;
16627 rettv->vval.v_number = lnum;
16628}
16629
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016630/* This dummy va_list is here because:
16631 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16632 * - locally in the function results in a "used before set" warning
16633 * - using va_start() to initialize it gives "function with fixed args" error */
16634static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016635
Bram Moolenaar8c711452005-01-14 21:53:12 +000016636/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016637 * "printf()" function
16638 */
16639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016641{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016642 char_u buf[NUMBUFLEN];
16643 int len;
16644 char_u *s;
16645 int saved_did_emsg = did_emsg;
16646 char *fmt;
16647
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016648 rettv->v_type = VAR_STRING;
16649 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016650
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016651 /* Get the required length, allocate the buffer and do it for real. */
16652 did_emsg = FALSE;
16653 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16654 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16655 if (!did_emsg)
16656 {
16657 s = alloc(len + 1);
16658 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016659 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016660 rettv->vval.v_string = s;
16661 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016662 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016663 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016664 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016665}
16666
16667/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016668 * "pumvisible()" function
16669 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016671f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016672{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016673#ifdef FEAT_INS_EXPAND
16674 if (pum_visible())
16675 rettv->vval.v_number = 1;
16676#endif
16677}
16678
Bram Moolenaardb913952012-06-29 12:54:53 +020016679#ifdef FEAT_PYTHON3
16680/*
16681 * "py3eval()" function
16682 */
16683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016684f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016685{
16686 char_u *str;
16687 char_u buf[NUMBUFLEN];
16688
16689 str = get_tv_string_buf(&argvars[0], buf);
16690 do_py3eval(str, rettv);
16691}
16692#endif
16693
16694#ifdef FEAT_PYTHON
16695/*
16696 * "pyeval()" function
16697 */
16698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016699f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016700{
16701 char_u *str;
16702 char_u buf[NUMBUFLEN];
16703
16704 str = get_tv_string_buf(&argvars[0], buf);
16705 do_pyeval(str, rettv);
16706}
16707#endif
16708
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016709/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016710 * "range()" function
16711 */
16712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016713f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016714{
16715 long start;
16716 long end;
16717 long stride = 1;
16718 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016722 if (argvars[1].v_type == VAR_UNKNOWN)
16723 {
16724 end = start - 1;
16725 start = 0;
16726 }
16727 else
16728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016731 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016732 }
16733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016734 if (error)
16735 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016736 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016737 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016738 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016739 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016740 else
16741 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016742 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016743 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016744 if (list_append_number(rettv->vval.v_list,
16745 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016746 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016747 }
16748}
16749
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016750/*
16751 * "readfile()" function
16752 */
16753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016754f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016755{
16756 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016757 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016758 char_u *fname;
16759 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016760 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16761 int io_size = sizeof(buf);
16762 int readlen; /* size of last fread() */
16763 char_u *prev = NULL; /* previously read bytes, if any */
16764 long prevlen = 0; /* length of data in prev */
16765 long prevsize = 0; /* size of prev buffer */
16766 long maxline = MAXLNUM;
16767 long cnt = 0;
16768 char_u *p; /* position in buf */
16769 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016770
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016771 if (argvars[1].v_type != VAR_UNKNOWN)
16772 {
16773 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16774 binary = TRUE;
16775 if (argvars[2].v_type != VAR_UNKNOWN)
16776 maxline = get_tv_number(&argvars[2]);
16777 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016778
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016779 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016780 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016781
16782 /* Always open the file in binary mode, library functions have a mind of
16783 * their own about CR-LF conversion. */
16784 fname = get_tv_string(&argvars[0]);
16785 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16786 {
16787 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16788 return;
16789 }
16790
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016791 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016792 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016793 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016795 /* This for loop processes what was read, but is also entered at end
16796 * of file so that either:
16797 * - an incomplete line gets written
16798 * - a "binary" file gets an empty line at the end if it ends in a
16799 * newline. */
16800 for (p = buf, start = buf;
16801 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16802 ++p)
16803 {
16804 if (*p == '\n' || readlen <= 0)
16805 {
16806 listitem_T *li;
16807 char_u *s = NULL;
16808 long_u len = p - start;
16809
16810 /* Finished a line. Remove CRs before NL. */
16811 if (readlen > 0 && !binary)
16812 {
16813 while (len > 0 && start[len - 1] == '\r')
16814 --len;
16815 /* removal may cross back to the "prev" string */
16816 if (len == 0)
16817 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16818 --prevlen;
16819 }
16820 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016821 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016822 else
16823 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016824 /* Change "prev" buffer to be the right size. This way
16825 * the bytes are only copied once, and very long lines are
16826 * allocated only once. */
16827 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016828 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016829 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016830 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016831 prev = NULL; /* the list will own the string */
16832 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016833 }
16834 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016835 if (s == NULL)
16836 {
16837 do_outofmem_msg((long_u) prevlen + len + 1);
16838 failed = TRUE;
16839 break;
16840 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016841
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016842 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016843 {
16844 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016845 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016846 break;
16847 }
16848 li->li_tv.v_type = VAR_STRING;
16849 li->li_tv.v_lock = 0;
16850 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016851 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016852
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016853 start = p + 1; /* step over newline */
16854 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016855 break;
16856 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016857 else if (*p == NUL)
16858 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016859#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016860 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16861 * when finding the BF and check the previous two bytes. */
16862 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016863 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016864 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16865 * + 1, these may be in the "prev" string. */
16866 char_u back1 = p >= buf + 1 ? p[-1]
16867 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16868 char_u back2 = p >= buf + 2 ? p[-2]
16869 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16870 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016871
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016872 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016873 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016874 char_u *dest = p - 2;
16875
16876 /* Usually a BOM is at the beginning of a file, and so at
16877 * the beginning of a line; then we can just step over it.
16878 */
16879 if (start == dest)
16880 start = p + 1;
16881 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016882 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 /* have to shuffle buf to close gap */
16884 int adjust_prevlen = 0;
16885
16886 if (dest < buf)
16887 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016888 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016889 dest = buf;
16890 }
16891 if (readlen > p - buf + 1)
16892 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16893 readlen -= 3 - adjust_prevlen;
16894 prevlen -= adjust_prevlen;
16895 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016896 }
16897 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016898 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016899#endif
16900 } /* for */
16901
16902 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16903 break;
16904 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016905 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016906 /* There's part of a line in buf, store it in "prev". */
16907 if (p - start + prevlen >= prevsize)
16908 {
16909 /* need bigger "prev" buffer */
16910 char_u *newprev;
16911
16912 /* A common use case is ordinary text files and "prev" gets a
16913 * fragment of a line, so the first allocation is made
16914 * small, to avoid repeatedly 'allocing' large and
16915 * 'reallocing' small. */
16916 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016917 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016918 else
16919 {
16920 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016921 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016922 prevsize = grow50pc > growmin ? grow50pc : growmin;
16923 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016924 newprev = prev == NULL ? alloc(prevsize)
16925 : vim_realloc(prev, prevsize);
16926 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016927 {
16928 do_outofmem_msg((long_u)prevsize);
16929 failed = TRUE;
16930 break;
16931 }
16932 prev = newprev;
16933 }
16934 /* Add the line part to end of "prev". */
16935 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016936 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016937 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016938 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016939
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016940 /*
16941 * For a negative line count use only the lines at the end of the file,
16942 * free the rest.
16943 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016944 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016945 while (cnt > -maxline)
16946 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016947 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016948 --cnt;
16949 }
16950
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016951 if (failed)
16952 {
16953 list_free(rettv->vval.v_list, TRUE);
16954 /* readfile doc says an empty list is returned on error */
16955 rettv->vval.v_list = list_alloc();
16956 }
16957
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016958 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016959 fclose(fd);
16960}
16961
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016962#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016963static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016964
16965/*
16966 * Convert a List to proftime_T.
16967 * Return FAIL when there is something wrong.
16968 */
16969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016970list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016971{
16972 long n1, n2;
16973 int error = FALSE;
16974
16975 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16976 || arg->vval.v_list->lv_len != 2)
16977 return FAIL;
16978 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16979 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16980# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016981 tm->HighPart = n1;
16982 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016983# else
16984 tm->tv_sec = n1;
16985 tm->tv_usec = n2;
16986# endif
16987 return error ? FAIL : OK;
16988}
16989#endif /* FEAT_RELTIME */
16990
16991/*
16992 * "reltime()" function
16993 */
16994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016995f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016996{
16997#ifdef FEAT_RELTIME
16998 proftime_T res;
16999 proftime_T start;
17000
17001 if (argvars[0].v_type == VAR_UNKNOWN)
17002 {
17003 /* No arguments: get current time. */
17004 profile_start(&res);
17005 }
17006 else if (argvars[1].v_type == VAR_UNKNOWN)
17007 {
17008 if (list2proftime(&argvars[0], &res) == FAIL)
17009 return;
17010 profile_end(&res);
17011 }
17012 else
17013 {
17014 /* Two arguments: compute the difference. */
17015 if (list2proftime(&argvars[0], &start) == FAIL
17016 || list2proftime(&argvars[1], &res) == FAIL)
17017 return;
17018 profile_sub(&res, &start);
17019 }
17020
17021 if (rettv_list_alloc(rettv) == OK)
17022 {
17023 long n1, n2;
17024
17025# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017026 n1 = res.HighPart;
17027 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017028# else
17029 n1 = res.tv_sec;
17030 n2 = res.tv_usec;
17031# endif
17032 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17033 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17034 }
17035#endif
17036}
17037
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017038#ifdef FEAT_FLOAT
17039/*
17040 * "reltimefloat()" function
17041 */
17042 static void
17043f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17044{
17045# ifdef FEAT_RELTIME
17046 proftime_T tm;
17047# endif
17048
17049 rettv->v_type = VAR_FLOAT;
17050 rettv->vval.v_float = 0;
17051# ifdef FEAT_RELTIME
17052 if (list2proftime(&argvars[0], &tm) == OK)
17053 rettv->vval.v_float = profile_float(&tm);
17054# endif
17055}
17056#endif
17057
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017058/*
17059 * "reltimestr()" function
17060 */
17061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017062f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017063{
17064#ifdef FEAT_RELTIME
17065 proftime_T tm;
17066#endif
17067
17068 rettv->v_type = VAR_STRING;
17069 rettv->vval.v_string = NULL;
17070#ifdef FEAT_RELTIME
17071 if (list2proftime(&argvars[0], &tm) == OK)
17072 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17073#endif
17074}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017075
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017077static void make_connection(void);
17078static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079
17080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017081make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082{
17083 if (X_DISPLAY == NULL
17084# ifdef FEAT_GUI
17085 && !gui.in_use
17086# endif
17087 )
17088 {
17089 x_force_connect = TRUE;
17090 setup_term_clip();
17091 x_force_connect = FALSE;
17092 }
17093}
17094
17095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017096check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097{
17098 make_connection();
17099 if (X_DISPLAY == NULL)
17100 {
17101 EMSG(_("E240: No connection to Vim server"));
17102 return FAIL;
17103 }
17104 return OK;
17105}
17106#endif
17107
17108#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017109static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110
17111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017112remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113{
17114 char_u *server_name;
17115 char_u *keys;
17116 char_u *r = NULL;
17117 char_u buf[NUMBUFLEN];
17118# ifdef WIN32
17119 HWND w;
17120# else
17121 Window w;
17122# endif
17123
17124 if (check_restricted() || check_secure())
17125 return;
17126
17127# ifdef FEAT_X11
17128 if (check_connection() == FAIL)
17129 return;
17130# endif
17131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 server_name = get_tv_string_chk(&argvars[0]);
17133 if (server_name == NULL)
17134 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 keys = get_tv_string_buf(&argvars[1], buf);
17136# ifdef WIN32
17137 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17138# else
17139 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17140 < 0)
17141# endif
17142 {
17143 if (r != NULL)
17144 EMSG(r); /* sending worked but evaluation failed */
17145 else
17146 EMSG2(_("E241: Unable to send to %s"), server_name);
17147 return;
17148 }
17149
17150 rettv->vval.v_string = r;
17151
17152 if (argvars[2].v_type != VAR_UNKNOWN)
17153 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017154 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017155 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017158 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017159 v.di_tv.v_type = VAR_STRING;
17160 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017161 idvar = get_tv_string_chk(&argvars[2]);
17162 if (idvar != NULL)
17163 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017164 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 }
17166}
17167#endif
17168
17169/*
17170 * "remote_expr()" function
17171 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017173f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174{
17175 rettv->v_type = VAR_STRING;
17176 rettv->vval.v_string = NULL;
17177#ifdef FEAT_CLIENTSERVER
17178 remote_common(argvars, rettv, TRUE);
17179#endif
17180}
17181
17182/*
17183 * "remote_foreground()" function
17184 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188#ifdef FEAT_CLIENTSERVER
17189# ifdef WIN32
17190 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017191 {
17192 char_u *server_name = get_tv_string_chk(&argvars[0]);
17193
17194 if (server_name != NULL)
17195 serverForeground(server_name);
17196 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197# else
17198 /* Send a foreground() expression to the server. */
17199 argvars[1].v_type = VAR_STRING;
17200 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17201 argvars[2].v_type = VAR_UNKNOWN;
17202 remote_common(argvars, rettv, TRUE);
17203 vim_free(argvars[1].vval.v_string);
17204# endif
17205#endif
17206}
17207
Bram Moolenaar0d660222005-01-07 21:51:51 +000017208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017209f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210{
17211#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017212 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017213 char_u *s = NULL;
17214# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017215 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017216# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017217 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218
17219 if (check_restricted() || check_secure())
17220 {
17221 rettv->vval.v_number = -1;
17222 return;
17223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017224 serverid = get_tv_string_chk(&argvars[0]);
17225 if (serverid == NULL)
17226 {
17227 rettv->vval.v_number = -1;
17228 return; /* type error; errmsg already given */
17229 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017231 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017232 if (n == 0)
17233 rettv->vval.v_number = -1;
17234 else
17235 {
17236 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17237 rettv->vval.v_number = (s != NULL);
17238 }
17239# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017240 if (check_connection() == FAIL)
17241 return;
17242
17243 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017244 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245# endif
17246
17247 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017249 char_u *retvar;
17250
Bram Moolenaar33570922005-01-25 22:26:29 +000017251 v.di_tv.v_type = VAR_STRING;
17252 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017253 retvar = get_tv_string_chk(&argvars[1]);
17254 if (retvar != NULL)
17255 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017256 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257 }
17258#else
17259 rettv->vval.v_number = -1;
17260#endif
17261}
17262
Bram Moolenaar0d660222005-01-07 21:51:51 +000017263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017264f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017265{
17266 char_u *r = NULL;
17267
17268#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017269 char_u *serverid = get_tv_string_chk(&argvars[0]);
17270
17271 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272 {
17273# ifdef WIN32
17274 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017275 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017277 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 if (n != 0)
17279 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17280 if (r == NULL)
17281# else
17282 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017283 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284# endif
17285 EMSG(_("E277: Unable to read a server reply"));
17286 }
17287#endif
17288 rettv->v_type = VAR_STRING;
17289 rettv->vval.v_string = r;
17290}
17291
17292/*
17293 * "remote_send()" function
17294 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017296f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017297{
17298 rettv->v_type = VAR_STRING;
17299 rettv->vval.v_string = NULL;
17300#ifdef FEAT_CLIENTSERVER
17301 remote_common(argvars, rettv, FALSE);
17302#endif
17303}
17304
17305/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017306 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017307 */
17308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017310{
Bram Moolenaar33570922005-01-25 22:26:29 +000017311 list_T *l;
17312 listitem_T *item, *item2;
17313 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017314 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017315 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017316 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017317 dict_T *d;
17318 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017319 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320
Bram Moolenaar8c711452005-01-14 21:53:12 +000017321 if (argvars[0].v_type == VAR_DICT)
17322 {
17323 if (argvars[2].v_type != VAR_UNKNOWN)
17324 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017325 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017326 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017328 key = get_tv_string_chk(&argvars[1]);
17329 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017331 di = dict_find(d, key, -1);
17332 if (di == NULL)
17333 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017334 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17335 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 {
17337 *rettv = di->di_tv;
17338 init_tv(&di->di_tv);
17339 dictitem_remove(d, di);
17340 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017341 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017342 }
17343 }
17344 else if (argvars[0].v_type != VAR_LIST)
17345 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017346 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017347 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 int error = FALSE;
17350
17351 idx = get_tv_number_chk(&argvars[1], &error);
17352 if (error)
17353 ; /* type error: do nothing, errmsg already given */
17354 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017355 EMSGN(_(e_listidx), idx);
17356 else
17357 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017358 if (argvars[2].v_type == VAR_UNKNOWN)
17359 {
17360 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017361 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017362 *rettv = item->li_tv;
17363 vim_free(item);
17364 }
17365 else
17366 {
17367 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017368 end = get_tv_number_chk(&argvars[2], &error);
17369 if (error)
17370 ; /* type error: do nothing */
17371 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017372 EMSGN(_(e_listidx), end);
17373 else
17374 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017375 int cnt = 0;
17376
17377 for (li = item; li != NULL; li = li->li_next)
17378 {
17379 ++cnt;
17380 if (li == item2)
17381 break;
17382 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017383 if (li == NULL) /* didn't find "item2" after "item" */
17384 EMSG(_(e_invrange));
17385 else
17386 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017387 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017388 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017389 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017390 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017391 l->lv_first = item;
17392 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017393 item->li_prev = NULL;
17394 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017395 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017396 }
17397 }
17398 }
17399 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017400 }
17401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402}
17403
17404/*
17405 * "rename({from}, {to})" function
17406 */
17407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017408f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409{
17410 char_u buf[NUMBUFLEN];
17411
17412 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017415 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17416 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417}
17418
17419/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017420 * "repeat()" function
17421 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017423f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017424{
17425 char_u *p;
17426 int n;
17427 int slen;
17428 int len;
17429 char_u *r;
17430 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017431
17432 n = get_tv_number(&argvars[1]);
17433 if (argvars[0].v_type == VAR_LIST)
17434 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017435 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017436 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017437 if (list_extend(rettv->vval.v_list,
17438 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017439 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017440 }
17441 else
17442 {
17443 p = get_tv_string(&argvars[0]);
17444 rettv->v_type = VAR_STRING;
17445 rettv->vval.v_string = NULL;
17446
17447 slen = (int)STRLEN(p);
17448 len = slen * n;
17449 if (len <= 0)
17450 return;
17451
17452 r = alloc(len + 1);
17453 if (r != NULL)
17454 {
17455 for (i = 0; i < n; i++)
17456 mch_memmove(r + i * slen, p, (size_t)slen);
17457 r[len] = NUL;
17458 }
17459
17460 rettv->vval.v_string = r;
17461 }
17462}
17463
17464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465 * "resolve()" function
17466 */
17467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017468f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469{
17470 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017471#ifdef HAVE_READLINK
17472 char_u *buf = NULL;
17473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017475 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476#ifdef FEAT_SHORTCUT
17477 {
17478 char_u *v = NULL;
17479
17480 v = mch_resolve_shortcut(p);
17481 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017482 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017484 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 }
17486#else
17487# ifdef HAVE_READLINK
17488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 char_u *cpy;
17490 int len;
17491 char_u *remain = NULL;
17492 char_u *q;
17493 int is_relative_to_current = FALSE;
17494 int has_trailing_pathsep = FALSE;
17495 int limit = 100;
17496
17497 p = vim_strsave(p);
17498
17499 if (p[0] == '.' && (vim_ispathsep(p[1])
17500 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17501 is_relative_to_current = TRUE;
17502
17503 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017504 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017507 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509
17510 q = getnextcomp(p);
17511 if (*q != NUL)
17512 {
17513 /* Separate the first path component in "p", and keep the
17514 * remainder (beginning with the path separator). */
17515 remain = vim_strsave(q - 1);
17516 q[-1] = NUL;
17517 }
17518
Bram Moolenaard9462e32011-04-11 21:35:11 +020017519 buf = alloc(MAXPATHL + 1);
17520 if (buf == NULL)
17521 goto fail;
17522
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 for (;;)
17524 {
17525 for (;;)
17526 {
17527 len = readlink((char *)p, (char *)buf, MAXPATHL);
17528 if (len <= 0)
17529 break;
17530 buf[len] = NUL;
17531
17532 if (limit-- == 0)
17533 {
17534 vim_free(p);
17535 vim_free(remain);
17536 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 goto fail;
17539 }
17540
17541 /* Ensure that the result will have a trailing path separator
17542 * if the argument has one. */
17543 if (remain == NULL && has_trailing_pathsep)
17544 add_pathsep(buf);
17545
17546 /* Separate the first path component in the link value and
17547 * concatenate the remainders. */
17548 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17549 if (*q != NUL)
17550 {
17551 if (remain == NULL)
17552 remain = vim_strsave(q - 1);
17553 else
17554 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017555 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 if (cpy != NULL)
17557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 vim_free(remain);
17559 remain = cpy;
17560 }
17561 }
17562 q[-1] = NUL;
17563 }
17564
17565 q = gettail(p);
17566 if (q > p && *q == NUL)
17567 {
17568 /* Ignore trailing path separator. */
17569 q[-1] = NUL;
17570 q = gettail(p);
17571 }
17572 if (q > p && !mch_isFullName(buf))
17573 {
17574 /* symlink is relative to directory of argument */
17575 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17576 if (cpy != NULL)
17577 {
17578 STRCPY(cpy, p);
17579 STRCPY(gettail(cpy), buf);
17580 vim_free(p);
17581 p = cpy;
17582 }
17583 }
17584 else
17585 {
17586 vim_free(p);
17587 p = vim_strsave(buf);
17588 }
17589 }
17590
17591 if (remain == NULL)
17592 break;
17593
17594 /* Append the first path component of "remain" to "p". */
17595 q = getnextcomp(remain + 1);
17596 len = q - remain - (*q != NUL);
17597 cpy = vim_strnsave(p, STRLEN(p) + len);
17598 if (cpy != NULL)
17599 {
17600 STRNCAT(cpy, remain, len);
17601 vim_free(p);
17602 p = cpy;
17603 }
17604 /* Shorten "remain". */
17605 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017606 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 else
17608 {
17609 vim_free(remain);
17610 remain = NULL;
17611 }
17612 }
17613
17614 /* If the result is a relative path name, make it explicitly relative to
17615 * the current directory if and only if the argument had this form. */
17616 if (!vim_ispathsep(*p))
17617 {
17618 if (is_relative_to_current
17619 && *p != NUL
17620 && !(p[0] == '.'
17621 && (p[1] == NUL
17622 || vim_ispathsep(p[1])
17623 || (p[1] == '.'
17624 && (p[2] == NUL
17625 || vim_ispathsep(p[2]))))))
17626 {
17627 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017628 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 if (cpy != NULL)
17630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 vim_free(p);
17632 p = cpy;
17633 }
17634 }
17635 else if (!is_relative_to_current)
17636 {
17637 /* Strip leading "./". */
17638 q = p;
17639 while (q[0] == '.' && vim_ispathsep(q[1]))
17640 q += 2;
17641 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 }
17644 }
17645
17646 /* Ensure that the result will have no trailing path separator
17647 * if the argument had none. But keep "/" or "//". */
17648 if (!has_trailing_pathsep)
17649 {
17650 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017651 if (after_pathsep(p, q))
17652 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 }
17654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 }
17657# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017658 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659# endif
17660#endif
17661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663
17664#ifdef HAVE_READLINK
17665fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017666 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669}
17670
17671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 */
17674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017675f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676{
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 list_T *l;
17678 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679
Bram Moolenaar0d660222005-01-07 21:51:51 +000017680 if (argvars[0].v_type != VAR_LIST)
17681 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017682 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017683 && !tv_check_lock(l->lv_lock,
17684 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685 {
17686 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017687 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017688 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 while (li != NULL)
17690 {
17691 ni = li->li_prev;
17692 list_append(l, li);
17693 li = ni;
17694 }
17695 rettv->vval.v_list = l;
17696 rettv->v_type = VAR_LIST;
17697 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017698 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700}
17701
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017702#define SP_NOMOVE 0x01 /* don't move cursor */
17703#define SP_REPEAT 0x02 /* repeat to find outer pair */
17704#define SP_RETCOUNT 0x04 /* return matchcount */
17705#define SP_SETPCMARK 0x08 /* set previous context mark */
17706#define SP_START 0x10 /* accept match at start position */
17707#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17708#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017709#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017710
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017711static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712
17713/*
17714 * Get flags for a search function.
17715 * Possibly sets "p_ws".
17716 * Returns BACKWARD, FORWARD or zero (for an error).
17717 */
17718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017719get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017720{
17721 int dir = FORWARD;
17722 char_u *flags;
17723 char_u nbuf[NUMBUFLEN];
17724 int mask;
17725
17726 if (varp->v_type != VAR_UNKNOWN)
17727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017728 flags = get_tv_string_buf_chk(varp, nbuf);
17729 if (flags == NULL)
17730 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017731 while (*flags != NUL)
17732 {
17733 switch (*flags)
17734 {
17735 case 'b': dir = BACKWARD; break;
17736 case 'w': p_ws = TRUE; break;
17737 case 'W': p_ws = FALSE; break;
17738 default: mask = 0;
17739 if (flagsp != NULL)
17740 switch (*flags)
17741 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017742 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017743 case 'e': mask = SP_END; break;
17744 case 'm': mask = SP_RETCOUNT; break;
17745 case 'n': mask = SP_NOMOVE; break;
17746 case 'p': mask = SP_SUBPAT; break;
17747 case 'r': mask = SP_REPEAT; break;
17748 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017749 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017750 }
17751 if (mask == 0)
17752 {
17753 EMSG2(_(e_invarg2), flags);
17754 dir = 0;
17755 }
17756 else
17757 *flagsp |= mask;
17758 }
17759 if (dir == 0)
17760 break;
17761 ++flags;
17762 }
17763 }
17764 return dir;
17765}
17766
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017768 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017771search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017773 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 char_u *pat;
17775 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017776 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 int save_p_ws = p_ws;
17778 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017779 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017780 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017781 proftime_T tm;
17782#ifdef FEAT_RELTIME
17783 long time_limit = 0;
17784#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017785 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017786 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017788 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017789 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017790 if (dir == 0)
17791 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017792 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017793 if (flags & SP_START)
17794 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017795 if (flags & SP_END)
17796 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017797 if (flags & SP_COLUMN)
17798 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017799
Bram Moolenaar76929292008-01-06 19:07:36 +000017800 /* Optional arguments: line number to stop searching and timeout. */
17801 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017802 {
17803 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17804 if (lnum_stop < 0)
17805 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017806#ifdef FEAT_RELTIME
17807 if (argvars[3].v_type != VAR_UNKNOWN)
17808 {
17809 time_limit = get_tv_number_chk(&argvars[3], NULL);
17810 if (time_limit < 0)
17811 goto theend;
17812 }
17813#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017814 }
17815
Bram Moolenaar76929292008-01-06 19:07:36 +000017816#ifdef FEAT_RELTIME
17817 /* Set the time limit, if there is one. */
17818 profile_setlimit(time_limit, &tm);
17819#endif
17820
Bram Moolenaar231334e2005-07-25 20:46:57 +000017821 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017822 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017823 * Check to make sure only those flags are set.
17824 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17825 * flags cannot be set. Check for that condition also.
17826 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017827 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017828 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017830 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017831 goto theend;
17832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017834 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017835 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017836 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017837 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017839 if (flags & SP_SUBPAT)
17840 retval = subpatnum;
17841 else
17842 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017843 if (flags & SP_SETPCMARK)
17844 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017846 if (match_pos != NULL)
17847 {
17848 /* Store the match cursor position */
17849 match_pos->lnum = pos.lnum;
17850 match_pos->col = pos.col + 1;
17851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 /* "/$" will put the cursor after the end of the line, may need to
17853 * correct that here */
17854 check_cursor();
17855 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017856
17857 /* If 'n' flag is used: restore cursor position. */
17858 if (flags & SP_NOMOVE)
17859 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017860 else
17861 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017862theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017864
17865 return retval;
17866}
17867
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017868#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017869
17870/*
17871 * round() is not in C90, use ceil() or floor() instead.
17872 */
17873 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017874vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017875{
17876 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17877}
17878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017879/*
17880 * "round({float})" function
17881 */
17882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017883f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017884{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017885 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017886
17887 rettv->v_type = VAR_FLOAT;
17888 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017889 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017890 else
17891 rettv->vval.v_float = 0.0;
17892}
17893#endif
17894
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017895/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017896 * "screenattr()" function
17897 */
17898 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017899f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017900{
17901 int row;
17902 int col;
17903 int c;
17904
17905 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17906 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17907 if (row < 0 || row >= screen_Rows
17908 || col < 0 || col >= screen_Columns)
17909 c = -1;
17910 else
17911 c = ScreenAttrs[LineOffset[row] + col];
17912 rettv->vval.v_number = c;
17913}
17914
17915/*
17916 * "screenchar()" function
17917 */
17918 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017919f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017920{
17921 int row;
17922 int col;
17923 int off;
17924 int c;
17925
17926 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17927 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17928 if (row < 0 || row >= screen_Rows
17929 || col < 0 || col >= screen_Columns)
17930 c = -1;
17931 else
17932 {
17933 off = LineOffset[row] + col;
17934#ifdef FEAT_MBYTE
17935 if (enc_utf8 && ScreenLinesUC[off] != 0)
17936 c = ScreenLinesUC[off];
17937 else
17938#endif
17939 c = ScreenLines[off];
17940 }
17941 rettv->vval.v_number = c;
17942}
17943
17944/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017945 * "screencol()" function
17946 *
17947 * First column is 1 to be consistent with virtcol().
17948 */
17949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017950f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017951{
17952 rettv->vval.v_number = screen_screencol() + 1;
17953}
17954
17955/*
17956 * "screenrow()" function
17957 */
17958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017959f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017960{
17961 rettv->vval.v_number = screen_screenrow() + 1;
17962}
17963
17964/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965 * "search()" function
17966 */
17967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017968f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017969{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017970 int flags = 0;
17971
17972 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973}
17974
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017976 * "searchdecl()" function
17977 */
17978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017979f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017980{
17981 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017982 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017983 int error = FALSE;
17984 char_u *name;
17985
17986 rettv->vval.v_number = 1; /* default: FAIL */
17987
17988 name = get_tv_string_chk(&argvars[0]);
17989 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017990 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017991 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017992 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17993 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17994 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017995 if (!error && name != NULL)
17996 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017997 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017998}
17999
18000/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018001 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018004searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005{
18006 char_u *spat, *mpat, *epat;
18007 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 int dir;
18010 int flags = 0;
18011 char_u nbuf1[NUMBUFLEN];
18012 char_u nbuf2[NUMBUFLEN];
18013 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018014 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018015 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018016 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018019 spat = get_tv_string_chk(&argvars[0]);
18020 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18021 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18022 if (spat == NULL || mpat == NULL || epat == NULL)
18023 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 /* Handle the optional fourth argument: flags */
18026 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018027 if (dir == 0)
18028 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018029
18030 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018031 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18032 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018033 if ((flags & (SP_END | SP_SUBPAT)) != 0
18034 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018035 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018036 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018037 goto theend;
18038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018040 /* Using 'r' implies 'W', otherwise it doesn't work. */
18041 if (flags & SP_REPEAT)
18042 p_ws = FALSE;
18043
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018044 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018045 if (argvars[3].v_type == VAR_UNKNOWN
18046 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047 skip = (char_u *)"";
18048 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018050 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018051 if (argvars[5].v_type != VAR_UNKNOWN)
18052 {
18053 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
18054 if (lnum_stop < 0)
18055 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018056#ifdef FEAT_RELTIME
18057 if (argvars[6].v_type != VAR_UNKNOWN)
18058 {
18059 time_limit = get_tv_number_chk(&argvars[6], NULL);
18060 if (time_limit < 0)
18061 goto theend;
18062 }
18063#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018064 }
18065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018066 if (skip == NULL)
18067 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018069 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018070 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018071
18072theend:
18073 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018074
18075 return retval;
18076}
18077
18078/*
18079 * "searchpair()" function
18080 */
18081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018082f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018083{
18084 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18085}
18086
18087/*
18088 * "searchpairpos()" function
18089 */
18090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018091f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018092{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018093 pos_T match_pos;
18094 int lnum = 0;
18095 int col = 0;
18096
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018097 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018098 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018099
18100 if (searchpair_cmn(argvars, &match_pos) > 0)
18101 {
18102 lnum = match_pos.lnum;
18103 col = match_pos.col;
18104 }
18105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018106 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18107 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018108}
18109
18110/*
18111 * Search for a start/middle/end thing.
18112 * Used by searchpair(), see its documentation for the details.
18113 * Returns 0 or -1 for no match,
18114 */
18115 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018116do_searchpair(
18117 char_u *spat, /* start pattern */
18118 char_u *mpat, /* middle pattern */
18119 char_u *epat, /* end pattern */
18120 int dir, /* BACKWARD or FORWARD */
18121 char_u *skip, /* skip expression */
18122 int flags, /* SP_SETPCMARK and other SP_ values */
18123 pos_T *match_pos,
18124 linenr_T lnum_stop, /* stop at this line if not zero */
18125 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018126{
18127 char_u *save_cpo;
18128 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18129 long retval = 0;
18130 pos_T pos;
18131 pos_T firstpos;
18132 pos_T foundpos;
18133 pos_T save_cursor;
18134 pos_T save_pos;
18135 int n;
18136 int r;
18137 int nest = 1;
18138 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018139 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018140 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018141
18142 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18143 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018144 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018145
Bram Moolenaar76929292008-01-06 19:07:36 +000018146#ifdef FEAT_RELTIME
18147 /* Set the time limit, if there is one. */
18148 profile_setlimit(time_limit, &tm);
18149#endif
18150
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018151 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18152 * start/middle/end (pat3, for the top pair). */
18153 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18154 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18155 if (pat2 == NULL || pat3 == NULL)
18156 goto theend;
18157 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18158 if (*mpat == NUL)
18159 STRCPY(pat3, pat2);
18160 else
18161 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18162 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018163 if (flags & SP_START)
18164 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018165
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 save_cursor = curwin->w_cursor;
18167 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018168 clearpos(&firstpos);
18169 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 pat = pat3;
18171 for (;;)
18172 {
18173 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018174 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18176 /* didn't find it or found the first match again: FAIL */
18177 break;
18178
18179 if (firstpos.lnum == 0)
18180 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018181 if (equalpos(pos, foundpos))
18182 {
18183 /* Found the same position again. Can happen with a pattern that
18184 * has "\zs" at the end and searching backwards. Advance one
18185 * character and try again. */
18186 if (dir == BACKWARD)
18187 decl(&pos);
18188 else
18189 incl(&pos);
18190 }
18191 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018193 /* clear the start flag to avoid getting stuck here */
18194 options &= ~SEARCH_START;
18195
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 /* If the skip pattern matches, ignore this match. */
18197 if (*skip != NUL)
18198 {
18199 save_pos = curwin->w_cursor;
18200 curwin->w_cursor = pos;
18201 r = eval_to_bool(skip, &err, NULL, FALSE);
18202 curwin->w_cursor = save_pos;
18203 if (err)
18204 {
18205 /* Evaluating {skip} caused an error, break here. */
18206 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018207 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018208 break;
18209 }
18210 if (r)
18211 continue;
18212 }
18213
18214 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18215 {
18216 /* Found end when searching backwards or start when searching
18217 * forward: nested pair. */
18218 ++nest;
18219 pat = pat2; /* nested, don't search for middle */
18220 }
18221 else
18222 {
18223 /* Found end when searching forward or start when searching
18224 * backward: end of (nested) pair; or found middle in outer pair. */
18225 if (--nest == 1)
18226 pat = pat3; /* outer level, search for middle */
18227 }
18228
18229 if (nest == 0)
18230 {
18231 /* Found the match: return matchcount or line number. */
18232 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018233 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018235 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018236 if (flags & SP_SETPCMARK)
18237 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 curwin->w_cursor = pos;
18239 if (!(flags & SP_REPEAT))
18240 break;
18241 nest = 1; /* search for next unmatched */
18242 }
18243 }
18244
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018245 if (match_pos != NULL)
18246 {
18247 /* Store the match cursor position */
18248 match_pos->lnum = curwin->w_cursor.lnum;
18249 match_pos->col = curwin->w_cursor.col + 1;
18250 }
18251
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018253 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 curwin->w_cursor = save_cursor;
18255
18256theend:
18257 vim_free(pat2);
18258 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018259 if (p_cpo == empty_option)
18260 p_cpo = save_cpo;
18261 else
18262 /* Darn, evaluating the {skip} expression changed the value. */
18263 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018264
18265 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266}
18267
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018268/*
18269 * "searchpos()" function
18270 */
18271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018272f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018273{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018274 pos_T match_pos;
18275 int lnum = 0;
18276 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018277 int n;
18278 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018279
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018280 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018281 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018282
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018283 n = search_cmn(argvars, &match_pos, &flags);
18284 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018285 {
18286 lnum = match_pos.lnum;
18287 col = match_pos.col;
18288 }
18289
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018290 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18291 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018292 if (flags & SP_SUBPAT)
18293 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018294}
18295
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018297f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299#ifdef FEAT_CLIENTSERVER
18300 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 char_u *server = get_tv_string_chk(&argvars[0]);
18302 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 if (server == NULL || reply == NULL)
18306 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307 if (check_restricted() || check_secure())
18308 return;
18309# ifdef FEAT_X11
18310 if (check_connection() == FAIL)
18311 return;
18312# endif
18313
18314 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316 EMSG(_("E258: Unable to send to client"));
18317 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018319 rettv->vval.v_number = 0;
18320#else
18321 rettv->vval.v_number = -1;
18322#endif
18323}
18324
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018327{
18328 char_u *r = NULL;
18329
18330#ifdef FEAT_CLIENTSERVER
18331# ifdef WIN32
18332 r = serverGetVimNames();
18333# else
18334 make_connection();
18335 if (X_DISPLAY != NULL)
18336 r = serverGetVimNames(X_DISPLAY);
18337# endif
18338#endif
18339 rettv->v_type = VAR_STRING;
18340 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341}
18342
18343/*
18344 * "setbufvar()" function
18345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018347f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348{
18349 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018352 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 char_u nbuf[NUMBUFLEN];
18354
18355 if (check_restricted() || check_secure())
18356 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018357 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18358 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018359 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360 varp = &argvars[2];
18361
18362 if (buf != NULL && varname != NULL && varp != NULL)
18363 {
18364 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366
18367 if (*varname == '&')
18368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018369 long numval;
18370 char_u *strval;
18371 int error = FALSE;
18372
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018374 numval = get_tv_number_chk(varp, &error);
18375 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018376 if (!error && strval != NULL)
18377 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 }
18379 else
18380 {
18381 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18382 if (bufvarname != NULL)
18383 {
18384 STRCPY(bufvarname, "b:");
18385 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018386 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387 vim_free(bufvarname);
18388 }
18389 }
18390
18391 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394}
18395
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018397f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018398{
18399 dict_T *d;
18400 dictitem_T *di;
18401 char_u *csearch;
18402
18403 if (argvars[0].v_type != VAR_DICT)
18404 {
18405 EMSG(_(e_dictreq));
18406 return;
18407 }
18408
18409 if ((d = argvars[0].vval.v_dict) != NULL)
18410 {
18411 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18412 if (csearch != NULL)
18413 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018414#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018415 if (enc_utf8)
18416 {
18417 int pcc[MAX_MCO];
18418 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018419
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018420 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18421 }
18422 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018423#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018424 set_last_csearch(PTR2CHAR(csearch),
18425 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018426 }
18427
18428 di = dict_find(d, (char_u *)"forward", -1);
18429 if (di != NULL)
18430 set_csearch_direction(get_tv_number(&di->di_tv)
18431 ? FORWARD : BACKWARD);
18432
18433 di = dict_find(d, (char_u *)"until", -1);
18434 if (di != NULL)
18435 set_csearch_until(!!get_tv_number(&di->di_tv));
18436 }
18437}
18438
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439/*
18440 * "setcmdpos()" function
18441 */
18442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018443f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 int pos = (int)get_tv_number(&argvars[0]) - 1;
18446
18447 if (pos >= 0)
18448 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018449}
18450
18451/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018452 * "setfperm({fname}, {mode})" function
18453 */
18454 static void
18455f_setfperm(typval_T *argvars, typval_T *rettv)
18456{
18457 char_u *fname;
18458 char_u modebuf[NUMBUFLEN];
18459 char_u *mode_str;
18460 int i;
18461 int mask;
18462 int mode = 0;
18463
18464 rettv->vval.v_number = 0;
18465 fname = get_tv_string_chk(&argvars[0]);
18466 if (fname == NULL)
18467 return;
18468 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18469 if (mode_str == NULL)
18470 return;
18471 if (STRLEN(mode_str) != 9)
18472 {
18473 EMSG2(_(e_invarg2), mode_str);
18474 return;
18475 }
18476
18477 mask = 1;
18478 for (i = 8; i >= 0; --i)
18479 {
18480 if (mode_str[i] != '-')
18481 mode |= mask;
18482 mask = mask << 1;
18483 }
18484 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18485}
18486
18487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 * "setline()" function
18489 */
18490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018491f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492{
18493 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018494 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018495 list_T *l = NULL;
18496 listitem_T *li = NULL;
18497 long added = 0;
18498 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018500 lnum = get_tv_lnum(&argvars[0]);
18501 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018503 l = argvars[1].vval.v_list;
18504 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018506 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018507 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018508
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018509 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018510 for (;;)
18511 {
18512 if (l != NULL)
18513 {
18514 /* list argument, get next string */
18515 if (li == NULL)
18516 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018518 li = li->li_next;
18519 }
18520
18521 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018522 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018523 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018524
18525 /* When coming here from Insert mode, sync undo, so that this can be
18526 * undone separately from what was previously inserted. */
18527 if (u_sync_once == 2)
18528 {
18529 u_sync_once = 1; /* notify that u_sync() was called */
18530 u_sync(TRUE);
18531 }
18532
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018533 if (lnum <= curbuf->b_ml.ml_line_count)
18534 {
18535 /* existing line, replace it */
18536 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18537 {
18538 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018539 if (lnum == curwin->w_cursor.lnum)
18540 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018541 rettv->vval.v_number = 0; /* OK */
18542 }
18543 }
18544 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18545 {
18546 /* lnum is one past the last line, append the line */
18547 ++added;
18548 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18549 rettv->vval.v_number = 0; /* OK */
18550 }
18551
18552 if (l == NULL) /* only one string argument */
18553 break;
18554 ++lnum;
18555 }
18556
18557 if (added > 0)
18558 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559}
18560
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018561static 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 +000018562
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018564 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018565 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018567set_qf_ll_list(
18568 win_T *wp UNUSED,
18569 typval_T *list_arg UNUSED,
18570 typval_T *action_arg UNUSED,
18571 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018572{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018573#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018574 char_u *act;
18575 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018576#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018577
Bram Moolenaar2641f772005-03-25 21:58:17 +000018578 rettv->vval.v_number = -1;
18579
18580#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018581 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018582 EMSG(_(e_listreq));
18583 else
18584 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018585 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018586
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018587 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018588 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018589 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 if (act == NULL)
18591 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018592 if (*act == 'a' || *act == 'r')
18593 action = *act;
18594 }
18595
Bram Moolenaar81484f42012-12-05 15:16:47 +010018596 if (l != NULL && set_errorlist(wp, l, action,
18597 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018598 rettv->vval.v_number = 0;
18599 }
18600#endif
18601}
18602
18603/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018604 * "setloclist()" function
18605 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018607f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018608{
18609 win_T *win;
18610
18611 rettv->vval.v_number = -1;
18612
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018613 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018614 if (win != NULL)
18615 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18616}
18617
18618/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018619 * "setmatches()" function
18620 */
18621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018622f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018623{
18624#ifdef FEAT_SEARCH_EXTRA
18625 list_T *l;
18626 listitem_T *li;
18627 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018628 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018629
18630 rettv->vval.v_number = -1;
18631 if (argvars[0].v_type != VAR_LIST)
18632 {
18633 EMSG(_(e_listreq));
18634 return;
18635 }
18636 if ((l = argvars[0].vval.v_list) != NULL)
18637 {
18638
18639 /* To some extent make sure that we are dealing with a list from
18640 * "getmatches()". */
18641 li = l->lv_first;
18642 while (li != NULL)
18643 {
18644 if (li->li_tv.v_type != VAR_DICT
18645 || (d = li->li_tv.vval.v_dict) == NULL)
18646 {
18647 EMSG(_(e_invarg));
18648 return;
18649 }
18650 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018651 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18652 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018653 && dict_find(d, (char_u *)"priority", -1) != NULL
18654 && dict_find(d, (char_u *)"id", -1) != NULL))
18655 {
18656 EMSG(_(e_invarg));
18657 return;
18658 }
18659 li = li->li_next;
18660 }
18661
18662 clear_matches(curwin);
18663 li = l->lv_first;
18664 while (li != NULL)
18665 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018666 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018667 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018668 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018669 char_u *group;
18670 int priority;
18671 int id;
18672 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018673
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018674 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018675 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18676 {
18677 if (s == NULL)
18678 {
18679 s = list_alloc();
18680 if (s == NULL)
18681 return;
18682 }
18683
18684 /* match from matchaddpos() */
18685 for (i = 1; i < 9; i++)
18686 {
18687 sprintf((char *)buf, (char *)"pos%d", i);
18688 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18689 {
18690 if (di->di_tv.v_type != VAR_LIST)
18691 return;
18692
18693 list_append_tv(s, &di->di_tv);
18694 s->lv_refcount++;
18695 }
18696 else
18697 break;
18698 }
18699 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018700
18701 group = get_dict_string(d, (char_u *)"group", FALSE);
18702 priority = (int)get_dict_number(d, (char_u *)"priority");
18703 id = (int)get_dict_number(d, (char_u *)"id");
18704 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18705 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18706 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018707 if (i == 0)
18708 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018709 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018710 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018711 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018712 }
18713 else
18714 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018715 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018716 list_unref(s);
18717 s = NULL;
18718 }
18719
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018720 li = li->li_next;
18721 }
18722 rettv->vval.v_number = 0;
18723 }
18724#endif
18725}
18726
18727/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018728 * "setpos()" function
18729 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018731f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018732{
18733 pos_T pos;
18734 int fnum;
18735 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018736 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018737
Bram Moolenaar08250432008-02-13 11:42:46 +000018738 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018739 name = get_tv_string_chk(argvars);
18740 if (name != NULL)
18741 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018742 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018743 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018744 if (--pos.col < 0)
18745 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018746 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018747 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018748 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018749 if (fnum == curbuf->b_fnum)
18750 {
18751 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018752 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018753 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018754 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018755 curwin->w_set_curswant = FALSE;
18756 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018757 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018758 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018759 }
18760 else
18761 EMSG(_(e_invarg));
18762 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018763 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18764 {
18765 /* set mark */
18766 if (setmark_pos(name[1], &pos, fnum) == OK)
18767 rettv->vval.v_number = 0;
18768 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018769 else
18770 EMSG(_(e_invarg));
18771 }
18772 }
18773}
18774
18775/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018776 * "setqflist()" function
18777 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018779f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018780{
18781 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18782}
18783
18784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 * "setreg()" function
18786 */
18787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018788f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789{
18790 int regname;
18791 char_u *strregname;
18792 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018793 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 int append;
18795 char_u yank_type;
18796 long block_len;
18797
18798 block_len = -1;
18799 yank_type = MAUTO;
18800 append = FALSE;
18801
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018802 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018803 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018805 if (strregname == NULL)
18806 return; /* type error; errmsg already given */
18807 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 if (regname == 0 || regname == '@')
18809 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018811 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 stropt = get_tv_string_chk(&argvars[2]);
18814 if (stropt == NULL)
18815 return; /* type error */
18816 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 switch (*stropt)
18818 {
18819 case 'a': case 'A': /* append */
18820 append = TRUE;
18821 break;
18822 case 'v': case 'c': /* character-wise selection */
18823 yank_type = MCHAR;
18824 break;
18825 case 'V': case 'l': /* line-wise selection */
18826 yank_type = MLINE;
18827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 case 'b': case Ctrl_V: /* block-wise selection */
18829 yank_type = MBLOCK;
18830 if (VIM_ISDIGIT(stropt[1]))
18831 {
18832 ++stropt;
18833 block_len = getdigits(&stropt) - 1;
18834 --stropt;
18835 }
18836 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 }
18838 }
18839
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018840 if (argvars[1].v_type == VAR_LIST)
18841 {
18842 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018843 char_u **allocval;
18844 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018845 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018846 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018847 int len = argvars[1].vval.v_list->lv_len;
18848 listitem_T *li;
18849
Bram Moolenaar7d647822014-04-05 21:28:56 +020018850 /* First half: use for pointers to result lines; second half: use for
18851 * pointers to allocated copies. */
18852 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018853 if (lstval == NULL)
18854 return;
18855 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018856 allocval = lstval + len + 2;
18857 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018858
18859 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18860 li = li->li_next)
18861 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018862 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018863 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018864 goto free_lstval;
18865 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018866 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018867 /* Need to make a copy, next get_tv_string_buf_chk() will
18868 * overwrite the string. */
18869 strval = vim_strsave(buf);
18870 if (strval == NULL)
18871 goto free_lstval;
18872 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018873 }
18874 *curval++ = strval;
18875 }
18876 *curval++ = NULL;
18877
18878 write_reg_contents_lst(regname, lstval, -1,
18879 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018880free_lstval:
18881 while (curallocval > allocval)
18882 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018883 vim_free(lstval);
18884 }
18885 else
18886 {
18887 strval = get_tv_string_chk(&argvars[1]);
18888 if (strval == NULL)
18889 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018890 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018893 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894}
18895
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018896/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018897 * "settabvar()" function
18898 */
18899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018900f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018901{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018902#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018903 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018904 tabpage_T *tp;
18905#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018906 char_u *varname, *tabvarname;
18907 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018908
18909 rettv->vval.v_number = 0;
18910
18911 if (check_restricted() || check_secure())
18912 return;
18913
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018914#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018915 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018916#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018917 varname = get_tv_string_chk(&argvars[1]);
18918 varp = &argvars[2];
18919
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018920 if (varname != NULL && varp != NULL
18921#ifdef FEAT_WINDOWS
18922 && tp != NULL
18923#endif
18924 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018925 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018926#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018927 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018928 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018929#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018930
18931 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18932 if (tabvarname != NULL)
18933 {
18934 STRCPY(tabvarname, "t:");
18935 STRCPY(tabvarname + 2, varname);
18936 set_var(tabvarname, varp, TRUE);
18937 vim_free(tabvarname);
18938 }
18939
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018940#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018941 /* Restore current tabpage */
18942 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018943 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018944#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018945 }
18946}
18947
18948/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018949 * "settabwinvar()" function
18950 */
18951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018952f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018953{
18954 setwinvar(argvars, rettv, 1);
18955}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
18957/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018958 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018963 setwinvar(argvars, rettv, 0);
18964}
18965
18966/*
18967 * "setwinvar()" and "settabwinvar()" functions
18968 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018969
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018972{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973 win_T *win;
18974#ifdef FEAT_WINDOWS
18975 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018976 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018977 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978#endif
18979 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018980 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018982 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983
18984 if (check_restricted() || check_secure())
18985 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018986
18987#ifdef FEAT_WINDOWS
18988 if (off == 1)
18989 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18990 else
18991 tp = curtab;
18992#endif
18993 win = find_win_by_nr(&argvars[off], tp);
18994 varname = get_tv_string_chk(&argvars[off + 1]);
18995 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996
18997 if (win != NULL && varname != NULL && varp != NULL)
18998 {
18999#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019000 need_switch_win = !(tp == curtab && win == curwin);
19001 if (!need_switch_win
19002 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019005 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019007 long numval;
19008 char_u *strval;
19009 int error = FALSE;
19010
19011 ++varname;
19012 numval = get_tv_number_chk(varp, &error);
19013 strval = get_tv_string_buf_chk(varp, nbuf);
19014 if (!error && strval != NULL)
19015 set_option_value(varname, numval, strval, OPT_LOCAL);
19016 }
19017 else
19018 {
19019 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19020 if (winvarname != NULL)
19021 {
19022 STRCPY(winvarname, "w:");
19023 STRCPY(winvarname + 2, varname);
19024 set_var(winvarname, varp, TRUE);
19025 vim_free(winvarname);
19026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 }
19028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019030 if (need_switch_win)
19031 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032#endif
19033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034}
19035
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019036#ifdef FEAT_CRYPT
19037/*
19038 * "sha256({string})" function
19039 */
19040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019041f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019042{
19043 char_u *p;
19044
19045 p = get_tv_string(&argvars[0]);
19046 rettv->vval.v_string = vim_strsave(
19047 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19048 rettv->v_type = VAR_STRING;
19049}
19050#endif /* FEAT_CRYPT */
19051
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019053 * "shellescape({string})" function
19054 */
19055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019056f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019057{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019058 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019059 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019060 rettv->v_type = VAR_STRING;
19061}
19062
19063/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019064 * shiftwidth() function
19065 */
19066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019067f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019068{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019069 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019070}
19071
19072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 */
19075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019076f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019077{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019078 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080 p = get_tv_string(&argvars[0]);
19081 rettv->vval.v_string = vim_strsave(p);
19082 simplify_filename(rettv->vval.v_string); /* simplify in place */
19083 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019084}
19085
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019086#ifdef FEAT_FLOAT
19087/*
19088 * "sin()" function
19089 */
19090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019091f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019092{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019093 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019094
19095 rettv->v_type = VAR_FLOAT;
19096 if (get_float_arg(argvars, &f) == OK)
19097 rettv->vval.v_float = sin(f);
19098 else
19099 rettv->vval.v_float = 0.0;
19100}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019101
19102/*
19103 * "sinh()" function
19104 */
19105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019106f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019107{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019108 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019109
19110 rettv->v_type = VAR_FLOAT;
19111 if (get_float_arg(argvars, &f) == OK)
19112 rettv->vval.v_float = sinh(f);
19113 else
19114 rettv->vval.v_float = 0.0;
19115}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019116#endif
19117
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118static int
19119#ifdef __BORLANDC__
19120 _RTLENTRYF
19121#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019122 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123static int
19124#ifdef __BORLANDC__
19125 _RTLENTRYF
19126#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019127 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019129/* struct used in the array that's given to qsort() */
19130typedef struct
19131{
19132 listitem_T *item;
19133 int idx;
19134} sortItem_T;
19135
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136/* struct storing information about current sort */
19137typedef struct
19138{
19139 int item_compare_ic;
19140 int item_compare_numeric;
19141 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019142#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019143 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019144#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019145 char_u *item_compare_func;
19146 dict_T *item_compare_selfdict;
19147 int item_compare_func_err;
19148 int item_compare_keep_zero;
19149} sortinfo_T;
19150static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019151static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152#define ITEM_COMPARE_FAIL 999
19153
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019155 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 static int
19158#ifdef __BORLANDC__
19159_RTLENTRYF
19160#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019161item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019163 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019164 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019166 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167 int res;
19168 char_u numbuf1[NUMBUFLEN];
19169 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 si1 = (sortItem_T *)s1;
19172 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019173 tv1 = &si1->item->li_tv;
19174 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019175
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019177 {
19178 long v1 = get_tv_number(tv1);
19179 long v2 = get_tv_number(tv2);
19180
19181 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19182 }
19183
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019184#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019186 {
19187 float_T v1 = get_tv_float(tv1);
19188 float_T v2 = get_tv_float(tv2);
19189
19190 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19191 }
19192#endif
19193
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019194 /* tv2string() puts quotes around a string and allocates memory. Don't do
19195 * that for string variables. Use a single quote when comparing with a
19196 * non-string to do what the docs promise. */
19197 if (tv1->v_type == VAR_STRING)
19198 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019199 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019200 p1 = (char_u *)"'";
19201 else
19202 p1 = tv1->vval.v_string;
19203 }
19204 else
19205 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19206 if (tv2->v_type == VAR_STRING)
19207 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019209 p2 = (char_u *)"'";
19210 else
19211 p2 = tv2->vval.v_string;
19212 }
19213 else
19214 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019215 if (p1 == NULL)
19216 p1 = (char_u *)"";
19217 if (p2 == NULL)
19218 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019220 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019221 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019222 res = STRICMP(p1, p2);
19223 else
19224 res = STRCMP(p1, p2);
19225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019227 {
19228 double n1, n2;
19229 n1 = strtod((char *)p1, (char **)&p1);
19230 n2 = strtod((char *)p2, (char **)&p2);
19231 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19232 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019233
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019234 /* When the result would be zero, compare the item indexes. Makes the
19235 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019237 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019238
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 vim_free(tofree1);
19240 vim_free(tofree2);
19241 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
19244 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019245#ifdef __BORLANDC__
19246_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019250 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019253 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019256 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019257 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 return 0;
19259
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019260 si1 = (sortItem_T *)s1;
19261 si2 = (sortItem_T *)s2;
19262
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019263 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019264 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019265 copy_tv(&si1->item->li_tv, &argv[0]);
19266 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019267
19268 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019269 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019270 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019271 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019272 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019273 clear_tv(&argv[0]);
19274 clear_tv(&argv[1]);
19275
19276 if (res == FAIL)
19277 res = ITEM_COMPARE_FAIL;
19278 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019279 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19280 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019281 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019283
19284 /* When the result would be zero, compare the pointers themselves. Makes
19285 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019287 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019288
Bram Moolenaar0d660222005-01-07 21:51:51 +000019289 return res;
19290}
19291
19292/*
19293 * "sort({list})" function
19294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019296do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019297{
Bram Moolenaar33570922005-01-25 22:26:29 +000019298 list_T *l;
19299 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019300 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301 sortinfo_T *old_sortinfo;
19302 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303 long len;
19304 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305
Bram Moolenaar0b962472016-02-22 22:51:33 +010019306 /* Pointer to current info struct used in compare function. Save and
19307 * restore the current one for nested calls. */
19308 old_sortinfo = sortinfo;
19309 sortinfo = &info;
19310
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019312 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 else
19314 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019315 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019316 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019317 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19318 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 rettv->vval.v_list = l;
19321 rettv->v_type = VAR_LIST;
19322 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 len = list_len(l);
19325 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019326 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327
Bram Moolenaar0b962472016-02-22 22:51:33 +010019328 info.item_compare_ic = FALSE;
19329 info.item_compare_numeric = FALSE;
19330 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019331#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019332 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019333#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 info.item_compare_func = NULL;
19335 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019336 if (argvars[1].v_type != VAR_UNKNOWN)
19337 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019338 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019339 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019340 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019341 else
19342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019343 int error = FALSE;
19344
19345 i = get_tv_number_chk(&argvars[1], &error);
19346 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019347 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019348 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019349 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019350 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019351 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019352 else if (i != 0)
19353 {
19354 EMSG(_(e_invarg));
19355 goto theend;
19356 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019357 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019358 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019359 if (*info.item_compare_func == NUL)
19360 {
19361 /* empty string means default sort */
19362 info.item_compare_func = NULL;
19363 }
19364 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019365 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019366 info.item_compare_func = NULL;
19367 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019368 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019369 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019370 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019371 info.item_compare_func = NULL;
19372 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019373 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019374#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019376 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019377 info.item_compare_func = NULL;
19378 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019379 }
19380#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019381 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019382 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019383 info.item_compare_func = NULL;
19384 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019385 }
19386 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019387 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019388
19389 if (argvars[2].v_type != VAR_UNKNOWN)
19390 {
19391 /* optional third argument: {dict} */
19392 if (argvars[2].v_type != VAR_DICT)
19393 {
19394 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019395 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019396 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019397 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019398 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400
Bram Moolenaar0d660222005-01-07 21:51:51 +000019401 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019402 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019404 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405
Bram Moolenaar327aa022014-03-25 18:24:23 +010019406 i = 0;
19407 if (sort)
19408 {
19409 /* sort(): ptrs will be the list to sort */
19410 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019411 {
19412 ptrs[i].item = li;
19413 ptrs[i].idx = i;
19414 ++i;
19415 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019416
Bram Moolenaar0b962472016-02-22 22:51:33 +010019417 info.item_compare_func_err = FALSE;
19418 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019419 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019420 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019421 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019422 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019423 EMSG(_("E702: Sort compare function failed"));
19424 else
19425 {
19426 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019427 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019428 info.item_compare_func == NULL
19429 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019430
Bram Moolenaar0b962472016-02-22 22:51:33 +010019431 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019432 {
19433 /* Clear the List and append the items in sorted order. */
19434 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19435 l->lv_len = 0;
19436 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019437 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019438 }
19439 }
19440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019442 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019443 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019444
19445 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019446 info.item_compare_func_err = FALSE;
19447 info.item_compare_keep_zero = TRUE;
19448 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019449 ? item_compare2 : item_compare;
19450
19451 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19452 li = li->li_next)
19453 {
19454 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19455 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019456 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019457 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019458 {
19459 EMSG(_("E882: Uniq compare function failed"));
19460 break;
19461 }
19462 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019463
Bram Moolenaar0b962472016-02-22 22:51:33 +010019464 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019465 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019466 while (--i >= 0)
19467 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019468 li = ptrs[i].item->li_next;
19469 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019470 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019471 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019472 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019473 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019474 list_fix_watch(l, li);
19475 listitem_free(li);
19476 l->lv_len--;
19477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019478 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019479 }
19480
19481 vim_free(ptrs);
19482 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019483theend:
19484 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019485}
19486
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019487/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019488 * "sort({list})" function
19489 */
19490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019491f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019492{
19493 do_sort_uniq(argvars, rettv, TRUE);
19494}
19495
19496/*
19497 * "uniq({list})" function
19498 */
19499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019500f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019501{
19502 do_sort_uniq(argvars, rettv, FALSE);
19503}
19504
19505/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019506 * "soundfold({word})" function
19507 */
19508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019509f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019510{
19511 char_u *s;
19512
19513 rettv->v_type = VAR_STRING;
19514 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019515#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019516 rettv->vval.v_string = eval_soundfold(s);
19517#else
19518 rettv->vval.v_string = vim_strsave(s);
19519#endif
19520}
19521
19522/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019523 * "spellbadword()" function
19524 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019526f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019527{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019528 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019529 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019530 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019531
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019532 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019533 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019534
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019535#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019536 if (argvars[0].v_type == VAR_UNKNOWN)
19537 {
19538 /* Find the start and length of the badly spelled word. */
19539 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19540 if (len != 0)
19541 word = ml_get_cursor();
19542 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019543 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019544 {
19545 char_u *str = get_tv_string_chk(&argvars[0]);
19546 int capcol = -1;
19547
19548 if (str != NULL)
19549 {
19550 /* Check the argument for spelling. */
19551 while (*str != NUL)
19552 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019553 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019554 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019555 {
19556 word = str;
19557 break;
19558 }
19559 str += len;
19560 }
19561 }
19562 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019563#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019565 list_append_string(rettv->vval.v_list, word, len);
19566 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019567 attr == HLF_SPB ? "bad" :
19568 attr == HLF_SPR ? "rare" :
19569 attr == HLF_SPL ? "local" :
19570 attr == HLF_SPC ? "caps" :
19571 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019572}
19573
19574/*
19575 * "spellsuggest()" function
19576 */
19577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019578f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019579{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019580#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019581 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019582 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019583 int maxcount;
19584 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019585 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019586 listitem_T *li;
19587 int need_capital = FALSE;
19588#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019590 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019591 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019592
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019593#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019594 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019595 {
19596 str = get_tv_string(&argvars[0]);
19597 if (argvars[1].v_type != VAR_UNKNOWN)
19598 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019599 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019600 if (maxcount <= 0)
19601 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019602 if (argvars[2].v_type != VAR_UNKNOWN)
19603 {
19604 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19605 if (typeerr)
19606 return;
19607 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019608 }
19609 else
19610 maxcount = 25;
19611
Bram Moolenaar4770d092006-01-12 23:22:24 +000019612 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019613
19614 for (i = 0; i < ga.ga_len; ++i)
19615 {
19616 str = ((char_u **)ga.ga_data)[i];
19617
19618 li = listitem_alloc();
19619 if (li == NULL)
19620 vim_free(str);
19621 else
19622 {
19623 li->li_tv.v_type = VAR_STRING;
19624 li->li_tv.v_lock = 0;
19625 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019626 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019627 }
19628 }
19629 ga_clear(&ga);
19630 }
19631#endif
19632}
19633
Bram Moolenaar0d660222005-01-07 21:51:51 +000019634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019635f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019636{
19637 char_u *str;
19638 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019639 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019640 regmatch_T regmatch;
19641 char_u patbuf[NUMBUFLEN];
19642 char_u *save_cpo;
19643 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019644 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019645 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019646 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019647
19648 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19649 save_cpo = p_cpo;
19650 p_cpo = (char_u *)"";
19651
19652 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019653 if (argvars[1].v_type != VAR_UNKNOWN)
19654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19656 if (pat == NULL)
19657 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019659 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019660 }
19661 if (pat == NULL || *pat == NUL)
19662 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019663
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019664 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019666 if (typeerr)
19667 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668
Bram Moolenaar0d660222005-01-07 21:51:51 +000019669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19670 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019673 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019674 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019675 if (*str == NUL)
19676 match = FALSE; /* empty item at the end */
19677 else
19678 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019679 if (match)
19680 end = regmatch.startp[0];
19681 else
19682 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019683 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19684 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019685 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019686 if (list_append_string(rettv->vval.v_list, str,
19687 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019688 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019689 }
19690 if (!match)
19691 break;
19692 /* Advance to just after the match. */
19693 if (regmatch.endp[0] > str)
19694 col = 0;
19695 else
19696 {
19697 /* Don't get stuck at the same match. */
19698#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019699 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700#else
19701 col = 1;
19702#endif
19703 }
19704 str = regmatch.endp[0];
19705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706
Bram Moolenaar473de612013-06-08 18:19:48 +020019707 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709
Bram Moolenaar0d660222005-01-07 21:51:51 +000019710 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019711}
19712
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019713#ifdef FEAT_FLOAT
19714/*
19715 * "sqrt()" function
19716 */
19717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019718f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019719{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019720 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019721
19722 rettv->v_type = VAR_FLOAT;
19723 if (get_float_arg(argvars, &f) == OK)
19724 rettv->vval.v_float = sqrt(f);
19725 else
19726 rettv->vval.v_float = 0.0;
19727}
19728
19729/*
19730 * "str2float()" function
19731 */
19732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019733f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019734{
19735 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19736
19737 if (*p == '+')
19738 p = skipwhite(p + 1);
19739 (void)string2float(p, &rettv->vval.v_float);
19740 rettv->v_type = VAR_FLOAT;
19741}
19742#endif
19743
Bram Moolenaar2c932302006-03-18 21:42:09 +000019744/*
19745 * "str2nr()" function
19746 */
19747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019748f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019749{
19750 int base = 10;
19751 char_u *p;
19752 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019753 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019754
19755 if (argvars[1].v_type != VAR_UNKNOWN)
19756 {
19757 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019758 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019759 {
19760 EMSG(_(e_invarg));
19761 return;
19762 }
19763 }
19764
19765 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019766 if (*p == '+')
19767 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019768 switch (base)
19769 {
19770 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19771 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19772 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19773 default: what = 0;
19774 }
19775 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019776 rettv->vval.v_number = n;
19777}
19778
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779#ifdef HAVE_STRFTIME
19780/*
19781 * "strftime({format}[, {time}])" function
19782 */
19783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019784f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
19786 char_u result_buf[256];
19787 struct tm *curtime;
19788 time_t seconds;
19789 char_u *p;
19790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019791 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019793 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019794 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795 seconds = time(NULL);
19796 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 curtime = localtime(&seconds);
19799 /* MSVC returns NULL for an invalid value of seconds. */
19800 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019801 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 else
19803 {
19804# ifdef FEAT_MBYTE
19805 vimconv_T conv;
19806 char_u *enc;
19807
19808 conv.vc_type = CONV_NONE;
19809 enc = enc_locale();
19810 convert_setup(&conv, p_enc, enc);
19811 if (conv.vc_type != CONV_NONE)
19812 p = string_convert(&conv, p, NULL);
19813# endif
19814 if (p != NULL)
19815 (void)strftime((char *)result_buf, sizeof(result_buf),
19816 (char *)p, curtime);
19817 else
19818 result_buf[0] = NUL;
19819
19820# ifdef FEAT_MBYTE
19821 if (conv.vc_type != CONV_NONE)
19822 vim_free(p);
19823 convert_setup(&conv, enc, p_enc);
19824 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 else
19827# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829
19830# ifdef FEAT_MBYTE
19831 /* Release conversion descriptors */
19832 convert_setup(&conv, NULL, NULL);
19833 vim_free(enc);
19834# endif
19835 }
19836}
19837#endif
19838
19839/*
19840 * "stridx()" function
19841 */
19842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019843f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844{
19845 char_u buf[NUMBUFLEN];
19846 char_u *needle;
19847 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019849 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019852 needle = get_tv_string_chk(&argvars[1]);
19853 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019854 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019855 if (needle == NULL || haystack == NULL)
19856 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857
Bram Moolenaar33570922005-01-25 22:26:29 +000019858 if (argvars[2].v_type != VAR_UNKNOWN)
19859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019860 int error = FALSE;
19861
19862 start_idx = get_tv_number_chk(&argvars[2], &error);
19863 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019864 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019865 if (start_idx >= 0)
19866 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 }
19868
19869 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19870 if (pos != NULL)
19871 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872}
19873
19874/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 * "string()" function
19876 */
19877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019878f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879{
19880 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019881 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019883 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019884 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019886 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888}
19889
19890/*
19891 * "strlen()" function
19892 */
19893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019894f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019896 rettv->vval.v_number = (varnumber_T)(STRLEN(
19897 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898}
19899
19900/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019901 * "strchars()" function
19902 */
19903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019904f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019905{
19906 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019907 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019908#ifdef FEAT_MBYTE
19909 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019910 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019911#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019912
19913 if (argvars[1].v_type != VAR_UNKNOWN)
19914 skipcc = get_tv_number_chk(&argvars[1], NULL);
19915 if (skipcc < 0 || skipcc > 1)
19916 EMSG(_(e_invarg));
19917 else
19918 {
19919#ifdef FEAT_MBYTE
19920 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19921 while (*s != NUL)
19922 {
19923 func_mb_ptr2char_adv(&s);
19924 ++len;
19925 }
19926 rettv->vval.v_number = len;
19927#else
19928 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19929#endif
19930 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019931}
19932
19933/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019934 * "strdisplaywidth()" function
19935 */
19936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019937f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019938{
19939 char_u *s = get_tv_string(&argvars[0]);
19940 int col = 0;
19941
19942 if (argvars[1].v_type != VAR_UNKNOWN)
19943 col = get_tv_number(&argvars[1]);
19944
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019945 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019946}
19947
19948/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019949 * "strwidth()" function
19950 */
19951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019952f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019953{
19954 char_u *s = get_tv_string(&argvars[0]);
19955
19956 rettv->vval.v_number = (varnumber_T)(
19957#ifdef FEAT_MBYTE
19958 mb_string2cells(s, -1)
19959#else
19960 STRLEN(s)
19961#endif
19962 );
19963}
19964
19965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 * "strpart()" function
19967 */
19968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
19971 char_u *p;
19972 int n;
19973 int len;
19974 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 slen = (int)STRLEN(p);
19979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019980 n = get_tv_number_chk(&argvars[1], &error);
19981 if (error)
19982 len = 0;
19983 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 else
19986 len = slen - n; /* default len: all bytes that are available. */
19987
19988 /*
19989 * Only return the overlap between the specified part and the actual
19990 * string.
19991 */
19992 if (n < 0)
19993 {
19994 len += n;
19995 n = 0;
19996 }
19997 else if (n > slen)
19998 n = slen;
19999 if (len < 0)
20000 len = 0;
20001 else if (n + len > slen)
20002 len = slen - n;
20003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 rettv->v_type = VAR_STRING;
20005 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006}
20007
20008/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020009 * "strridx()" function
20010 */
20011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020012f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020013{
20014 char_u buf[NUMBUFLEN];
20015 char_u *needle;
20016 char_u *haystack;
20017 char_u *rest;
20018 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020019 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020021 needle = get_tv_string_chk(&argvars[1]);
20022 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023
20024 rettv->vval.v_number = -1;
20025 if (needle == NULL || haystack == NULL)
20026 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020027
20028 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020029 if (argvars[2].v_type != VAR_UNKNOWN)
20030 {
20031 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020033 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020034 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020035 }
20036 else
20037 end_idx = haystack_len;
20038
Bram Moolenaar0d660222005-01-07 21:51:51 +000020039 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020040 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020041 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020042 lastmatch = haystack + end_idx;
20043 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020044 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020045 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020046 for (rest = haystack; *rest != '\0'; ++rest)
20047 {
20048 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020049 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020050 break;
20051 lastmatch = rest;
20052 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020054
20055 if (lastmatch == NULL)
20056 rettv->vval.v_number = -1;
20057 else
20058 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20059}
20060
20061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 * "strtrans()" function
20063 */
20064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020065f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 rettv->v_type = VAR_STRING;
20068 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020072 * "submatch()" function
20073 */
20074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020076{
Bram Moolenaar41571762014-04-02 19:00:58 +020020077 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020078 int no;
20079 int retList = 0;
20080
20081 no = (int)get_tv_number_chk(&argvars[0], &error);
20082 if (error)
20083 return;
20084 error = FALSE;
20085 if (argvars[1].v_type != VAR_UNKNOWN)
20086 retList = get_tv_number_chk(&argvars[1], &error);
20087 if (error)
20088 return;
20089
20090 if (retList == 0)
20091 {
20092 rettv->v_type = VAR_STRING;
20093 rettv->vval.v_string = reg_submatch(no);
20094 }
20095 else
20096 {
20097 rettv->v_type = VAR_LIST;
20098 rettv->vval.v_list = reg_submatch_list(no);
20099 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020100}
20101
20102/*
20103 * "substitute()" function
20104 */
20105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020106f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020107{
20108 char_u patbuf[NUMBUFLEN];
20109 char_u subbuf[NUMBUFLEN];
20110 char_u flagsbuf[NUMBUFLEN];
20111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020112 char_u *str = get_tv_string_chk(&argvars[0]);
20113 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20114 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20115 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20116
Bram Moolenaar0d660222005-01-07 21:51:51 +000020117 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020118 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20119 rettv->vval.v_string = NULL;
20120 else
20121 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020122}
20123
20124/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020125 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020128f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129{
20130 int id = 0;
20131#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020132 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 long col;
20134 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020135 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020137 lnum = get_tv_lnum(argvars); /* -1 on type error */
20138 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20139 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020141 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020142 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020143 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144#endif
20145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147}
20148
20149/*
20150 * "synIDattr(id, what [, mode])" function
20151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020153f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154{
20155 char_u *p = NULL;
20156#ifdef FEAT_SYN_HL
20157 int id;
20158 char_u *what;
20159 char_u *mode;
20160 char_u modebuf[NUMBUFLEN];
20161 int modec;
20162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 id = get_tv_number(&argvars[0]);
20164 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020169 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 modec = 0; /* replace invalid with current */
20171 }
20172 else
20173 {
20174#ifdef FEAT_GUI
20175 if (gui.in_use)
20176 modec = 'g';
20177 else
20178#endif
20179 if (t_colors > 1)
20180 modec = 'c';
20181 else
20182 modec = 't';
20183 }
20184
20185
20186 switch (TOLOWER_ASC(what[0]))
20187 {
20188 case 'b':
20189 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20190 p = highlight_color(id, what, modec);
20191 else /* bold */
20192 p = highlight_has_attr(id, HL_BOLD, modec);
20193 break;
20194
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020195 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 p = highlight_color(id, what, modec);
20197 break;
20198
20199 case 'i':
20200 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20201 p = highlight_has_attr(id, HL_INVERSE, modec);
20202 else /* italic */
20203 p = highlight_has_attr(id, HL_ITALIC, modec);
20204 break;
20205
20206 case 'n': /* name */
20207 p = get_highlight_name(NULL, id - 1);
20208 break;
20209
20210 case 'r': /* reverse */
20211 p = highlight_has_attr(id, HL_INVERSE, modec);
20212 break;
20213
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020214 case 's':
20215 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20216 p = highlight_color(id, what, modec);
20217 else /* standout */
20218 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 break;
20220
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020221 case 'u':
20222 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20223 /* underline */
20224 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20225 else
20226 /* undercurl */
20227 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 break;
20229 }
20230
20231 if (p != NULL)
20232 p = vim_strsave(p);
20233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 rettv->v_type = VAR_STRING;
20235 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
20238/*
20239 * "synIDtrans(id)" function
20240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020242f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
20244 int id;
20245
20246#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248
20249 if (id > 0)
20250 id = syn_get_final_id(id);
20251 else
20252#endif
20253 id = 0;
20254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256}
20257
20258/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020259 * "synconcealed(lnum, col)" function
20260 */
20261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020262f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020263{
20264#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20265 long lnum;
20266 long col;
20267 int syntax_flags = 0;
20268 int cchar;
20269 int matchid = 0;
20270 char_u str[NUMBUFLEN];
20271#endif
20272
20273 rettv->v_type = VAR_LIST;
20274 rettv->vval.v_list = NULL;
20275
20276#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20277 lnum = get_tv_lnum(argvars); /* -1 on type error */
20278 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20279
20280 vim_memset(str, NUL, sizeof(str));
20281
20282 if (rettv_list_alloc(rettv) != FAIL)
20283 {
20284 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20285 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20286 && curwin->w_p_cole > 0)
20287 {
20288 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20289 syntax_flags = get_syntax_info(&matchid);
20290
20291 /* get the conceal character */
20292 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20293 {
20294 cchar = syn_get_sub_char();
20295 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20296 cchar = lcs_conceal;
20297 if (cchar != NUL)
20298 {
20299# ifdef FEAT_MBYTE
20300 if (has_mbyte)
20301 (*mb_char2bytes)(cchar, str);
20302 else
20303# endif
20304 str[0] = cchar;
20305 }
20306 }
20307 }
20308
20309 list_append_number(rettv->vval.v_list,
20310 (syntax_flags & HL_CONCEAL) != 0);
20311 /* -1 to auto-determine strlen */
20312 list_append_string(rettv->vval.v_list, str, -1);
20313 list_append_number(rettv->vval.v_list, matchid);
20314 }
20315#endif
20316}
20317
20318/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020319 * "synstack(lnum, col)" function
20320 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020323{
20324#ifdef FEAT_SYN_HL
20325 long lnum;
20326 long col;
20327 int i;
20328 int id;
20329#endif
20330
20331 rettv->v_type = VAR_LIST;
20332 rettv->vval.v_list = NULL;
20333
20334#ifdef FEAT_SYN_HL
20335 lnum = get_tv_lnum(argvars); /* -1 on type error */
20336 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20337
20338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020339 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020340 && rettv_list_alloc(rettv) != FAIL)
20341 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020342 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020343 for (i = 0; ; ++i)
20344 {
20345 id = syn_get_stack_item(i);
20346 if (id < 0)
20347 break;
20348 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20349 break;
20350 }
20351 }
20352#endif
20353}
20354
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356get_cmd_output_as_rettv(
20357 typval_T *argvars,
20358 typval_T *rettv,
20359 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020361 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020363 char_u *infile = NULL;
20364 char_u buf[NUMBUFLEN];
20365 int err = FALSE;
20366 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020367 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020368 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020370 rettv->v_type = VAR_STRING;
20371 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020372 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020373 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020376 {
20377 /*
20378 * Write the string to a temp file, to be used for input of the shell
20379 * command.
20380 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020381 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020382 {
20383 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020384 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020385 }
20386
20387 fd = mch_fopen((char *)infile, WRITEBIN);
20388 if (fd == NULL)
20389 {
20390 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020391 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020392 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020393 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020394 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020395 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20396 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020397 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020398 else
20399 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020400 size_t len;
20401
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020402 p = get_tv_string_buf_chk(&argvars[1], buf);
20403 if (p == NULL)
20404 {
20405 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020406 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020407 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020408 len = STRLEN(p);
20409 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020410 err = TRUE;
20411 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020412 if (fclose(fd) != 0)
20413 err = TRUE;
20414 if (err)
20415 {
20416 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020417 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020418 }
20419 }
20420
Bram Moolenaar52a72462014-08-29 15:53:52 +020020421 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20422 * echoes typeahead, that messes up the display. */
20423 if (!msg_silent)
20424 flags += SHELL_COOKED;
20425
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020426 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020428 int len;
20429 listitem_T *li;
20430 char_u *s = NULL;
20431 char_u *start;
20432 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020433 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434
Bram Moolenaar52a72462014-08-29 15:53:52 +020020435 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020436 if (res == NULL)
20437 goto errret;
20438
20439 list = list_alloc();
20440 if (list == NULL)
20441 goto errret;
20442
20443 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020445 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020446 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020448 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020449
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020450 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020451 if (s == NULL)
20452 goto errret;
20453
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020454 for (p = s; start < end; ++p, ++start)
20455 *p = *start == NUL ? NL : *start;
20456 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457
20458 li = listitem_alloc();
20459 if (li == NULL)
20460 {
20461 vim_free(s);
20462 goto errret;
20463 }
20464 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020465 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020466 li->li_tv.vval.v_string = s;
20467 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020469
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020470 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020471 rettv->v_type = VAR_LIST;
20472 rettv->vval.v_list = list;
20473 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020475 else
20476 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020477 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020478#ifdef USE_CR
20479 /* translate <CR> into <NL> */
20480 if (res != NULL)
20481 {
20482 char_u *s;
20483
20484 for (s = res; *s; ++s)
20485 {
20486 if (*s == CAR)
20487 *s = NL;
20488 }
20489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490#else
20491# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020492 /* translate <CR><NL> into <NL> */
20493 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020495 char_u *s, *d;
20496
20497 d = res;
20498 for (s = res; *s; ++s)
20499 {
20500 if (s[0] == CAR && s[1] == NL)
20501 ++s;
20502 *d++ = *s;
20503 }
20504 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506# endif
20507#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020508 rettv->vval.v_string = res;
20509 res = NULL;
20510 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020511
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020512errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020513 if (infile != NULL)
20514 {
20515 mch_remove(infile);
20516 vim_free(infile);
20517 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020518 if (res != NULL)
20519 vim_free(res);
20520 if (list != NULL)
20521 list_free(list, TRUE);
20522}
20523
20524/*
20525 * "system()" function
20526 */
20527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020528f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020529{
20530 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20531}
20532
20533/*
20534 * "systemlist()" function
20535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020538{
20539 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540}
20541
20542/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020543 * "tabpagebuflist()" function
20544 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020546f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020547{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020548#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020549 tabpage_T *tp;
20550 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020551
20552 if (argvars[0].v_type == VAR_UNKNOWN)
20553 wp = firstwin;
20554 else
20555 {
20556 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20557 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020558 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020559 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020560 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020561 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020562 for (; wp != NULL; wp = wp->w_next)
20563 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020564 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020565 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020566 }
20567#endif
20568}
20569
20570
20571/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020572 * "tabpagenr()" function
20573 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020575f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020576{
20577 int nr = 1;
20578#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020579 char_u *arg;
20580
20581 if (argvars[0].v_type != VAR_UNKNOWN)
20582 {
20583 arg = get_tv_string_chk(&argvars[0]);
20584 nr = 0;
20585 if (arg != NULL)
20586 {
20587 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020588 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020589 else
20590 EMSG2(_(e_invexpr2), arg);
20591 }
20592 }
20593 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020594 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020595#endif
20596 rettv->vval.v_number = nr;
20597}
20598
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020599
20600#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020601static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020602
20603/*
20604 * Common code for tabpagewinnr() and winnr().
20605 */
20606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020607get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020608{
20609 win_T *twin;
20610 int nr = 1;
20611 win_T *wp;
20612 char_u *arg;
20613
20614 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20615 if (argvar->v_type != VAR_UNKNOWN)
20616 {
20617 arg = get_tv_string_chk(argvar);
20618 if (arg == NULL)
20619 nr = 0; /* type error; errmsg already given */
20620 else if (STRCMP(arg, "$") == 0)
20621 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20622 else if (STRCMP(arg, "#") == 0)
20623 {
20624 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20625 if (twin == NULL)
20626 nr = 0;
20627 }
20628 else
20629 {
20630 EMSG2(_(e_invexpr2), arg);
20631 nr = 0;
20632 }
20633 }
20634
20635 if (nr > 0)
20636 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20637 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020638 {
20639 if (wp == NULL)
20640 {
20641 /* didn't find it in this tabpage */
20642 nr = 0;
20643 break;
20644 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020645 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020646 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020647 return nr;
20648}
20649#endif
20650
20651/*
20652 * "tabpagewinnr()" function
20653 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020655f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020656{
20657 int nr = 1;
20658#ifdef FEAT_WINDOWS
20659 tabpage_T *tp;
20660
20661 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20662 if (tp == NULL)
20663 nr = 0;
20664 else
20665 nr = get_winnr(tp, &argvars[1]);
20666#endif
20667 rettv->vval.v_number = nr;
20668}
20669
20670
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020671/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020672 * "tagfiles()" function
20673 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020676{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020677 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020678 tagname_T tn;
20679 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020682 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020683 fname = alloc(MAXPATHL);
20684 if (fname == NULL)
20685 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020687 for (first = TRUE; ; first = FALSE)
20688 if (get_tagfname(&tn, first, fname) == FAIL
20689 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020690 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020691 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020692 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020693}
20694
20695/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020696 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020697 */
20698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020700{
20701 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020702
20703 tag_pattern = get_tv_string(&argvars[0]);
20704
20705 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020706 if (*tag_pattern == NUL)
20707 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020708
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020709 if (rettv_list_alloc(rettv) == OK)
20710 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020711}
20712
20713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 * "tempname()" function
20715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020717f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718{
20719 static int x = 'A';
20720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020721 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020722 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723
20724 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20725 * names. Skip 'I' and 'O', they are used for shell redirection. */
20726 do
20727 {
20728 if (x == 'Z')
20729 x = '0';
20730 else if (x == '9')
20731 x = 'A';
20732 else
20733 {
20734#ifdef EBCDIC
20735 if (x == 'I')
20736 x = 'J';
20737 else if (x == 'R')
20738 x = 'S';
20739 else
20740#endif
20741 ++x;
20742 }
20743 } while (x == 'I' || x == 'O');
20744}
20745
20746/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020747 * "test(list)" function: Just checking the walls...
20748 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020750f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020751{
20752 /* Used for unit testing. Change the code below to your liking. */
20753#if 0
20754 listitem_T *li;
20755 list_T *l;
20756 char_u *bad, *good;
20757
20758 if (argvars[0].v_type != VAR_LIST)
20759 return;
20760 l = argvars[0].vval.v_list;
20761 if (l == NULL)
20762 return;
20763 li = l->lv_first;
20764 if (li == NULL)
20765 return;
20766 bad = get_tv_string(&li->li_tv);
20767 li = li->li_next;
20768 if (li == NULL)
20769 return;
20770 good = get_tv_string(&li->li_tv);
20771 rettv->vval.v_number = test_edit_score(bad, good);
20772#endif
20773}
20774
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020775#ifdef FEAT_FLOAT
20776/*
20777 * "tan()" function
20778 */
20779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020780f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020781{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020782 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020783
20784 rettv->v_type = VAR_FLOAT;
20785 if (get_float_arg(argvars, &f) == OK)
20786 rettv->vval.v_float = tan(f);
20787 else
20788 rettv->vval.v_float = 0.0;
20789}
20790
20791/*
20792 * "tanh()" function
20793 */
20794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020795f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020796{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020797 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020798
20799 rettv->v_type = VAR_FLOAT;
20800 if (get_float_arg(argvars, &f) == OK)
20801 rettv->vval.v_float = tanh(f);
20802 else
20803 rettv->vval.v_float = 0.0;
20804}
20805#endif
20806
Bram Moolenaard52d9742005-08-21 22:20:28 +000020807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808 * "tolower(string)" function
20809 */
20810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020811f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812{
20813 char_u *p;
20814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020815 p = vim_strsave(get_tv_string(&argvars[0]));
20816 rettv->v_type = VAR_STRING;
20817 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818
20819 if (p != NULL)
20820 while (*p != NUL)
20821 {
20822#ifdef FEAT_MBYTE
20823 int l;
20824
20825 if (enc_utf8)
20826 {
20827 int c, lc;
20828
20829 c = utf_ptr2char(p);
20830 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020831 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 /* TODO: reallocate string when byte count changes. */
20833 if (utf_char2len(lc) == l)
20834 utf_char2bytes(lc, p);
20835 p += l;
20836 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020837 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 p += l; /* skip multi-byte character */
20839 else
20840#endif
20841 {
20842 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20843 ++p;
20844 }
20845 }
20846}
20847
20848/*
20849 * "toupper(string)" function
20850 */
20851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020852f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020854 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020855 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856}
20857
20858/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020859 * "tr(string, fromstr, tostr)" function
20860 */
20861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020862f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020863{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020864 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020865 char_u *fromstr;
20866 char_u *tostr;
20867 char_u *p;
20868#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020869 int inlen;
20870 int fromlen;
20871 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020872 int idx;
20873 char_u *cpstr;
20874 int cplen;
20875 int first = TRUE;
20876#endif
20877 char_u buf[NUMBUFLEN];
20878 char_u buf2[NUMBUFLEN];
20879 garray_T ga;
20880
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020881 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020882 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20883 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020884
20885 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020886 rettv->v_type = VAR_STRING;
20887 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020888 if (fromstr == NULL || tostr == NULL)
20889 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020890 ga_init2(&ga, (int)sizeof(char), 80);
20891
20892#ifdef FEAT_MBYTE
20893 if (!has_mbyte)
20894#endif
20895 /* not multi-byte: fromstr and tostr must be the same length */
20896 if (STRLEN(fromstr) != STRLEN(tostr))
20897 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020898#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020900#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020901 EMSG2(_(e_invarg2), fromstr);
20902 ga_clear(&ga);
20903 return;
20904 }
20905
20906 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020907 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020908 {
20909#ifdef FEAT_MBYTE
20910 if (has_mbyte)
20911 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020912 inlen = (*mb_ptr2len)(in_str);
20913 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020914 cplen = inlen;
20915 idx = 0;
20916 for (p = fromstr; *p != NUL; p += fromlen)
20917 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020918 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020919 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020920 {
20921 for (p = tostr; *p != NUL; p += tolen)
20922 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020923 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020924 if (idx-- == 0)
20925 {
20926 cplen = tolen;
20927 cpstr = p;
20928 break;
20929 }
20930 }
20931 if (*p == NUL) /* tostr is shorter than fromstr */
20932 goto error;
20933 break;
20934 }
20935 ++idx;
20936 }
20937
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020938 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020939 {
20940 /* Check that fromstr and tostr have the same number of
20941 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020942 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020943 first = FALSE;
20944 for (p = tostr; *p != NUL; p += tolen)
20945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020946 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020947 --idx;
20948 }
20949 if (idx != 0)
20950 goto error;
20951 }
20952
Bram Moolenaarcde88542015-08-11 19:14:00 +020020953 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020954 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020955 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020956
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020957 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020958 }
20959 else
20960#endif
20961 {
20962 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020963 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020964 if (p != NULL)
20965 ga_append(&ga, tostr[p - fromstr]);
20966 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020967 ga_append(&ga, *in_str);
20968 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020969 }
20970 }
20971
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020972 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020973 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020974 ga_append(&ga, NUL);
20975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020976 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020977}
20978
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020979#ifdef FEAT_FLOAT
20980/*
20981 * "trunc({float})" function
20982 */
20983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020984f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020985{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020986 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020987
20988 rettv->v_type = VAR_FLOAT;
20989 if (get_float_arg(argvars, &f) == OK)
20990 /* trunc() is not in C90, use floor() or ceil() instead. */
20991 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20992 else
20993 rettv->vval.v_float = 0.0;
20994}
20995#endif
20996
Bram Moolenaar8299df92004-07-10 09:47:34 +000020997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 * "type(expr)" function
20999 */
21000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021001f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021003 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021004
21005 switch (argvars[0].v_type)
21006 {
21007 case VAR_NUMBER: n = 0; break;
21008 case VAR_STRING: n = 1; break;
21009 case VAR_FUNC: n = 2; break;
21010 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021011 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021012 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021013 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021014 if (argvars[0].vval.v_number == VVAL_FALSE
21015 || argvars[0].vval.v_number == VVAL_TRUE)
21016 n = 6;
21017 else
21018 n = 7;
21019 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021020 case VAR_JOB: n = 8; break;
21021 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021022 case VAR_UNKNOWN:
21023 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21024 n = -1;
21025 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021026 }
21027 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028}
21029
21030/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021031 * "undofile(name)" function
21032 */
21033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021034f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021035{
21036 rettv->v_type = VAR_STRING;
21037#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021038 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021039 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021040
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021041 if (*fname == NUL)
21042 {
21043 /* If there is no file name there will be no undo file. */
21044 rettv->vval.v_string = NULL;
21045 }
21046 else
21047 {
21048 char_u *ffname = FullName_save(fname, FALSE);
21049
21050 if (ffname != NULL)
21051 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21052 vim_free(ffname);
21053 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021054 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021055#else
21056 rettv->vval.v_string = NULL;
21057#endif
21058}
21059
21060/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021061 * "undotree()" function
21062 */
21063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021064f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021065{
21066 if (rettv_dict_alloc(rettv) == OK)
21067 {
21068 dict_T *dict = rettv->vval.v_dict;
21069 list_T *list;
21070
Bram Moolenaar730cde92010-06-27 05:18:54 +020021071 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021072 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021073 dict_add_nr_str(dict, "save_last",
21074 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021075 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21076 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021077 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021078
21079 list = list_alloc();
21080 if (list != NULL)
21081 {
21082 u_eval_tree(curbuf->b_u_oldhead, list);
21083 dict_add_list(dict, "entries", list);
21084 }
21085 }
21086}
21087
21088/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021089 * "values(dict)" function
21090 */
21091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021092f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021093{
21094 dict_list(argvars, rettv, 1);
21095}
21096
21097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 * "virtcol(string)" function
21099 */
21100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102{
21103 colnr_T vcol = 0;
21104 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021105 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021107 fp = var2fpos(&argvars[0], FALSE, &fnum);
21108 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21109 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 {
21111 getvvcol(curwin, fp, NULL, NULL, &vcol);
21112 ++vcol;
21113 }
21114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021115 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116}
21117
21118/*
21119 * "visualmode()" function
21120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021122f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 char_u str[2];
21125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021126 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 str[0] = curbuf->b_visual_mode_eval;
21128 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021129 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130
21131 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021132 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134}
21135
21136/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021137 * "wildmenumode()" function
21138 */
21139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021140f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021141{
21142#ifdef FEAT_WILDMENU
21143 if (wild_menu_showing)
21144 rettv->vval.v_number = 1;
21145#endif
21146}
21147
21148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 * "winbufnr(nr)" function
21150 */
21151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021152f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153{
21154 win_T *wp;
21155
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021156 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021160 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161}
21162
21163/*
21164 * "wincol()" function
21165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021167f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168{
21169 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021170 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171}
21172
21173/*
21174 * "winheight(nr)" function
21175 */
21176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178{
21179 win_T *wp;
21180
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021181 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021184 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186}
21187
21188/*
21189 * "winline()" function
21190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021192f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193{
21194 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196}
21197
21198/*
21199 * "winnr()" function
21200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203{
21204 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021205
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021207 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021209 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210}
21211
21212/*
21213 * "winrestcmd()" function
21214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021216f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217{
21218#ifdef FEAT_WINDOWS
21219 win_T *wp;
21220 int winnr = 1;
21221 garray_T ga;
21222 char_u buf[50];
21223
21224 ga_init2(&ga, (int)sizeof(char), 70);
21225 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21226 {
21227 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21228 ga_concat(&ga, buf);
21229# ifdef FEAT_VERTSPLIT
21230 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21231 ga_concat(&ga, buf);
21232# endif
21233 ++winnr;
21234 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021235 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021237 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021241 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242}
21243
21244/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021245 * "winrestview()" function
21246 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021249{
21250 dict_T *dict;
21251
21252 if (argvars[0].v_type != VAR_DICT
21253 || (dict = argvars[0].vval.v_dict) == NULL)
21254 EMSG(_(e_invarg));
21255 else
21256 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021257 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21258 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21259 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21260 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021261#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021262 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21263 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021264#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021265 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21266 {
21267 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21268 curwin->w_set_curswant = FALSE;
21269 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021270
Bram Moolenaar82c25852014-05-28 16:47:16 +020021271 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21272 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021273#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021274 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21275 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021276#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021277 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21278 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21279 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21280 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021281
21282 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021283 win_new_height(curwin, curwin->w_height);
21284# ifdef FEAT_VERTSPLIT
21285 win_new_width(curwin, W_WIDTH(curwin));
21286# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021287 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021288
Bram Moolenaarb851a962014-10-31 15:45:52 +010021289 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021290 curwin->w_topline = 1;
21291 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21292 curwin->w_topline = curbuf->b_ml.ml_line_count;
21293#ifdef FEAT_DIFF
21294 check_topfill(curwin, TRUE);
21295#endif
21296 }
21297}
21298
21299/*
21300 * "winsaveview()" function
21301 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021303f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021304{
21305 dict_T *dict;
21306
Bram Moolenaara800b422010-06-27 01:15:55 +020021307 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021308 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021309 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021310
21311 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21312 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21313#ifdef FEAT_VIRTUALEDIT
21314 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21315#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021316 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021317 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21318
21319 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21320#ifdef FEAT_DIFF
21321 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21322#endif
21323 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21324 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21325}
21326
21327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 * "winwidth(nr)" function
21329 */
21330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021331f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332{
21333 win_T *wp;
21334
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021335 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021337 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 else
21339#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021340 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021342 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343#endif
21344}
21345
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021347 * "wordcount()" function
21348 */
21349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021350f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021351{
21352 if (rettv_dict_alloc(rettv) == FAIL)
21353 return;
21354 cursor_pos_info(rettv->vval.v_dict);
21355}
21356
21357/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021358 * Write list of strings to file
21359 */
21360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021361write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021362{
21363 listitem_T *li;
21364 int c;
21365 int ret = OK;
21366 char_u *s;
21367
21368 for (li = list->lv_first; li != NULL; li = li->li_next)
21369 {
21370 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21371 {
21372 if (*s == '\n')
21373 c = putc(NUL, fd);
21374 else
21375 c = putc(*s, fd);
21376 if (c == EOF)
21377 {
21378 ret = FAIL;
21379 break;
21380 }
21381 }
21382 if (!binary || li->li_next != NULL)
21383 if (putc('\n', fd) == EOF)
21384 {
21385 ret = FAIL;
21386 break;
21387 }
21388 if (ret == FAIL)
21389 {
21390 EMSG(_(e_write));
21391 break;
21392 }
21393 }
21394 return ret;
21395}
21396
21397/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021398 * "writefile()" function
21399 */
21400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021401f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021402{
21403 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021404 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021405 char_u *fname;
21406 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021407 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021408
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021409 if (check_restricted() || check_secure())
21410 return;
21411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021412 if (argvars[0].v_type != VAR_LIST)
21413 {
21414 EMSG2(_(e_listarg), "writefile()");
21415 return;
21416 }
21417 if (argvars[0].vval.v_list == NULL)
21418 return;
21419
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021420 if (argvars[2].v_type != VAR_UNKNOWN)
21421 {
21422 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21423 binary = TRUE;
21424 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21425 append = TRUE;
21426 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021427
21428 /* Always open the file in binary mode, library functions have a mind of
21429 * their own about CR-LF conversion. */
21430 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021431 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21432 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021433 {
21434 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21435 ret = -1;
21436 }
21437 else
21438 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021439 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21440 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021441 fclose(fd);
21442 }
21443
21444 rettv->vval.v_number = ret;
21445}
21446
21447/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021448 * "xor(expr, expr)" function
21449 */
21450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021451f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021452{
21453 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21454 ^ get_tv_number_chk(&argvars[1], NULL);
21455}
21456
21457
21458/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021460 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 */
21462 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021463var2fpos(
21464 typval_T *varp,
21465 int dollar_lnum, /* TRUE when $ is last line */
21466 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021468 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021470 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471
Bram Moolenaara5525202006-03-02 22:52:09 +000021472 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021473 if (varp->v_type == VAR_LIST)
21474 {
21475 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021476 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021477 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021478 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021479
21480 l = varp->vval.v_list;
21481 if (l == NULL)
21482 return NULL;
21483
21484 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021485 pos.lnum = list_find_nr(l, 0L, &error);
21486 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021487 return NULL; /* invalid line number */
21488
21489 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021490 pos.col = list_find_nr(l, 1L, &error);
21491 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021492 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021493 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021494
21495 /* We accept "$" for the column number: last column. */
21496 li = list_find(l, 1L);
21497 if (li != NULL && li->li_tv.v_type == VAR_STRING
21498 && li->li_tv.vval.v_string != NULL
21499 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21500 pos.col = len + 1;
21501
Bram Moolenaara5525202006-03-02 22:52:09 +000021502 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021503 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021504 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021505 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021506
Bram Moolenaara5525202006-03-02 22:52:09 +000021507#ifdef FEAT_VIRTUALEDIT
21508 /* Get the virtual offset. Defaults to zero. */
21509 pos.coladd = list_find_nr(l, 2L, &error);
21510 if (error)
21511 pos.coladd = 0;
21512#endif
21513
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021514 return &pos;
21515 }
21516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021517 name = get_tv_string_chk(varp);
21518 if (name == NULL)
21519 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021520 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021522 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21523 {
21524 if (VIsual_active)
21525 return &VIsual;
21526 return &curwin->w_cursor;
21527 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021528 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021530 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21532 return NULL;
21533 return pp;
21534 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021535
21536#ifdef FEAT_VIRTUALEDIT
21537 pos.coladd = 0;
21538#endif
21539
Bram Moolenaar477933c2007-07-17 14:32:23 +000021540 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021541 {
21542 pos.col = 0;
21543 if (name[1] == '0') /* "w0": first visible line */
21544 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021545 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021546 pos.lnum = curwin->w_topline;
21547 return &pos;
21548 }
21549 else if (name[1] == '$') /* "w$": last visible line */
21550 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021551 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021552 pos.lnum = curwin->w_botline - 1;
21553 return &pos;
21554 }
21555 }
21556 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021558 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 {
21560 pos.lnum = curbuf->b_ml.ml_line_count;
21561 pos.col = 0;
21562 }
21563 else
21564 {
21565 pos.lnum = curwin->w_cursor.lnum;
21566 pos.col = (colnr_T)STRLEN(ml_get_curline());
21567 }
21568 return &pos;
21569 }
21570 return NULL;
21571}
21572
21573/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021574 * Convert list in "arg" into a position and optional file number.
21575 * When "fnump" is NULL there is no file number, only 3 items.
21576 * Note that the column is passed on as-is, the caller may want to decrement
21577 * it to use 1 for the first column.
21578 * Return FAIL when conversion is not possible, doesn't check the position for
21579 * validity.
21580 */
21581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021582list2fpos(
21583 typval_T *arg,
21584 pos_T *posp,
21585 int *fnump,
21586 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021587{
21588 list_T *l = arg->vval.v_list;
21589 long i = 0;
21590 long n;
21591
Bram Moolenaar493c1782014-05-28 14:34:46 +020021592 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21593 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021594 if (arg->v_type != VAR_LIST
21595 || l == NULL
21596 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021597 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021598 return FAIL;
21599
21600 if (fnump != NULL)
21601 {
21602 n = list_find_nr(l, i++, NULL); /* fnum */
21603 if (n < 0)
21604 return FAIL;
21605 if (n == 0)
21606 n = curbuf->b_fnum; /* current buffer */
21607 *fnump = n;
21608 }
21609
21610 n = list_find_nr(l, i++, NULL); /* lnum */
21611 if (n < 0)
21612 return FAIL;
21613 posp->lnum = n;
21614
21615 n = list_find_nr(l, i++, NULL); /* col */
21616 if (n < 0)
21617 return FAIL;
21618 posp->col = n;
21619
21620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021621 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021622 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021623 posp->coladd = 0;
21624 else
21625 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021626#endif
21627
Bram Moolenaar493c1782014-05-28 14:34:46 +020021628 if (curswantp != NULL)
21629 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21630
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021631 return OK;
21632}
21633
21634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 * Get the length of an environment variable name.
21636 * Advance "arg" to the first character after the name.
21637 * Return 0 for error.
21638 */
21639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021640get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641{
21642 char_u *p;
21643 int len;
21644
21645 for (p = *arg; vim_isIDc(*p); ++p)
21646 ;
21647 if (p == *arg) /* no name found */
21648 return 0;
21649
21650 len = (int)(p - *arg);
21651 *arg = p;
21652 return len;
21653}
21654
21655/*
21656 * Get the length of the name of a function or internal variable.
21657 * "arg" is advanced to the first non-white character after the name.
21658 * Return 0 if something is wrong.
21659 */
21660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021661get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662{
21663 char_u *p;
21664 int len;
21665
21666 /* Find the end of the name. */
21667 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021668 {
21669 if (*p == ':')
21670 {
21671 /* "s:" is start of "s:var", but "n:" is not and can be used in
21672 * slice "[n:]". Also "xx:" is not a namespace. */
21673 len = (int)(p - *arg);
21674 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21675 || len > 1)
21676 break;
21677 }
21678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 if (p == *arg) /* no name found */
21680 return 0;
21681
21682 len = (int)(p - *arg);
21683 *arg = skipwhite(p);
21684
21685 return len;
21686}
21687
21688/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021689 * Get the length of the name of a variable or function.
21690 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021692 * Return -1 if curly braces expansion failed.
21693 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 * If the name contains 'magic' {}'s, expand them and return the
21695 * expanded name in an allocated string via 'alias' - caller must free.
21696 */
21697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021698get_name_len(
21699 char_u **arg,
21700 char_u **alias,
21701 int evaluate,
21702 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703{
21704 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 char_u *p;
21706 char_u *expr_start;
21707 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708
21709 *alias = NULL; /* default to no alias */
21710
21711 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21712 && (*arg)[2] == (int)KE_SNR)
21713 {
21714 /* hard coded <SNR>, already translated */
21715 *arg += 3;
21716 return get_id_len(arg) + 3;
21717 }
21718 len = eval_fname_script(*arg);
21719 if (len > 0)
21720 {
21721 /* literal "<SID>", "s:" or "<SNR>" */
21722 *arg += len;
21723 }
21724
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021726 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021728 p = find_name_end(*arg, &expr_start, &expr_end,
21729 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 if (expr_start != NULL)
21731 {
21732 char_u *temp_string;
21733
21734 if (!evaluate)
21735 {
21736 len += (int)(p - *arg);
21737 *arg = skipwhite(p);
21738 return len;
21739 }
21740
21741 /*
21742 * Include any <SID> etc in the expanded string:
21743 * Thus the -len here.
21744 */
21745 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21746 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021747 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 *alias = temp_string;
21749 *arg = skipwhite(p);
21750 return (int)STRLEN(temp_string);
21751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752
21753 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021754 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 EMSG2(_(e_invexpr2), *arg);
21756
21757 return len;
21758}
21759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021760/*
21761 * Find the end of a variable or function name, taking care of magic braces.
21762 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21763 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021764 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021765 * Return a pointer to just after the name. Equal to "arg" if there is no
21766 * valid name.
21767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021769find_name_end(
21770 char_u *arg,
21771 char_u **expr_start,
21772 char_u **expr_end,
21773 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021775 int mb_nest = 0;
21776 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021778 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021780 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021782 *expr_start = NULL;
21783 *expr_end = NULL;
21784 }
21785
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021786 /* Quick check for valid starting character. */
21787 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21788 return arg;
21789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021790 for (p = arg; *p != NUL
21791 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021792 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021793 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021794 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021795 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021796 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021797 if (*p == '\'')
21798 {
21799 /* skip over 'string' to avoid counting [ and ] inside it. */
21800 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21801 ;
21802 if (*p == NUL)
21803 break;
21804 }
21805 else if (*p == '"')
21806 {
21807 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21808 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21809 if (*p == '\\' && p[1] != NUL)
21810 ++p;
21811 if (*p == NUL)
21812 break;
21813 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021814 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21815 {
21816 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021817 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021818 len = (int)(p - arg);
21819 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021820 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021821 break;
21822 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021823
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021824 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021826 if (*p == '[')
21827 ++br_nest;
21828 else if (*p == ']')
21829 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021832 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021834 if (*p == '{')
21835 {
21836 mb_nest++;
21837 if (expr_start != NULL && *expr_start == NULL)
21838 *expr_start = p;
21839 }
21840 else if (*p == '}')
21841 {
21842 mb_nest--;
21843 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21844 *expr_end = p;
21845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 }
21848
21849 return p;
21850}
21851
21852/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021853 * Expands out the 'magic' {}'s in a variable/function name.
21854 * Note that this can call itself recursively, to deal with
21855 * constructs like foo{bar}{baz}{bam}
21856 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21857 * "in_start" ^
21858 * "expr_start" ^
21859 * "expr_end" ^
21860 * "in_end" ^
21861 *
21862 * Returns a new allocated string, which the caller must free.
21863 * Returns NULL for failure.
21864 */
21865 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021866make_expanded_name(
21867 char_u *in_start,
21868 char_u *expr_start,
21869 char_u *expr_end,
21870 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021871{
21872 char_u c1;
21873 char_u *retval = NULL;
21874 char_u *temp_result;
21875 char_u *nextcmd = NULL;
21876
21877 if (expr_end == NULL || in_end == NULL)
21878 return NULL;
21879 *expr_start = NUL;
21880 *expr_end = NUL;
21881 c1 = *in_end;
21882 *in_end = NUL;
21883
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021884 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021885 if (temp_result != NULL && nextcmd == NULL)
21886 {
21887 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21888 + (in_end - expr_end) + 1));
21889 if (retval != NULL)
21890 {
21891 STRCPY(retval, in_start);
21892 STRCAT(retval, temp_result);
21893 STRCAT(retval, expr_end + 1);
21894 }
21895 }
21896 vim_free(temp_result);
21897
21898 *in_end = c1; /* put char back for error messages */
21899 *expr_start = '{';
21900 *expr_end = '}';
21901
21902 if (retval != NULL)
21903 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021904 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021905 if (expr_start != NULL)
21906 {
21907 /* Further expansion! */
21908 temp_result = make_expanded_name(retval, expr_start,
21909 expr_end, temp_result);
21910 vim_free(retval);
21911 retval = temp_result;
21912 }
21913 }
21914
21915 return retval;
21916}
21917
21918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021920 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 */
21922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021923eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021925 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21926}
21927
21928/*
21929 * Return TRUE if character "c" can be used as the first character in a
21930 * variable or function name (excluding '{' and '}').
21931 */
21932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021933eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021934{
21935 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936}
21937
21938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 * Set number v: variable to "val".
21940 */
21941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021942set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021944 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945}
21946
21947/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021948 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949 */
21950 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954}
21955
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021956/*
21957 * Get string v: variable value. Uses a static buffer, can only be used once.
21958 */
21959 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021960get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021961{
21962 return get_tv_string(&vimvars[idx].vv_tv);
21963}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021964
Bram Moolenaar071d4272004-06-13 20:20:40 +000021965/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021966 * Get List v: variable value. Caller must take care of reference count when
21967 * needed.
21968 */
21969 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021970get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021971{
21972 return vimvars[idx].vv_list;
21973}
21974
21975/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021976 * Set v:char to character "c".
21977 */
21978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021979set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021980{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021981 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021982
21983#ifdef FEAT_MBYTE
21984 if (has_mbyte)
21985 buf[(*mb_char2bytes)(c, buf)] = NUL;
21986 else
21987#endif
21988 {
21989 buf[0] = c;
21990 buf[1] = NUL;
21991 }
21992 set_vim_var_string(VV_CHAR, buf, -1);
21993}
21994
21995/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021996 * Set v:count to "count" and v:count1 to "count1".
21997 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 */
21999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022000set_vcount(
22001 long count,
22002 long count1,
22003 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022005 if (set_prevcount)
22006 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022007 vimvars[VV_COUNT].vv_nr = count;
22008 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009}
22010
22011/*
22012 * Set string v: variable to a copy of "val".
22013 */
22014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022015set_vim_var_string(
22016 int idx,
22017 char_u *val,
22018 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019{
Bram Moolenaara542c682016-01-31 16:28:04 +010022020 clear_tv(&vimvars[idx].vv_di.di_tv);
22021 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022023 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022025 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022027 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028}
22029
22030/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022031 * Set List v: variable to "val".
22032 */
22033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022034set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022035{
Bram Moolenaara542c682016-01-31 16:28:04 +010022036 clear_tv(&vimvars[idx].vv_di.di_tv);
22037 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022038 vimvars[idx].vv_list = val;
22039 if (val != NULL)
22040 ++val->lv_refcount;
22041}
22042
22043/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022044 * Set Dictionary v: variable to "val".
22045 */
22046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022047set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022048{
22049 int todo;
22050 hashitem_T *hi;
22051
Bram Moolenaara542c682016-01-31 16:28:04 +010022052 clear_tv(&vimvars[idx].vv_di.di_tv);
22053 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022054 vimvars[idx].vv_dict = val;
22055 if (val != NULL)
22056 {
22057 ++val->dv_refcount;
22058
22059 /* Set readonly */
22060 todo = (int)val->dv_hashtab.ht_used;
22061 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22062 {
22063 if (HASHITEM_EMPTY(hi))
22064 continue;
22065 --todo;
22066 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22067 }
22068 }
22069}
22070
22071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 * Set v:register if needed.
22073 */
22074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022075set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076{
22077 char_u regname;
22078
22079 if (c == 0 || c == ' ')
22080 regname = '"';
22081 else
22082 regname = c;
22083 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022084 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 set_vim_var_string(VV_REG, &regname, 1);
22086}
22087
22088/*
22089 * Get or set v:exception. If "oldval" == NULL, return the current value.
22090 * Otherwise, restore the value to "oldval" and return NULL.
22091 * Must always be called in pairs to save and restore v:exception! Does not
22092 * take care of memory allocations.
22093 */
22094 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096{
22097 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022098 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099
Bram Moolenaare9a41262005-01-15 22:18:47 +000022100 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 return NULL;
22102}
22103
22104/*
22105 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22106 * Otherwise, restore the value to "oldval" and return NULL.
22107 * Must always be called in pairs to save and restore v:throwpoint! Does not
22108 * take care of memory allocations.
22109 */
22110 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022111v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112{
22113 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022114 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115
Bram Moolenaare9a41262005-01-15 22:18:47 +000022116 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117 return NULL;
22118}
22119
22120#if defined(FEAT_AUTOCMD) || defined(PROTO)
22121/*
22122 * Set v:cmdarg.
22123 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22124 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22125 * Must always be called in pairs!
22126 */
22127 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022128set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129{
22130 char_u *oldval;
22131 char_u *newval;
22132 unsigned len;
22133
Bram Moolenaare9a41262005-01-15 22:18:47 +000022134 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022135 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022137 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022138 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022139 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 }
22141
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022142 if (eap->force_bin == FORCE_BIN)
22143 len = 6;
22144 else if (eap->force_bin == FORCE_NOBIN)
22145 len = 8;
22146 else
22147 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022148
22149 if (eap->read_edit)
22150 len += 7;
22151
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022152 if (eap->force_ff != 0)
22153 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22154# ifdef FEAT_MBYTE
22155 if (eap->force_enc != 0)
22156 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022157 if (eap->bad_char != 0)
22158 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022159# endif
22160
22161 newval = alloc(len + 1);
22162 if (newval == NULL)
22163 return NULL;
22164
22165 if (eap->force_bin == FORCE_BIN)
22166 sprintf((char *)newval, " ++bin");
22167 else if (eap->force_bin == FORCE_NOBIN)
22168 sprintf((char *)newval, " ++nobin");
22169 else
22170 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022171
22172 if (eap->read_edit)
22173 STRCAT(newval, " ++edit");
22174
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022175 if (eap->force_ff != 0)
22176 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22177 eap->cmd + eap->force_ff);
22178# ifdef FEAT_MBYTE
22179 if (eap->force_enc != 0)
22180 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22181 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022182 if (eap->bad_char == BAD_KEEP)
22183 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22184 else if (eap->bad_char == BAD_DROP)
22185 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22186 else if (eap->bad_char != 0)
22187 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022188# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022189 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022190 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191}
22192#endif
22193
22194/*
22195 * Get the value of internal variable "name".
22196 * Return OK or FAIL.
22197 */
22198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022199get_var_tv(
22200 char_u *name,
22201 int len, /* length of "name" */
22202 typval_T *rettv, /* NULL when only checking existence */
22203 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22204 int verbose, /* may give error message */
22205 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206{
22207 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022208 typval_T *tv = NULL;
22209 typval_T atv;
22210 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212
22213 /* truncate the name, so that we can use strcmp() */
22214 cc = name[len];
22215 name[len] = NUL;
22216
22217 /*
22218 * Check for "b:changedtick".
22219 */
22220 if (STRCMP(name, "b:changedtick") == 0)
22221 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022222 atv.v_type = VAR_NUMBER;
22223 atv.vval.v_number = curbuf->b_changedtick;
22224 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 }
22226
22227 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 * Check for user-defined variables.
22229 */
22230 else
22231 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022232 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022236 if (dip != NULL)
22237 *dip = v;
22238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 }
22240
Bram Moolenaare9a41262005-01-15 22:18:47 +000022241 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022243 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 ret = FAIL;
22246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022247 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022248 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249
22250 name[len] = cc;
22251
22252 return ret;
22253}
22254
22255/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022256 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22257 * Also handle function call with Funcref variable: func(expr)
22258 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22259 */
22260 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022261handle_subscript(
22262 char_u **arg,
22263 typval_T *rettv,
22264 int evaluate, /* do more than finding the end */
22265 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022266{
22267 int ret = OK;
22268 dict_T *selfdict = NULL;
22269 char_u *s;
22270 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022271 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022272
22273 while (ret == OK
22274 && (**arg == '['
22275 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022276 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022277 && !vim_iswhite(*(*arg - 1)))
22278 {
22279 if (**arg == '(')
22280 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022281 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022282 if (evaluate)
22283 {
22284 functv = *rettv;
22285 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022286
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022287 /* Invoke the function. Recursive! */
22288 s = functv.vval.v_string;
22289 }
22290 else
22291 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022292 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022293 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22294 &len, evaluate, selfdict);
22295
22296 /* Clear the funcref afterwards, so that deleting it while
22297 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022298 if (evaluate)
22299 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022300
22301 /* Stop the expression evaluation when immediately aborting on
22302 * error, or when an interrupt occurred or an exception was thrown
22303 * but not caught. */
22304 if (aborting())
22305 {
22306 if (ret == OK)
22307 clear_tv(rettv);
22308 ret = FAIL;
22309 }
22310 dict_unref(selfdict);
22311 selfdict = NULL;
22312 }
22313 else /* **arg == '[' || **arg == '.' */
22314 {
22315 dict_unref(selfdict);
22316 if (rettv->v_type == VAR_DICT)
22317 {
22318 selfdict = rettv->vval.v_dict;
22319 if (selfdict != NULL)
22320 ++selfdict->dv_refcount;
22321 }
22322 else
22323 selfdict = NULL;
22324 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22325 {
22326 clear_tv(rettv);
22327 ret = FAIL;
22328 }
22329 }
22330 }
22331 dict_unref(selfdict);
22332 return ret;
22333}
22334
22335/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022336 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022337 * value).
22338 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022339 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022340alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022341{
Bram Moolenaar33570922005-01-25 22:26:29 +000022342 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022343}
22344
22345/*
22346 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 * The string "s" must have been allocated, it is consumed.
22348 * Return NULL for out of memory, the variable otherwise.
22349 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022351alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352{
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022355 rettv = alloc_tv();
22356 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022358 rettv->v_type = VAR_STRING;
22359 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 }
22361 else
22362 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022363 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364}
22365
22366/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022367 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022370free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371{
22372 if (varp != NULL)
22373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022374 switch (varp->v_type)
22375 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022376 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022377 func_unref(varp->vval.v_string);
22378 /*FALLTHROUGH*/
22379 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022380 vim_free(varp->vval.v_string);
22381 break;
22382 case VAR_LIST:
22383 list_unref(varp->vval.v_list);
22384 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 case VAR_DICT:
22386 dict_unref(varp->vval.v_dict);
22387 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022388 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022389#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022390 job_unref(varp->vval.v_job);
22391 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022392#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022393 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022394#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022395 channel_unref(varp->vval.v_channel);
22396 break;
22397#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022398 case VAR_NUMBER:
22399 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022400 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022401 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022402 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 vim_free(varp);
22405 }
22406}
22407
22408/*
22409 * Free the memory for a variable value and set the value to NULL or 0.
22410 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022412clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413{
22414 if (varp != NULL)
22415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022416 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022418 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022419 func_unref(varp->vval.v_string);
22420 /*FALLTHROUGH*/
22421 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422 vim_free(varp->vval.v_string);
22423 varp->vval.v_string = NULL;
22424 break;
22425 case VAR_LIST:
22426 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022427 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022428 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022429 case VAR_DICT:
22430 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022431 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022432 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022434 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022435 varp->vval.v_number = 0;
22436 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022437 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022438#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022439 varp->vval.v_float = 0.0;
22440 break;
22441#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022442 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022443#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022444 job_unref(varp->vval.v_job);
22445 varp->vval.v_job = NULL;
22446#endif
22447 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022448 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022449#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022450 channel_unref(varp->vval.v_channel);
22451 varp->vval.v_channel = NULL;
22452#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022453 case VAR_UNKNOWN:
22454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022456 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 }
22458}
22459
22460/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 * Set the value of a variable to NULL without freeing items.
22462 */
22463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022464init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022465{
22466 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022468}
22469
22470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 * Get the number value of a variable.
22472 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022473 * For incompatible types, return 0.
22474 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22475 * caller of incompatible types: it sets *denote to TRUE if "denote"
22476 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 */
22478 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022481 int error = FALSE;
22482
22483 return get_tv_number_chk(varp, &error); /* return 0L on error */
22484}
22485
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022486 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022487get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022488{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022489 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022491 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022493 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022494 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022495 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022496#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022497 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022498 break;
22499#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022501 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 break;
22503 case VAR_STRING:
22504 if (varp->vval.v_string != NULL)
22505 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022506 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022507 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022508 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022509 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022510 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022511 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022512 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022513 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022514 case VAR_SPECIAL:
22515 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22516 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022517 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022519 EMSG(_("E910: Using a Job as a Number"));
22520 break;
22521#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022522 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022524 EMSG(_("E913: Using a Channel as a Number"));
22525 break;
22526#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022527 case VAR_UNKNOWN:
22528 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022531 if (denote == NULL) /* useful for values that must be unsigned */
22532 n = -1;
22533 else
22534 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022535 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536}
22537
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022538#ifdef FEAT_FLOAT
22539 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022540get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022541{
22542 switch (varp->v_type)
22543 {
22544 case VAR_NUMBER:
22545 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022546 case VAR_FLOAT:
22547 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022548 case VAR_FUNC:
22549 EMSG(_("E891: Using a Funcref as a Float"));
22550 break;
22551 case VAR_STRING:
22552 EMSG(_("E892: Using a String as a Float"));
22553 break;
22554 case VAR_LIST:
22555 EMSG(_("E893: Using a List as a Float"));
22556 break;
22557 case VAR_DICT:
22558 EMSG(_("E894: Using a Dictionary as a Float"));
22559 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022560 case VAR_SPECIAL:
22561 EMSG(_("E907: Using a special value as a Float"));
22562 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022563 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022564# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022565 EMSG(_("E911: Using a Job as a Float"));
22566 break;
22567# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022568 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022569# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022570 EMSG(_("E914: Using a Channel as a Float"));
22571 break;
22572# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022573 case VAR_UNKNOWN:
22574 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022575 break;
22576 }
22577 return 0;
22578}
22579#endif
22580
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022582 * Get the lnum from the first argument.
22583 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022584 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 */
22586 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022587get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588{
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 linenr_T lnum;
22591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022592 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 if (lnum == 0) /* no valid number, try using line() */
22594 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022595 rettv.v_type = VAR_NUMBER;
22596 f_line(argvars, &rettv);
22597 lnum = rettv.vval.v_number;
22598 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 }
22600 return lnum;
22601}
22602
22603/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022604 * Get the lnum from the first argument.
22605 * Also accepts "$", then "buf" is used.
22606 * Returns 0 on error.
22607 */
22608 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022609get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022610{
22611 if (argvars[0].v_type == VAR_STRING
22612 && argvars[0].vval.v_string != NULL
22613 && argvars[0].vval.v_string[0] == '$'
22614 && buf != NULL)
22615 return buf->b_ml.ml_line_count;
22616 return get_tv_number_chk(&argvars[0], NULL);
22617}
22618
22619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 * Get the string value of a variable.
22621 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022622 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22623 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 * If the String variable has never been set, return an empty string.
22625 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022626 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22627 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022628 */
22629 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022630get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631{
22632 static char_u mybuf[NUMBUFLEN];
22633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022634 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635}
22636
22637 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022640 char_u *res = get_tv_string_buf_chk(varp, buf);
22641
22642 return res != NULL ? res : (char_u *)"";
22643}
22644
Bram Moolenaar7d647822014-04-05 21:28:56 +020022645/*
22646 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22647 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022649get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022650{
22651 static char_u mybuf[NUMBUFLEN];
22652
22653 return get_tv_string_buf_chk(varp, mybuf);
22654}
22655
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022656 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022658{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022659 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 case VAR_NUMBER:
22662 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22663 return buf;
22664 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 break;
22667 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022668 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022669 break;
22670 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022672 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022673 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022674#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022675 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022676 break;
22677#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022678 case VAR_STRING:
22679 if (varp->vval.v_string != NULL)
22680 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022681 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022682 case VAR_SPECIAL:
22683 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22684 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022685 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022687 {
22688 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022689 char *status;
22690
22691 if (job == NULL)
22692 return (char_u *)"no process";
22693 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022694 : job->jv_status == JOB_ENDED ? "dead"
22695 : "run";
22696# ifdef UNIX
22697 vim_snprintf((char *)buf, NUMBUFLEN,
22698 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022699# elif defined(WIN32)
22700 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022701 "process %ld %s",
22702 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022703 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022704# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022705 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022706 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22707# endif
22708 return buf;
22709 }
22710#endif
22711 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022712 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022714 {
22715 channel_T *channel = varp->vval.v_channel;
22716 char *status = channel_status(channel);
22717
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022718 if (channel == NULL)
22719 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22720 else
22721 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022722 "channel %d %s", channel->ch_id, status);
22723 return buf;
22724 }
22725#endif
22726 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022727 case VAR_UNKNOWN:
22728 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022731 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732}
22733
22734/*
22735 * Find variable "name" in the list of variables.
22736 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022737 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022738 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 ht = find_var_ht(name, &varname);
22748 if (htp != NULL)
22749 *htp = ht;
22750 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022752 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753}
22754
22755/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022756 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022760find_var_in_ht(
22761 hashtab_T *ht,
22762 int htname,
22763 char_u *varname,
22764 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022765{
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 hashitem_T *hi;
22767
22768 if (*varname == NUL)
22769 {
22770 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022771 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022773 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 case 'g': return &globvars_var;
22775 case 'v': return &vimvars_var;
22776 case 'b': return &curbuf->b_bufvar;
22777 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022778#ifdef FEAT_WINDOWS
22779 case 't': return &curtab->tp_winvar;
22780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022781 case 'l': return current_funccal == NULL
22782 ? NULL : &current_funccal->l_vars_var;
22783 case 'a': return current_funccal == NULL
22784 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 }
22786 return NULL;
22787 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022788
22789 hi = hash_find(ht, varname);
22790 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 {
22792 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022793 * worked find the variable again. Don't auto-load a script if it was
22794 * loaded already, otherwise it would be loaded every time when
22795 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022796 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022797 {
22798 /* Note: script_autoload() may make "hi" invalid. It must either
22799 * be obtained again or not used. */
22800 if (!script_autoload(varname, FALSE) || aborting())
22801 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022802 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022803 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022804 if (HASHITEM_EMPTY(hi))
22805 return NULL;
22806 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022808}
22809
22810/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022811 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022812 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022813 * Set "varname" to the start of name without ':'.
22814 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022815 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022816find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022818 hashitem_T *hi;
22819
Bram Moolenaar73627d02015-08-11 15:46:09 +020022820 if (name[0] == NUL)
22821 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 if (name[1] != ':')
22823 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022824 /* The name must not start with a colon or #. */
22825 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 return NULL;
22827 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022828
22829 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022830 hi = hash_find(&compat_hashtab, name);
22831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022832 return &compat_hashtab;
22833
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022836 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 }
22838 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022839 if (*name == 'g') /* global variable */
22840 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022841 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22842 */
22843 if (vim_strchr(name + 2, ':') != NULL
22844 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022845 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022847 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022849 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022850#ifdef FEAT_WINDOWS
22851 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022852 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022853#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 if (*name == 'v') /* v: variable */
22855 return &vimvarht;
22856 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022857 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022859 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 if (*name == 's' /* script variable */
22861 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22862 return &SCRIPT_VARS(current_SID);
22863 return NULL;
22864}
22865
22866/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022867 * Get function call environment based on bactrace debug level
22868 */
22869 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022871{
22872 int i;
22873 funccall_T *funccal;
22874 funccall_T *temp_funccal;
22875
22876 funccal = current_funccal;
22877 if (debug_backtrace_level > 0)
22878 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022879 for (i = 0; i < debug_backtrace_level; i++)
22880 {
22881 temp_funccal = funccal->caller;
22882 if (temp_funccal)
22883 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022884 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022885 /* backtrace level overflow. reset to max */
22886 debug_backtrace_level = i;
22887 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022888 }
22889 return funccal;
22890}
22891
22892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022894 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 * Returns NULL when it doesn't exist.
22896 */
22897 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022898get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899{
Bram Moolenaar33570922005-01-25 22:26:29 +000022900 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022902 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 if (v == NULL)
22904 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906}
22907
22908/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 * sourcing this script and when executing functions defined in the script.
22911 */
22912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022913new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914{
Bram Moolenaara7043832005-01-21 11:56:39 +000022915 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 hashtab_T *ht;
22917 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022918
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022921 /* Re-allocating ga_data means that an ht_array pointing to
22922 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022924 for (i = 1; i <= ga_scripts.ga_len; ++i)
22925 {
22926 ht = &SCRIPT_VARS(i);
22927 if (ht->ht_mask == HT_INIT_SIZE - 1)
22928 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022929 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022930 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022931 }
22932
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 while (ga_scripts.ga_len < id)
22934 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022935 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022936 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022937 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 }
22940 }
22941}
22942
22943/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022944 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22945 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 */
22947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022948init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949{
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022951 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022952 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022953 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022954 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022955 dict_var->di_tv.vval.v_dict = dict;
22956 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022957 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22959 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960}
22961
22962/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022963 * Unreference a dictionary initialized by init_var_dict().
22964 */
22965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022966unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022967{
22968 /* Now the dict needs to be freed if no one else is using it, go back to
22969 * normal reference counting. */
22970 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22971 dict_unref(dict);
22972}
22973
22974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 * Frees all allocated variables and the value they contain.
22977 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 */
22979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022980vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022981{
22982 vars_clear_ext(ht, TRUE);
22983}
22984
22985/*
22986 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22987 */
22988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022989vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990{
Bram Moolenaara7043832005-01-21 11:56:39 +000022991 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 hashitem_T *hi;
22993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022996 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022997 for (hi = ht->ht_array; todo > 0; ++hi)
22998 {
22999 if (!HASHITEM_EMPTY(hi))
23000 {
23001 --todo;
23002
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023004 * ht_array might change then. hash_clear() takes care of it
23005 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 v = HI2DI(hi);
23007 if (free_val)
23008 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023009 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023010 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023011 }
23012 }
23013 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023014 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015}
23016
Bram Moolenaara7043832005-01-21 11:56:39 +000023017/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 * Delete a variable from hashtab "ht" at item "hi".
23019 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023022delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023{
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023025
23026 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 clear_tv(&di->di_tv);
23028 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029}
23030
23031/*
23032 * List the value of one internal variable.
23033 */
23034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023037 char_u *tofree;
23038 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023040
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023041 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023043 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023044 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045}
23046
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023048list_one_var_a(
23049 char_u *prefix,
23050 char_u *name,
23051 int type,
23052 char_u *string,
23053 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054{
Bram Moolenaar31859182007-08-14 20:41:13 +000023055 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23056 msg_start();
23057 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 if (name != NULL) /* "a:" vars don't have a name stored */
23059 msg_puts(name);
23060 msg_putchar(' ');
23061 msg_advance(22);
23062 if (type == VAR_NUMBER)
23063 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023064 else if (type == VAR_FUNC)
23065 msg_putchar('*');
23066 else if (type == VAR_LIST)
23067 {
23068 msg_putchar('[');
23069 if (*string == '[')
23070 ++string;
23071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023072 else if (type == VAR_DICT)
23073 {
23074 msg_putchar('{');
23075 if (*string == '{')
23076 ++string;
23077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 else
23079 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023080
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023082
23083 if (type == VAR_FUNC)
23084 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023085 if (*first)
23086 {
23087 msg_clr_eos();
23088 *first = FALSE;
23089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090}
23091
23092/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023093 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 * If the variable already exists, the value is updated.
23095 * Otherwise the variable is created.
23096 */
23097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023098set_var(
23099 char_u *name,
23100 typval_T *tv,
23101 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102{
Bram Moolenaar33570922005-01-25 22:26:29 +000023103 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023107 ht = find_var_ht(name, &varname);
23108 if (ht == NULL || *varname == NUL)
23109 {
23110 EMSG2(_(e_illvar), name);
23111 return;
23112 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023113 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023114
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023115 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
23116 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023117
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023121 if (var_check_ro(v->di_flags, name, FALSE)
23122 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 return;
23124 if (v->di_tv.v_type != tv->v_type
23125 && !((v->di_tv.v_type == VAR_STRING
23126 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023127 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023128 || tv->v_type == VAR_NUMBER))
23129#ifdef FEAT_FLOAT
23130 && !((v->di_tv.v_type == VAR_NUMBER
23131 || v->di_tv.v_type == VAR_FLOAT)
23132 && (tv->v_type == VAR_NUMBER
23133 || tv->v_type == VAR_FLOAT))
23134#endif
23135 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023136 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000023137 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023138 return;
23139 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023140
23141 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023142 * Handle setting internal v: variables separately where needed to
23143 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023144 */
23145 if (ht == &vimvarht)
23146 {
23147 if (v->di_tv.v_type == VAR_STRING)
23148 {
23149 vim_free(v->di_tv.vval.v_string);
23150 if (copy || tv->v_type != VAR_STRING)
23151 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23152 else
23153 {
23154 /* Take over the string to avoid an extra alloc/free. */
23155 v->di_tv.vval.v_string = tv->vval.v_string;
23156 tv->vval.v_string = NULL;
23157 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023158 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023159 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023160 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023161 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023163 if (STRCMP(varname, "searchforward") == 0)
23164 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023165#ifdef FEAT_SEARCH_EXTRA
23166 else if (STRCMP(varname, "hlsearch") == 0)
23167 {
23168 no_hlsearch = !v->di_tv.vval.v_number;
23169 redraw_all_later(SOME_VALID);
23170 }
23171#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023172 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023173 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023174 else if (v->di_tv.v_type != tv->v_type)
23175 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023176 }
23177
23178 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 }
23180 else /* add a new variable */
23181 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023182 /* Can't add "v:" variable. */
23183 if (ht == &vimvarht)
23184 {
23185 EMSG2(_(e_illvar), name);
23186 return;
23187 }
23188
Bram Moolenaar92124a32005-06-17 22:03:40 +000023189 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023190 if (!valid_varname(varname))
23191 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023192
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023193 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23194 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023195 if (v == NULL)
23196 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023197 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023200 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 return;
23202 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023203 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023206 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023208 else
23209 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023210 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023211 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023212 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214}
23215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023216/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023217 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023218 * Also give an error message.
23219 */
23220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023221var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023222{
23223 if (flags & DI_FLAGS_RO)
23224 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023225 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 return TRUE;
23227 }
23228 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23229 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023230 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023231 return TRUE;
23232 }
23233 return FALSE;
23234}
23235
23236/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023237 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23238 * Also give an error message.
23239 */
23240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023241var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023242{
23243 if (flags & DI_FLAGS_FIX)
23244 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023245 EMSG2(_("E795: Cannot delete variable %s"),
23246 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023247 return TRUE;
23248 }
23249 return FALSE;
23250}
23251
23252/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023253 * Check if a funcref is assigned to a valid variable name.
23254 * Return TRUE and give an error if not.
23255 */
23256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023257var_check_func_name(
23258 char_u *name, /* points to start of variable name */
23259 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023260{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023261 /* Allow for w: b: s: and t:. */
23262 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023263 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23264 ? name[2] : name[0]))
23265 {
23266 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23267 name);
23268 return TRUE;
23269 }
23270 /* Don't allow hiding a function. When "v" is not NULL we might be
23271 * assigning another function to the same var, the type is checked
23272 * below. */
23273 if (new_var && function_exists(name))
23274 {
23275 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23276 name);
23277 return TRUE;
23278 }
23279 return FALSE;
23280}
23281
23282/*
23283 * Check if a variable name is valid.
23284 * Return FALSE and give an error if not.
23285 */
23286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023287valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023288{
23289 char_u *p;
23290
23291 for (p = varname; *p != NUL; ++p)
23292 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23293 && *p != AUTOLOAD_CHAR)
23294 {
23295 EMSG2(_(e_illvar), varname);
23296 return FALSE;
23297 }
23298 return TRUE;
23299}
23300
23301/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023302 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023303 * Also give an error message, using "name" or _("name") when use_gettext is
23304 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023305 */
23306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023307tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023308{
23309 if (lock & VAR_LOCKED)
23310 {
23311 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023312 name == NULL ? (char_u *)_("Unknown")
23313 : use_gettext ? (char_u *)_(name)
23314 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023315 return TRUE;
23316 }
23317 if (lock & VAR_FIXED)
23318 {
23319 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023320 name == NULL ? (char_u *)_("Unknown")
23321 : use_gettext ? (char_u *)_(name)
23322 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023323 return TRUE;
23324 }
23325 return FALSE;
23326}
23327
23328/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023329 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023330 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023331 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023332 * It is OK for "from" and "to" to point to the same item. This is used to
23333 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023334 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023336copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023338 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023339 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023340 switch (from->v_type)
23341 {
23342 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023343 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023344 to->vval.v_number = from->vval.v_number;
23345 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023346 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023347#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023348 to->vval.v_float = from->vval.v_float;
23349 break;
23350#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023351 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023352#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023353 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023354 if (to->vval.v_job != NULL)
23355 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023356 break;
23357#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023358 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023359#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023360 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023361 if (to->vval.v_channel != NULL)
23362 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023363 break;
23364#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023365 case VAR_STRING:
23366 case VAR_FUNC:
23367 if (from->vval.v_string == NULL)
23368 to->vval.v_string = NULL;
23369 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023371 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023372 if (from->v_type == VAR_FUNC)
23373 func_ref(to->vval.v_string);
23374 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023375 break;
23376 case VAR_LIST:
23377 if (from->vval.v_list == NULL)
23378 to->vval.v_list = NULL;
23379 else
23380 {
23381 to->vval.v_list = from->vval.v_list;
23382 ++to->vval.v_list->lv_refcount;
23383 }
23384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023385 case VAR_DICT:
23386 if (from->vval.v_dict == NULL)
23387 to->vval.v_dict = NULL;
23388 else
23389 {
23390 to->vval.v_dict = from->vval.v_dict;
23391 ++to->vval.v_dict->dv_refcount;
23392 }
23393 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023394 case VAR_UNKNOWN:
23395 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023396 break;
23397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398}
23399
23400/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023401 * Make a copy of an item.
23402 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023403 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23404 * reference to an already copied list/dict can be used.
23405 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023406 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023408item_copy(
23409 typval_T *from,
23410 typval_T *to,
23411 int deep,
23412 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023413{
23414 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023415 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023416
Bram Moolenaar33570922005-01-25 22:26:29 +000023417 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023418 {
23419 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023420 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023421 }
23422 ++recurse;
23423
23424 switch (from->v_type)
23425 {
23426 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023427 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023428 case VAR_STRING:
23429 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023430 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023431 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023432 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023433 copy_tv(from, to);
23434 break;
23435 case VAR_LIST:
23436 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023437 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023438 if (from->vval.v_list == NULL)
23439 to->vval.v_list = NULL;
23440 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23441 {
23442 /* use the copy made earlier */
23443 to->vval.v_list = from->vval.v_list->lv_copylist;
23444 ++to->vval.v_list->lv_refcount;
23445 }
23446 else
23447 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23448 if (to->vval.v_list == NULL)
23449 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023450 break;
23451 case VAR_DICT:
23452 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023453 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023454 if (from->vval.v_dict == NULL)
23455 to->vval.v_dict = NULL;
23456 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23457 {
23458 /* use the copy made earlier */
23459 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23460 ++to->vval.v_dict->dv_refcount;
23461 }
23462 else
23463 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23464 if (to->vval.v_dict == NULL)
23465 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023466 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023467 case VAR_UNKNOWN:
23468 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023469 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023470 }
23471 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023472 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023473}
23474
23475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 * ":echo expr1 ..." print each argument separated with a space, add a
23477 * newline at the end.
23478 * ":echon expr1 ..." print each argument plain.
23479 */
23480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023481ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482{
23483 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023484 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023485 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 char_u *p;
23487 int needclr = TRUE;
23488 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023489 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490
23491 if (eap->skip)
23492 ++emsg_skip;
23493 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23494 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023495 /* If eval1() causes an error message the text from the command may
23496 * still need to be cleared. E.g., "echo 22,44". */
23497 need_clr_eos = needclr;
23498
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023500 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 {
23502 /*
23503 * Report the invalid expression unless the expression evaluation
23504 * has been cancelled due to an aborting error, an interrupt, or an
23505 * exception.
23506 */
23507 if (!aborting())
23508 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023509 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 break;
23511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023512 need_clr_eos = FALSE;
23513
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 if (!eap->skip)
23515 {
23516 if (atstart)
23517 {
23518 atstart = FALSE;
23519 /* Call msg_start() after eval1(), evaluating the expression
23520 * may cause a message to appear. */
23521 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023522 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023523 /* Mark the saved text as finishing the line, so that what
23524 * follows is displayed on a new line when scrolling back
23525 * at the more prompt. */
23526 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 }
23530 else if (eap->cmdidx == CMD_echo)
23531 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023532 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023533 if (p != NULL)
23534 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023536 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023538 if (*p != TAB && needclr)
23539 {
23540 /* remove any text still there from the command */
23541 msg_clr_eos();
23542 needclr = FALSE;
23543 }
23544 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 }
23546 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023547 {
23548#ifdef FEAT_MBYTE
23549 if (has_mbyte)
23550 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023551 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023552
23553 (void)msg_outtrans_len_attr(p, i, echo_attr);
23554 p += i - 1;
23555 }
23556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023558 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023563 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564 arg = skipwhite(arg);
23565 }
23566 eap->nextcmd = check_nextcmd(arg);
23567
23568 if (eap->skip)
23569 --emsg_skip;
23570 else
23571 {
23572 /* remove text that may still be there from the command */
23573 if (needclr)
23574 msg_clr_eos();
23575 if (eap->cmdidx == CMD_echo)
23576 msg_end();
23577 }
23578}
23579
23580/*
23581 * ":echohl {name}".
23582 */
23583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023584ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585{
23586 int id;
23587
23588 id = syn_name2id(eap->arg);
23589 if (id == 0)
23590 echo_attr = 0;
23591 else
23592 echo_attr = syn_id2attr(id);
23593}
23594
23595/*
23596 * ":execute expr1 ..." execute the result of an expression.
23597 * ":echomsg expr1 ..." Print a message
23598 * ":echoerr expr1 ..." Print an error
23599 * Each gets spaces around each argument and a newline at the end for
23600 * echo commands
23601 */
23602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023603ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604{
23605 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023606 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 int ret = OK;
23608 char_u *p;
23609 garray_T ga;
23610 int len;
23611 int save_did_emsg;
23612
23613 ga_init2(&ga, 1, 80);
23614
23615 if (eap->skip)
23616 ++emsg_skip;
23617 while (*arg != NUL && *arg != '|' && *arg != '\n')
23618 {
23619 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023620 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 {
23622 /*
23623 * Report the invalid expression unless the expression evaluation
23624 * has been cancelled due to an aborting error, an interrupt, or an
23625 * exception.
23626 */
23627 if (!aborting())
23628 EMSG2(_(e_invexpr2), p);
23629 ret = FAIL;
23630 break;
23631 }
23632
23633 if (!eap->skip)
23634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023635 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 len = (int)STRLEN(p);
23637 if (ga_grow(&ga, len + 2) == FAIL)
23638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023639 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 ret = FAIL;
23641 break;
23642 }
23643 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 ga.ga_len += len;
23647 }
23648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023649 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 arg = skipwhite(arg);
23651 }
23652
23653 if (ret != FAIL && ga.ga_data != NULL)
23654 {
23655 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023658 out_flush();
23659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 else if (eap->cmdidx == CMD_echoerr)
23661 {
23662 /* We don't want to abort following commands, restore did_emsg. */
23663 save_did_emsg = did_emsg;
23664 EMSG((char_u *)ga.ga_data);
23665 if (!force_abort)
23666 did_emsg = save_did_emsg;
23667 }
23668 else if (eap->cmdidx == CMD_execute)
23669 do_cmdline((char_u *)ga.ga_data,
23670 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23671 }
23672
23673 ga_clear(&ga);
23674
23675 if (eap->skip)
23676 --emsg_skip;
23677
23678 eap->nextcmd = check_nextcmd(arg);
23679}
23680
23681/*
23682 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23683 * "arg" points to the "&" or '+' when called, to "option" when returning.
23684 * Returns NULL when no option name found. Otherwise pointer to the char
23685 * after the option name.
23686 */
23687 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023688find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689{
23690 char_u *p = *arg;
23691
23692 ++p;
23693 if (*p == 'g' && p[1] == ':')
23694 {
23695 *opt_flags = OPT_GLOBAL;
23696 p += 2;
23697 }
23698 else if (*p == 'l' && p[1] == ':')
23699 {
23700 *opt_flags = OPT_LOCAL;
23701 p += 2;
23702 }
23703 else
23704 *opt_flags = 0;
23705
23706 if (!ASCII_ISALPHA(*p))
23707 return NULL;
23708 *arg = p;
23709
23710 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23711 p += 4; /* termcap option */
23712 else
23713 while (ASCII_ISALPHA(*p))
23714 ++p;
23715 return p;
23716}
23717
23718/*
23719 * ":function"
23720 */
23721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023722ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723{
23724 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023725 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 int j;
23727 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023729 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 char_u *name = NULL;
23731 char_u *p;
23732 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023733 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 garray_T newargs;
23735 garray_T newlines;
23736 int varargs = FALSE;
23737 int mustend = FALSE;
23738 int flags = 0;
23739 ufunc_T *fp;
23740 int indent;
23741 int nesting;
23742 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023743 dictitem_T *v;
23744 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023745 static int func_nr = 0; /* number for nameless function */
23746 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023747 hashtab_T *ht;
23748 int todo;
23749 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023750 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751
23752 /*
23753 * ":function" without argument: list functions.
23754 */
23755 if (ends_excmd(*eap->arg))
23756 {
23757 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023758 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023759 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023760 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023761 {
23762 if (!HASHITEM_EMPTY(hi))
23763 {
23764 --todo;
23765 fp = HI2UF(hi);
23766 if (!isdigit(*fp->uf_name))
23767 list_func_head(fp, FALSE);
23768 }
23769 }
23770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 eap->nextcmd = check_nextcmd(eap->arg);
23772 return;
23773 }
23774
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023775 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023776 * ":function /pat": list functions matching pattern.
23777 */
23778 if (*eap->arg == '/')
23779 {
23780 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23781 if (!eap->skip)
23782 {
23783 regmatch_T regmatch;
23784
23785 c = *p;
23786 *p = NUL;
23787 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23788 *p = c;
23789 if (regmatch.regprog != NULL)
23790 {
23791 regmatch.rm_ic = p_ic;
23792
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023793 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023794 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23795 {
23796 if (!HASHITEM_EMPTY(hi))
23797 {
23798 --todo;
23799 fp = HI2UF(hi);
23800 if (!isdigit(*fp->uf_name)
23801 && vim_regexec(&regmatch, fp->uf_name, 0))
23802 list_func_head(fp, FALSE);
23803 }
23804 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023805 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023806 }
23807 }
23808 if (*p == '/')
23809 ++p;
23810 eap->nextcmd = check_nextcmd(p);
23811 return;
23812 }
23813
23814 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023815 * Get the function name. There are these situations:
23816 * func normal function name
23817 * "name" == func, "fudi.fd_dict" == NULL
23818 * dict.func new dictionary entry
23819 * "name" == NULL, "fudi.fd_dict" set,
23820 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23821 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023822 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023823 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23824 * dict.func existing dict entry that's not a Funcref
23825 * "name" == NULL, "fudi.fd_dict" set,
23826 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023827 * s:func script-local function name
23828 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 name = trans_function_name(&p, eap->skip, 0, &fudi);
23832 paren = (vim_strchr(p, '(') != NULL);
23833 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 {
23835 /*
23836 * Return on an invalid expression in braces, unless the expression
23837 * evaluation has been cancelled due to an aborting error, an
23838 * interrupt, or an exception.
23839 */
23840 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023841 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023842 if (!eap->skip && fudi.fd_newkey != NULL)
23843 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023844 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 else
23848 eap->skip = TRUE;
23849 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023850
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 /* An error in a function call during evaluation of an expression in magic
23852 * braces should not cause the function not to be defined. */
23853 saved_did_emsg = did_emsg;
23854 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855
23856 /*
23857 * ":function func" with only function name: list function.
23858 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 {
23861 if (!ends_excmd(*skipwhite(p)))
23862 {
23863 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023864 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 }
23866 eap->nextcmd = check_nextcmd(p);
23867 if (eap->nextcmd != NULL)
23868 *p = NUL;
23869 if (!eap->skip && !got_int)
23870 {
23871 fp = find_func(name);
23872 if (fp != NULL)
23873 {
23874 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023875 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023877 if (FUNCLINE(fp, j) == NULL)
23878 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 msg_putchar('\n');
23880 msg_outnum((long)(j + 1));
23881 if (j < 9)
23882 msg_putchar(' ');
23883 if (j < 99)
23884 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023885 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 out_flush(); /* show a line at a time */
23887 ui_breakcheck();
23888 }
23889 if (!got_int)
23890 {
23891 msg_putchar('\n');
23892 msg_puts((char_u *)" endfunction");
23893 }
23894 }
23895 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023896 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 }
23900
23901 /*
23902 * ":function name(arg1, arg2)" Define function.
23903 */
23904 p = skipwhite(p);
23905 if (*p != '(')
23906 {
23907 if (!eap->skip)
23908 {
23909 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023910 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911 }
23912 /* attempt to continue by skipping some text */
23913 if (vim_strchr(p, '(') != NULL)
23914 p = vim_strchr(p, '(');
23915 }
23916 p = skipwhite(p + 1);
23917
23918 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23919 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23920
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023921 if (!eap->skip)
23922 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023923 /* Check the name of the function. Unless it's a dictionary function
23924 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023925 if (name != NULL)
23926 arg = name;
23927 else
23928 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023929 if (arg != NULL && (fudi.fd_di == NULL
23930 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023931 {
23932 if (*arg == K_SPECIAL)
23933 j = 3;
23934 else
23935 j = 0;
23936 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23937 : eval_isnamec(arg[j])))
23938 ++j;
23939 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023940 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023941 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023942 /* Disallow using the g: dict. */
23943 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23944 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023945 }
23946
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 /*
23948 * Isolate the arguments: "arg1, arg2, ...)"
23949 */
23950 while (*p != ')')
23951 {
23952 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23953 {
23954 varargs = TRUE;
23955 p += 3;
23956 mustend = TRUE;
23957 }
23958 else
23959 {
23960 arg = p;
23961 while (ASCII_ISALNUM(*p) || *p == '_')
23962 ++p;
23963 if (arg == p || isdigit(*arg)
23964 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23965 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23966 {
23967 if (!eap->skip)
23968 EMSG2(_("E125: Illegal argument: %s"), arg);
23969 break;
23970 }
23971 if (ga_grow(&newargs, 1) == FAIL)
23972 goto erret;
23973 c = *p;
23974 *p = NUL;
23975 arg = vim_strsave(arg);
23976 if (arg == NULL)
23977 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023978
23979 /* Check for duplicate argument name. */
23980 for (i = 0; i < newargs.ga_len; ++i)
23981 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23982 {
23983 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023984 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023985 goto erret;
23986 }
23987
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23989 *p = c;
23990 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 if (*p == ',')
23992 ++p;
23993 else
23994 mustend = TRUE;
23995 }
23996 p = skipwhite(p);
23997 if (mustend && *p != ')')
23998 {
23999 if (!eap->skip)
24000 EMSG2(_(e_invarg2), eap->arg);
24001 break;
24002 }
24003 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024004 if (*p != ')')
24005 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 ++p; /* skip the ')' */
24007
Bram Moolenaare9a41262005-01-15 22:18:47 +000024008 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009 for (;;)
24010 {
24011 p = skipwhite(p);
24012 if (STRNCMP(p, "range", 5) == 0)
24013 {
24014 flags |= FC_RANGE;
24015 p += 5;
24016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024017 else if (STRNCMP(p, "dict", 4) == 0)
24018 {
24019 flags |= FC_DICT;
24020 p += 4;
24021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 else if (STRNCMP(p, "abort", 5) == 0)
24023 {
24024 flags |= FC_ABORT;
24025 p += 5;
24026 }
24027 else
24028 break;
24029 }
24030
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024031 /* When there is a line break use what follows for the function body.
24032 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24033 if (*p == '\n')
24034 line_arg = p + 1;
24035 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 EMSG(_(e_trailing));
24037
24038 /*
24039 * Read the body of the function, until ":endfunction" is found.
24040 */
24041 if (KeyTyped)
24042 {
24043 /* Check if the function already exists, don't let the user type the
24044 * whole function before telling him it doesn't work! For a script we
24045 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024046 if (!eap->skip && !eap->forceit)
24047 {
24048 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24049 EMSG(_(e_funcdict));
24050 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024051 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024054 if (!eap->skip && did_emsg)
24055 goto erret;
24056
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 msg_putchar('\n'); /* don't overwrite the function name */
24058 cmdline_row = msg_row;
24059 }
24060
24061 indent = 2;
24062 nesting = 0;
24063 for (;;)
24064 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024065 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024066 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024067 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024068 saved_wait_return = FALSE;
24069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024071 sourcing_lnum_off = sourcing_lnum;
24072
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024073 if (line_arg != NULL)
24074 {
24075 /* Use eap->arg, split up in parts by line breaks. */
24076 theline = line_arg;
24077 p = vim_strchr(theline, '\n');
24078 if (p == NULL)
24079 line_arg += STRLEN(line_arg);
24080 else
24081 {
24082 *p = NUL;
24083 line_arg = p + 1;
24084 }
24085 }
24086 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 theline = getcmdline(':', 0L, indent);
24088 else
24089 theline = eap->getline(':', eap->cookie, indent);
24090 if (KeyTyped)
24091 lines_left = Rows - 1;
24092 if (theline == NULL)
24093 {
24094 EMSG(_("E126: Missing :endfunction"));
24095 goto erret;
24096 }
24097
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024098 /* Detect line continuation: sourcing_lnum increased more than one. */
24099 if (sourcing_lnum > sourcing_lnum_off + 1)
24100 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24101 else
24102 sourcing_lnum_off = 0;
24103
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 if (skip_until != NULL)
24105 {
24106 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24107 * don't check for ":endfunc". */
24108 if (STRCMP(theline, skip_until) == 0)
24109 {
24110 vim_free(skip_until);
24111 skip_until = NULL;
24112 }
24113 }
24114 else
24115 {
24116 /* skip ':' and blanks*/
24117 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24118 ;
24119
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024120 /* Check for "endfunction". */
24121 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024123 if (line_arg == NULL)
24124 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 break;
24126 }
24127
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024128 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 * at "end". */
24130 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24131 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024132 else if (STRNCMP(p, "if", 2) == 0
24133 || STRNCMP(p, "wh", 2) == 0
24134 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 || STRNCMP(p, "try", 3) == 0)
24136 indent += 2;
24137
24138 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024139 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024141 if (*p == '!')
24142 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010024144 vim_free(trans_function_name(&p, TRUE, 0, NULL));
24145 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024147 ++nesting;
24148 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 }
24150 }
24151
24152 /* Check for ":append" or ":insert". */
24153 p = skip_range(p, NULL);
24154 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24155 || (p[0] == 'i'
24156 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24157 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24158 skip_until = vim_strsave((char_u *)".");
24159
24160 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24161 arg = skipwhite(skiptowhite(p));
24162 if (arg[0] == '<' && arg[1] =='<'
24163 && ((p[0] == 'p' && p[1] == 'y'
24164 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24165 || (p[0] == 'p' && p[1] == 'e'
24166 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24167 || (p[0] == 't' && p[1] == 'c'
24168 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024169 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24170 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24172 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024173 || (p[0] == 'm' && p[1] == 'z'
24174 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 ))
24176 {
24177 /* ":python <<" continues until a dot, like ":append" */
24178 p = skipwhite(arg + 2);
24179 if (*p == NUL)
24180 skip_until = vim_strsave((char_u *)".");
24181 else
24182 skip_until = vim_strsave(p);
24183 }
24184 }
24185
24186 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024187 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024188 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024189 if (line_arg == NULL)
24190 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024192 }
24193
24194 /* Copy the line to newly allocated memory. get_one_sourceline()
24195 * allocates 250 bytes per line, this saves 80% on average. The cost
24196 * is an extra alloc/free. */
24197 p = vim_strsave(theline);
24198 if (p != NULL)
24199 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024200 if (line_arg == NULL)
24201 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024202 theline = p;
24203 }
24204
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024205 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24206
24207 /* Add NULL lines for continuation lines, so that the line count is
24208 * equal to the index in the growarray. */
24209 while (sourcing_lnum_off-- > 0)
24210 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024211
24212 /* Check for end of eap->arg. */
24213 if (line_arg != NULL && *line_arg == NUL)
24214 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 }
24216
24217 /* Don't define the function when skipping commands or when an error was
24218 * detected. */
24219 if (eap->skip || did_emsg)
24220 goto erret;
24221
24222 /*
24223 * If there are no errors, add the function
24224 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024225 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024226 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024227 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024228 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024230 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024231 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024232 goto erret;
24233 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024234
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235 fp = find_func(name);
24236 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024238 if (!eap->forceit)
24239 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024240 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241 goto erret;
24242 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024243 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024244 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024245 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 name);
24247 goto erret;
24248 }
24249 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024250 ga_clear_strings(&(fp->uf_args));
24251 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252 vim_free(name);
24253 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 }
24256 else
24257 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258 char numbuf[20];
24259
24260 fp = NULL;
24261 if (fudi.fd_newkey == NULL && !eap->forceit)
24262 {
24263 EMSG(_(e_funcdict));
24264 goto erret;
24265 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024266 if (fudi.fd_di == NULL)
24267 {
24268 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024269 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024270 goto erret;
24271 }
24272 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024273 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024274 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024275
24276 /* Give the function a sequential number. Can only be used with a
24277 * Funcref! */
24278 vim_free(name);
24279 sprintf(numbuf, "%d", ++func_nr);
24280 name = vim_strsave((char_u *)numbuf);
24281 if (name == NULL)
24282 goto erret;
24283 }
24284
24285 if (fp == NULL)
24286 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024287 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024288 {
24289 int slen, plen;
24290 char_u *scriptname;
24291
24292 /* Check that the autoload name matches the script name. */
24293 j = FAIL;
24294 if (sourcing_name != NULL)
24295 {
24296 scriptname = autoload_name(name);
24297 if (scriptname != NULL)
24298 {
24299 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024300 plen = (int)STRLEN(p);
24301 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 if (slen > plen && fnamecmp(p,
24303 sourcing_name + slen - plen) == 0)
24304 j = OK;
24305 vim_free(scriptname);
24306 }
24307 }
24308 if (j == FAIL)
24309 {
24310 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24311 goto erret;
24312 }
24313 }
24314
24315 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 if (fp == NULL)
24317 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024318
24319 if (fudi.fd_dict != NULL)
24320 {
24321 if (fudi.fd_di == NULL)
24322 {
24323 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024324 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024325 if (fudi.fd_di == NULL)
24326 {
24327 vim_free(fp);
24328 goto erret;
24329 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024330 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24331 {
24332 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024333 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024334 goto erret;
24335 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024336 }
24337 else
24338 /* overwrite existing dict entry */
24339 clear_tv(&fudi.fd_di->di_tv);
24340 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024341 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024342 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024343 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024344
24345 /* behave like "dict" was used */
24346 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 }
24348
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024350 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024351 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24352 {
24353 vim_free(fp);
24354 goto erret;
24355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 fp->uf_args = newargs;
24358 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359#ifdef FEAT_PROFILE
24360 fp->uf_tml_count = NULL;
24361 fp->uf_tml_total = NULL;
24362 fp->uf_tml_self = NULL;
24363 fp->uf_profiling = FALSE;
24364 if (prof_def_func())
24365 func_do_profile(fp);
24366#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024367 fp->uf_varargs = varargs;
24368 fp->uf_flags = flags;
24369 fp->uf_calls = 0;
24370 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024371 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372
24373erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 ga_clear_strings(&newargs);
24375 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024376ret_free:
24377 vim_free(skip_until);
24378 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024381 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382}
24383
24384/*
24385 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024386 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024388 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024389 * TFN_INT: internal function name OK
24390 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024391 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 * Advances "pp" to just after the function name (if no error).
24393 */
24394 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024395trans_function_name(
24396 char_u **pp,
24397 int skip, /* only find the end, don't evaluate */
24398 int flags,
24399 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024401 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 char_u *start;
24403 char_u *end;
24404 int lead;
24405 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024407 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024408
24409 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024410 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024412
24413 /* Check for hard coded <SNR>: already translated function ID (from a user
24414 * command). */
24415 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24416 && (*pp)[2] == (int)KE_SNR)
24417 {
24418 *pp += 3;
24419 len = get_id_len(pp) + 3;
24420 return vim_strnsave(start, len);
24421 }
24422
24423 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24424 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024426 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024427 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024428
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024429 /* Note that TFN_ flags use the same values as GLV_ flags. */
24430 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024431 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024432 if (end == start)
24433 {
24434 if (!skip)
24435 EMSG(_("E129: Function name required"));
24436 goto theend;
24437 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024438 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024439 {
24440 /*
24441 * Report an invalid expression in braces, unless the expression
24442 * evaluation has been cancelled due to an aborting error, an
24443 * interrupt, or an exception.
24444 */
24445 if (!aborting())
24446 {
24447 if (end != NULL)
24448 EMSG2(_(e_invarg2), start);
24449 }
24450 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024451 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024452 goto theend;
24453 }
24454
24455 if (lv.ll_tv != NULL)
24456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024457 if (fdp != NULL)
24458 {
24459 fdp->fd_dict = lv.ll_dict;
24460 fdp->fd_newkey = lv.ll_newkey;
24461 lv.ll_newkey = NULL;
24462 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024463 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024464 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24465 {
24466 name = vim_strsave(lv.ll_tv->vval.v_string);
24467 *pp = end;
24468 }
24469 else
24470 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024471 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24472 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 EMSG(_(e_funcref));
24474 else
24475 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024476 name = NULL;
24477 }
24478 goto theend;
24479 }
24480
24481 if (lv.ll_name == NULL)
24482 {
24483 /* Error found, but continue after the function name. */
24484 *pp = end;
24485 goto theend;
24486 }
24487
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024488 /* Check if the name is a Funcref. If so, use the value. */
24489 if (lv.ll_exp_name != NULL)
24490 {
24491 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024492 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024493 if (name == lv.ll_exp_name)
24494 name = NULL;
24495 }
24496 else
24497 {
24498 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024499 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024500 if (name == *pp)
24501 name = NULL;
24502 }
24503 if (name != NULL)
24504 {
24505 name = vim_strsave(name);
24506 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024507 if (STRNCMP(name, "<SNR>", 5) == 0)
24508 {
24509 /* Change "<SNR>" to the byte sequence. */
24510 name[0] = K_SPECIAL;
24511 name[1] = KS_EXTRA;
24512 name[2] = (int)KE_SNR;
24513 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24514 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024515 goto theend;
24516 }
24517
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024518 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024519 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024520 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024521 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24522 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24523 {
24524 /* When there was "s:" already or the name expanded to get a
24525 * leading "s:" then remove it. */
24526 lv.ll_name += 2;
24527 len -= 2;
24528 lead = 2;
24529 }
24530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024531 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024532 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024533 /* skip over "s:" and "g:" */
24534 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024535 lv.ll_name += 2;
24536 len = (int)(end - lv.ll_name);
24537 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024538
24539 /*
24540 * Copy the function name to allocated memory.
24541 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24542 * Accept <SNR>123_name() outside a script.
24543 */
24544 if (skip)
24545 lead = 0; /* do nothing */
24546 else if (lead > 0)
24547 {
24548 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024549 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24550 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024551 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024552 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024553 if (current_SID <= 0)
24554 {
24555 EMSG(_(e_usingsid));
24556 goto theend;
24557 }
24558 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24559 lead += (int)STRLEN(sid_buf);
24560 }
24561 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024562 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024563 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024564 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024565 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024566 goto theend;
24567 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024568 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024569 {
24570 char_u *cp = vim_strchr(lv.ll_name, ':');
24571
24572 if (cp != NULL && cp < end)
24573 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024574 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024575 goto theend;
24576 }
24577 }
24578
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024579 name = alloc((unsigned)(len + lead + 1));
24580 if (name != NULL)
24581 {
24582 if (lead > 0)
24583 {
24584 name[0] = K_SPECIAL;
24585 name[1] = KS_EXTRA;
24586 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024587 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024588 STRCPY(name + 3, sid_buf);
24589 }
24590 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024591 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024592 }
24593 *pp = end;
24594
24595theend:
24596 clear_lval(&lv);
24597 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598}
24599
24600/*
24601 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24602 * Return 2 if "p" starts with "s:".
24603 * Return 0 otherwise.
24604 */
24605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024606eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024608 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24609 * the standard library function. */
24610 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24611 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612 return 5;
24613 if (p[0] == 's' && p[1] == ':')
24614 return 2;
24615 return 0;
24616}
24617
24618/*
24619 * Return TRUE if "p" starts with "<SID>" or "s:".
24620 * Only works if eval_fname_script() returned non-zero for "p"!
24621 */
24622 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024623eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624{
24625 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24626}
24627
24628/*
24629 * List the head of the function: "name(arg1, arg2)".
24630 */
24631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024632list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633{
24634 int j;
24635
24636 msg_start();
24637 if (indent)
24638 MSG_PUTS(" ");
24639 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024640 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 {
24642 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024644 }
24645 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024646 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024648 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 {
24650 if (j)
24651 MSG_PUTS(", ");
24652 msg_puts(FUNCARG(fp, j));
24653 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024654 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 {
24656 if (j)
24657 MSG_PUTS(", ");
24658 MSG_PUTS("...");
24659 }
24660 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024661 if (fp->uf_flags & FC_ABORT)
24662 MSG_PUTS(" abort");
24663 if (fp->uf_flags & FC_RANGE)
24664 MSG_PUTS(" range");
24665 if (fp->uf_flags & FC_DICT)
24666 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024667 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024668 if (p_verbose > 0)
24669 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670}
24671
24672/*
24673 * Find a function by name, return pointer to it in ufuncs.
24674 * Return NULL for unknown function.
24675 */
24676 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024677find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024679 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024681 hi = hash_find(&func_hashtab, name);
24682 if (!HASHITEM_EMPTY(hi))
24683 return HI2UF(hi);
24684 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685}
24686
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024687#if defined(EXITFREE) || defined(PROTO)
24688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024689free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024690{
24691 hashitem_T *hi;
24692
24693 /* Need to start all over every time, because func_free() may change the
24694 * hash table. */
24695 while (func_hashtab.ht_used > 0)
24696 for (hi = func_hashtab.ht_array; ; ++hi)
24697 if (!HASHITEM_EMPTY(hi))
24698 {
24699 func_free(HI2UF(hi));
24700 break;
24701 }
24702}
24703#endif
24704
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024706translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024707{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024708 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024709 return find_internal_func(name) >= 0;
24710 return find_func(name) != NULL;
24711}
24712
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024713/*
24714 * Return TRUE if a function "name" exists.
24715 */
24716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024717function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024718{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024719 char_u *nm = name;
24720 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024721 int n = FALSE;
24722
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024723 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24724 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024725 nm = skipwhite(nm);
24726
24727 /* Only accept "funcname", "funcname ", "funcname (..." and
24728 * "funcname(...", not "funcname!...". */
24729 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024730 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024731 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024732 return n;
24733}
24734
Bram Moolenaara1544c02013-05-30 12:35:52 +020024735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024736get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024737{
24738 char_u *nm = name;
24739 char_u *p;
24740
24741 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24742
24743 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024744 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024745 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024746
Bram Moolenaara1544c02013-05-30 12:35:52 +020024747 vim_free(p);
24748 return NULL;
24749}
24750
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024751/*
24752 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024753 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24754 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024755 */
24756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024757builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024758{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024759 char_u *p;
24760
24761 if (!ASCII_ISLOWER(name[0]))
24762 return FALSE;
24763 p = vim_strchr(name, AUTOLOAD_CHAR);
24764 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024765}
24766
Bram Moolenaar05159a02005-02-26 23:04:13 +000024767#if defined(FEAT_PROFILE) || defined(PROTO)
24768/*
24769 * Start profiling function "fp".
24770 */
24771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024772func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024774 int len = fp->uf_lines.ga_len;
24775
24776 if (len == 0)
24777 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024778 fp->uf_tm_count = 0;
24779 profile_zero(&fp->uf_tm_self);
24780 profile_zero(&fp->uf_tm_total);
24781 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024782 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024783 if (fp->uf_tml_total == NULL)
24784 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024785 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786 if (fp->uf_tml_self == NULL)
24787 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024788 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024789 fp->uf_tml_idx = -1;
24790 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24791 || fp->uf_tml_self == NULL)
24792 return; /* out of memory */
24793
24794 fp->uf_profiling = TRUE;
24795}
24796
24797/*
24798 * Dump the profiling results for all functions in file "fd".
24799 */
24800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024801func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802{
24803 hashitem_T *hi;
24804 int todo;
24805 ufunc_T *fp;
24806 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024807 ufunc_T **sorttab;
24808 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024809
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024810 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024811 if (todo == 0)
24812 return; /* nothing to dump */
24813
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024814 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024815
Bram Moolenaar05159a02005-02-26 23:04:13 +000024816 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24817 {
24818 if (!HASHITEM_EMPTY(hi))
24819 {
24820 --todo;
24821 fp = HI2UF(hi);
24822 if (fp->uf_profiling)
24823 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024824 if (sorttab != NULL)
24825 sorttab[st_len++] = fp;
24826
Bram Moolenaar05159a02005-02-26 23:04:13 +000024827 if (fp->uf_name[0] == K_SPECIAL)
24828 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24829 else
24830 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24831 if (fp->uf_tm_count == 1)
24832 fprintf(fd, "Called 1 time\n");
24833 else
24834 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24835 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24836 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24837 fprintf(fd, "\n");
24838 fprintf(fd, "count total (s) self (s)\n");
24839
24840 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24841 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024842 if (FUNCLINE(fp, i) == NULL)
24843 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024844 prof_func_line(fd, fp->uf_tml_count[i],
24845 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024846 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24847 }
24848 fprintf(fd, "\n");
24849 }
24850 }
24851 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024852
24853 if (sorttab != NULL && st_len > 0)
24854 {
24855 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24856 prof_total_cmp);
24857 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24858 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24859 prof_self_cmp);
24860 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24861 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024862
24863 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024864}
Bram Moolenaar73830342005-02-28 22:48:19 +000024865
24866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024867prof_sort_list(
24868 FILE *fd,
24869 ufunc_T **sorttab,
24870 int st_len,
24871 char *title,
24872 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024873{
24874 int i;
24875 ufunc_T *fp;
24876
24877 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24878 fprintf(fd, "count total (s) self (s) function\n");
24879 for (i = 0; i < 20 && i < st_len; ++i)
24880 {
24881 fp = sorttab[i];
24882 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24883 prefer_self);
24884 if (fp->uf_name[0] == K_SPECIAL)
24885 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24886 else
24887 fprintf(fd, " %s()\n", fp->uf_name);
24888 }
24889 fprintf(fd, "\n");
24890}
24891
24892/*
24893 * Print the count and times for one function or function line.
24894 */
24895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024896prof_func_line(
24897 FILE *fd,
24898 int count,
24899 proftime_T *total,
24900 proftime_T *self,
24901 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024902{
24903 if (count > 0)
24904 {
24905 fprintf(fd, "%5d ", count);
24906 if (prefer_self && profile_equal(total, self))
24907 fprintf(fd, " ");
24908 else
24909 fprintf(fd, "%s ", profile_msg(total));
24910 if (!prefer_self && profile_equal(total, self))
24911 fprintf(fd, " ");
24912 else
24913 fprintf(fd, "%s ", profile_msg(self));
24914 }
24915 else
24916 fprintf(fd, " ");
24917}
24918
24919/*
24920 * Compare function for total time sorting.
24921 */
24922 static int
24923#ifdef __BORLANDC__
24924_RTLENTRYF
24925#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024926prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024927{
24928 ufunc_T *p1, *p2;
24929
24930 p1 = *(ufunc_T **)s1;
24931 p2 = *(ufunc_T **)s2;
24932 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24933}
24934
24935/*
24936 * Compare function for self time sorting.
24937 */
24938 static int
24939#ifdef __BORLANDC__
24940_RTLENTRYF
24941#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024942prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024943{
24944 ufunc_T *p1, *p2;
24945
24946 p1 = *(ufunc_T **)s1;
24947 p2 = *(ufunc_T **)s2;
24948 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24949}
24950
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951#endif
24952
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024953/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024954 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024955 * Return TRUE if a package was loaded.
24956 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024957 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024958script_autoload(
24959 char_u *name,
24960 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024961{
24962 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024963 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024964 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024965 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024966
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024967 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024968 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024969 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024970 return FALSE;
24971
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024972 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024973
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024974 /* Find the name in the list of previously loaded package names. Skip
24975 * "autoload/", it's always the same. */
24976 for (i = 0; i < ga_loaded.ga_len; ++i)
24977 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24978 break;
24979 if (!reload && i < ga_loaded.ga_len)
24980 ret = FALSE; /* was loaded already */
24981 else
24982 {
24983 /* Remember the name if it wasn't loaded already. */
24984 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24985 {
24986 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24987 tofree = NULL;
24988 }
24989
24990 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024991 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024992 ret = TRUE;
24993 }
24994
24995 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024996 return ret;
24997}
24998
24999/*
25000 * Return the autoload script name for a function or variable name.
25001 * Returns NULL when out of memory.
25002 */
25003 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025004autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025005{
25006 char_u *p;
25007 char_u *scriptname;
25008
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025009 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025010 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25011 if (scriptname == NULL)
25012 return FALSE;
25013 STRCPY(scriptname, "autoload/");
25014 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025015 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025016 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025017 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025018 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025019 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025020}
25021
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25023
25024/*
25025 * Function given to ExpandGeneric() to obtain the list of user defined
25026 * function names.
25027 */
25028 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025029get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025031 static long_u done;
25032 static hashitem_T *hi;
25033 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034
25035 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025037 done = 0;
25038 hi = func_hashtab.ht_array;
25039 }
25040 if (done < func_hashtab.ht_used)
25041 {
25042 if (done++ > 0)
25043 ++hi;
25044 while (HASHITEM_EMPTY(hi))
25045 ++hi;
25046 fp = HI2UF(hi);
25047
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025048 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025049 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025050
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025051 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25052 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053
25054 cat_func_name(IObuff, fp);
25055 if (xp->xp_context != EXPAND_USER_FUNC)
25056 {
25057 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025058 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059 STRCAT(IObuff, ")");
25060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 return IObuff;
25062 }
25063 return NULL;
25064}
25065
25066#endif /* FEAT_CMDL_COMPL */
25067
25068/*
25069 * Copy the function name of "fp" to buffer "buf".
25070 * "buf" must be able to hold the function name plus three bytes.
25071 * Takes care of script-local function names.
25072 */
25073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025074cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025076 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 {
25078 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025079 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 }
25081 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025082 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083}
25084
25085/*
25086 * ":delfunction {name}"
25087 */
25088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025089ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025090{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025091 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092 char_u *p;
25093 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025094 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095
25096 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025097 name = trans_function_name(&p, eap->skip, 0, &fudi);
25098 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025100 {
25101 if (fudi.fd_dict != NULL && !eap->skip)
25102 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 if (!ends_excmd(*skipwhite(p)))
25106 {
25107 vim_free(name);
25108 EMSG(_(e_trailing));
25109 return;
25110 }
25111 eap->nextcmd = check_nextcmd(p);
25112 if (eap->nextcmd != NULL)
25113 *p = NUL;
25114
25115 if (!eap->skip)
25116 fp = find_func(name);
25117 vim_free(name);
25118
25119 if (!eap->skip)
25120 {
25121 if (fp == NULL)
25122 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025123 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 return;
25125 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025126 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127 {
25128 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25129 return;
25130 }
25131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025132 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025134 /* Delete the dict item that refers to the function, it will
25135 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025136 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025138 else
25139 func_free(fp);
25140 }
25141}
25142
25143/*
25144 * Free a function and remove it from the list of functions.
25145 */
25146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025147func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025148{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025149 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025150
25151 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025152 ga_clear_strings(&(fp->uf_args));
25153 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025154#ifdef FEAT_PROFILE
25155 vim_free(fp->uf_tml_count);
25156 vim_free(fp->uf_tml_total);
25157 vim_free(fp->uf_tml_self);
25158#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025160 /* remove the function from the function hashtable */
25161 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25162 if (HASHITEM_EMPTY(hi))
25163 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025164 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025165 hash_remove(&func_hashtab, hi);
25166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025167 vim_free(fp);
25168}
25169
25170/*
25171 * Unreference a Function: decrement the reference count and free it when it
25172 * becomes zero. Only for numbered functions.
25173 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025175func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025176{
25177 ufunc_T *fp;
25178
25179 if (name != NULL && isdigit(*name))
25180 {
25181 fp = find_func(name);
25182 if (fp == NULL)
25183 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025184 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025185 {
25186 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025187 * when "uf_calls" becomes zero. */
25188 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025189 func_free(fp);
25190 }
25191 }
25192}
25193
25194/*
25195 * Count a reference to a Function.
25196 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025198func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025199{
25200 ufunc_T *fp;
25201
25202 if (name != NULL && isdigit(*name))
25203 {
25204 fp = find_func(name);
25205 if (fp == NULL)
25206 EMSG2(_(e_intern2), "func_ref()");
25207 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 }
25210}
25211
25212/*
25213 * Call a user function.
25214 */
25215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025216call_user_func(
25217 ufunc_T *fp, /* pointer to function */
25218 int argcount, /* nr of args */
25219 typval_T *argvars, /* arguments */
25220 typval_T *rettv, /* return value */
25221 linenr_T firstline, /* first line of range */
25222 linenr_T lastline, /* last line of range */
25223 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224{
Bram Moolenaar33570922005-01-25 22:26:29 +000025225 char_u *save_sourcing_name;
25226 linenr_T save_sourcing_lnum;
25227 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025228 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025229 int save_did_emsg;
25230 static int depth = 0;
25231 dictitem_T *v;
25232 int fixvar_idx = 0; /* index in fixvar[] */
25233 int i;
25234 int ai;
25235 char_u numbuf[NUMBUFLEN];
25236 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025237 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025238#ifdef FEAT_PROFILE
25239 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025240 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242
25243 /* If depth of calling is getting too high, don't execute the function */
25244 if (depth >= p_mfd)
25245 {
25246 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025247 rettv->v_type = VAR_NUMBER;
25248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 return;
25250 }
25251 ++depth;
25252
25253 line_breakcheck(); /* check for CTRL-C hit */
25254
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025255 fc = (funccall_T *)alloc(sizeof(funccall_T));
25256 fc->caller = current_funccal;
25257 current_funccal = fc;
25258 fc->func = fp;
25259 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025260 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025261 fc->linenr = 0;
25262 fc->returned = FALSE;
25263 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025265 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25266 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267
Bram Moolenaar33570922005-01-25 22:26:29 +000025268 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025269 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025270 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25271 * each argument variable and saves a lot of time.
25272 */
25273 /*
25274 * Init l: variables.
25275 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025276 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025277 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025278 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025279 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25280 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025281 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025282 name = v->di_key;
25283 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025284 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025285 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025286 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025287 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025288 v->di_tv.vval.v_dict = selfdict;
25289 ++selfdict->dv_refcount;
25290 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025291
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 /*
25293 * Init a: variables.
25294 * Set a:0 to "argcount".
25295 * Set a:000 to a list with room for the "..." arguments.
25296 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025297 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025298 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025299 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025300 /* Use "name" to avoid a warning from some compiler that checks the
25301 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025302 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025303 name = v->di_key;
25304 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025305 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025306 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025307 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025308 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025309 v->di_tv.vval.v_list = &fc->l_varlist;
25310 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25311 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25312 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025313
25314 /*
25315 * Set a:firstline to "firstline" and a:lastline to "lastline".
25316 * Set a:name to named arguments.
25317 * Set a:N to the "..." arguments.
25318 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025319 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025320 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025321 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 (varnumber_T)lastline);
25323 for (i = 0; i < argcount; ++i)
25324 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025325 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025326 if (ai < 0)
25327 /* named argument a:name */
25328 name = FUNCARG(fp, i);
25329 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025330 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025331 /* "..." argument a:1, a:2, etc. */
25332 sprintf((char *)numbuf, "%d", ai + 1);
25333 name = numbuf;
25334 }
25335 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25336 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025337 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025338 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25339 }
25340 else
25341 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025342 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25343 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025344 if (v == NULL)
25345 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025346 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025347 }
25348 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025349 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025350
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025351 /* Note: the values are copied directly to avoid alloc/free.
25352 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025354 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025355
25356 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25357 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025358 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25359 fc->l_listitems[ai].li_tv = argvars[i];
25360 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025361 }
25362 }
25363
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 /* Don't redraw while executing the function. */
25365 ++RedrawingDisabled;
25366 save_sourcing_name = sourcing_name;
25367 save_sourcing_lnum = sourcing_lnum;
25368 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025369 /* need space for function name + ("function " + 3) or "[number]" */
25370 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25371 + STRLEN(fp->uf_name) + 20;
25372 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 if (sourcing_name != NULL)
25374 {
25375 if (save_sourcing_name != NULL
25376 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025377 sprintf((char *)sourcing_name, "%s[%d]..",
25378 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 else
25380 STRCPY(sourcing_name, "function ");
25381 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25382
25383 if (p_verbose >= 12)
25384 {
25385 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025386 verbose_enter_scroll();
25387
Bram Moolenaar555b2802005-05-19 21:08:39 +000025388 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 if (p_verbose >= 14)
25390 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025392 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025393 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025394 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395
25396 msg_puts((char_u *)"(");
25397 for (i = 0; i < argcount; ++i)
25398 {
25399 if (i > 0)
25400 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025401 if (argvars[i].v_type == VAR_NUMBER)
25402 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 else
25404 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025405 /* Do not want errors such as E724 here. */
25406 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025407 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025408 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025409 if (s != NULL)
25410 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025411 if (vim_strsize(s) > MSG_BUF_CLEN)
25412 {
25413 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25414 s = buf;
25415 }
25416 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025417 vim_free(tofree);
25418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 }
25420 }
25421 msg_puts((char_u *)")");
25422 }
25423 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025424
25425 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 --no_wait_return;
25427 }
25428 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025429#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025430 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025431 {
25432 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25433 func_do_profile(fp);
25434 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025435 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025436 {
25437 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025438 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025439 profile_zero(&fp->uf_tm_children);
25440 }
25441 script_prof_save(&wait_start);
25442 }
25443#endif
25444
Bram Moolenaar071d4272004-06-13 20:20:40 +000025445 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025446 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 save_did_emsg = did_emsg;
25448 did_emsg = FALSE;
25449
25450 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025451 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25453
25454 --RedrawingDisabled;
25455
25456 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025457 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025459 clear_tv(rettv);
25460 rettv->v_type = VAR_NUMBER;
25461 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 }
25463
Bram Moolenaar05159a02005-02-26 23:04:13 +000025464#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025465 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025466 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025467 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025468 profile_end(&call_start);
25469 profile_sub_wait(&wait_start, &call_start);
25470 profile_add(&fp->uf_tm_total, &call_start);
25471 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025472 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025473 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025474 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25475 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025476 }
25477 }
25478#endif
25479
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 /* when being verbose, mention the return value */
25481 if (p_verbose >= 12)
25482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025484 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025487 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025488 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025489 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025490 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025493 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025494 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025495 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025496 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025497
Bram Moolenaar555b2802005-05-19 21:08:39 +000025498 /* The value may be very long. Skip the middle part, so that we
25499 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025500 * truncate it at the end. Don't want errors such as E724 here. */
25501 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025502 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025503 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025504 if (s != NULL)
25505 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025506 if (vim_strsize(s) > MSG_BUF_CLEN)
25507 {
25508 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25509 s = buf;
25510 }
25511 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025512 vim_free(tofree);
25513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 }
25515 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025516
25517 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 --no_wait_return;
25519 }
25520
25521 vim_free(sourcing_name);
25522 sourcing_name = save_sourcing_name;
25523 sourcing_lnum = save_sourcing_lnum;
25524 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025525#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025526 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025527 script_prof_restore(&wait_start);
25528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529
25530 if (p_verbose >= 12 && sourcing_name != NULL)
25531 {
25532 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025533 verbose_enter_scroll();
25534
Bram Moolenaar555b2802005-05-19 21:08:39 +000025535 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025537
25538 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 --no_wait_return;
25540 }
25541
25542 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025543 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025545
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025546 /* If the a:000 list and the l: and a: dicts are not referenced we can
25547 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025548 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25549 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25550 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25551 {
25552 free_funccal(fc, FALSE);
25553 }
25554 else
25555 {
25556 hashitem_T *hi;
25557 listitem_T *li;
25558 int todo;
25559
25560 /* "fc" is still in use. This can happen when returning "a:000" or
25561 * assigning "l:" to a global variable.
25562 * Link "fc" in the list for garbage collection later. */
25563 fc->caller = previous_funccal;
25564 previous_funccal = fc;
25565
25566 /* Make a copy of the a: variables, since we didn't do that above. */
25567 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25568 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25569 {
25570 if (!HASHITEM_EMPTY(hi))
25571 {
25572 --todo;
25573 v = HI2DI(hi);
25574 copy_tv(&v->di_tv, &v->di_tv);
25575 }
25576 }
25577
25578 /* Make a copy of the a:000 items, since we didn't do that above. */
25579 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25580 copy_tv(&li->li_tv, &li->li_tv);
25581 }
25582}
25583
25584/*
25585 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025586 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025587 */
25588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025589can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025590{
25591 return (fc->l_varlist.lv_copyID != copyID
25592 && fc->l_vars.dv_copyID != copyID
25593 && fc->l_avars.dv_copyID != copyID);
25594}
25595
25596/*
25597 * Free "fc" and what it contains.
25598 */
25599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025600free_funccal(
25601 funccall_T *fc,
25602 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025603{
25604 listitem_T *li;
25605
25606 /* The a: variables typevals may not have been allocated, only free the
25607 * allocated variables. */
25608 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25609
25610 /* free all l: variables */
25611 vars_clear(&fc->l_vars.dv_hashtab);
25612
25613 /* Free the a:000 variables if they were allocated. */
25614 if (free_val)
25615 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25616 clear_tv(&li->li_tv);
25617
25618 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619}
25620
25621/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025622 * Add a number variable "name" to dict "dp" with value "nr".
25623 */
25624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025625add_nr_var(
25626 dict_T *dp,
25627 dictitem_T *v,
25628 char *name,
25629 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025630{
25631 STRCPY(v->di_key, name);
25632 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25633 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25634 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025635 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025636 v->di_tv.vval.v_number = nr;
25637}
25638
25639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 * ":return [expr]"
25641 */
25642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025643ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644{
25645 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025646 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647 int returning = FALSE;
25648
25649 if (current_funccal == NULL)
25650 {
25651 EMSG(_("E133: :return not inside a function"));
25652 return;
25653 }
25654
25655 if (eap->skip)
25656 ++emsg_skip;
25657
25658 eap->nextcmd = NULL;
25659 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025660 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 {
25662 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025663 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 }
25667 /* It's safer to return also on error. */
25668 else if (!eap->skip)
25669 {
25670 /*
25671 * Return unless the expression evaluation has been cancelled due to an
25672 * aborting error, an interrupt, or an exception.
25673 */
25674 if (!aborting())
25675 returning = do_return(eap, FALSE, TRUE, NULL);
25676 }
25677
25678 /* When skipping or the return gets pending, advance to the next command
25679 * in this line (!returning). Otherwise, ignore the rest of the line.
25680 * Following lines will be ignored by get_func_line(). */
25681 if (returning)
25682 eap->nextcmd = NULL;
25683 else if (eap->nextcmd == NULL) /* no argument */
25684 eap->nextcmd = check_nextcmd(arg);
25685
25686 if (eap->skip)
25687 --emsg_skip;
25688}
25689
25690/*
25691 * Return from a function. Possibly makes the return pending. Also called
25692 * for a pending return at the ":endtry" or after returning from an extra
25693 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025694 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025695 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025696 * FALSE when the return gets pending.
25697 */
25698 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025699do_return(
25700 exarg_T *eap,
25701 int reanimate,
25702 int is_cmd,
25703 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704{
25705 int idx;
25706 struct condstack *cstack = eap->cstack;
25707
25708 if (reanimate)
25709 /* Undo the return. */
25710 current_funccal->returned = FALSE;
25711
25712 /*
25713 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25714 * not in its finally clause (which then is to be executed next) is found.
25715 * In this case, make the ":return" pending for execution at the ":endtry".
25716 * Otherwise, return normally.
25717 */
25718 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25719 if (idx >= 0)
25720 {
25721 cstack->cs_pending[idx] = CSTP_RETURN;
25722
25723 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025724 /* A pending return again gets pending. "rettv" points to an
25725 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025727 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 else
25729 {
25730 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025731 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025733 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025735 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736 {
25737 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025738 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025739 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 else
25741 EMSG(_(e_outofmem));
25742 }
25743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025744 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745
25746 if (reanimate)
25747 {
25748 /* The pending return value could be overwritten by a ":return"
25749 * without argument in a finally clause; reset the default
25750 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025751 current_funccal->rettv->v_type = VAR_NUMBER;
25752 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 }
25754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025755 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 }
25757 else
25758 {
25759 current_funccal->returned = TRUE;
25760
25761 /* If the return is carried out now, store the return value. For
25762 * a return immediately after reanimation, the value is already
25763 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025764 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025766 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025767 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025768 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025769 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770 }
25771 }
25772
25773 return idx < 0;
25774}
25775
25776/*
25777 * Free the variable with a pending return value.
25778 */
25779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025780discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781{
Bram Moolenaar33570922005-01-25 22:26:29 +000025782 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783}
25784
25785/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025786 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 * is an allocated string. Used by report_pending() for verbose messages.
25788 */
25789 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025790get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025792 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025794 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025796 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025797 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025798 if (s == NULL)
25799 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025800
25801 STRCPY(IObuff, ":return ");
25802 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25803 if (STRLEN(s) + 8 >= IOSIZE)
25804 STRCPY(IObuff + IOSIZE - 4, "...");
25805 vim_free(tofree);
25806 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807}
25808
25809/*
25810 * Get next function line.
25811 * Called by do_cmdline() to get the next line.
25812 * Returns allocated string, or NULL for end of function.
25813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025815get_func_line(
25816 int c UNUSED,
25817 void *cookie,
25818 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819{
Bram Moolenaar33570922005-01-25 22:26:29 +000025820 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025821 ufunc_T *fp = fcp->func;
25822 char_u *retval;
25823 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824
25825 /* If breakpoints have been added/deleted need to check for it. */
25826 if (fcp->dbg_tick != debug_tick)
25827 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025828 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 sourcing_lnum);
25830 fcp->dbg_tick = debug_tick;
25831 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025832#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025833 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025834 func_line_end(cookie);
25835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836
Bram Moolenaar05159a02005-02-26 23:04:13 +000025837 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025838 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25839 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025840 retval = NULL;
25841 else
25842 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025843 /* Skip NULL lines (continuation lines). */
25844 while (fcp->linenr < gap->ga_len
25845 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25846 ++fcp->linenr;
25847 if (fcp->linenr >= gap->ga_len)
25848 retval = NULL;
25849 else
25850 {
25851 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25852 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025853#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025854 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025855 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025856#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 }
25859
25860 /* Did we encounter a breakpoint? */
25861 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25862 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025863 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025865 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 sourcing_lnum);
25867 fcp->dbg_tick = debug_tick;
25868 }
25869
25870 return retval;
25871}
25872
Bram Moolenaar05159a02005-02-26 23:04:13 +000025873#if defined(FEAT_PROFILE) || defined(PROTO)
25874/*
25875 * Called when starting to read a function line.
25876 * "sourcing_lnum" must be correct!
25877 * When skipping lines it may not actually be executed, but we won't find out
25878 * until later and we need to store the time now.
25879 */
25880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025881func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025882{
25883 funccall_T *fcp = (funccall_T *)cookie;
25884 ufunc_T *fp = fcp->func;
25885
25886 if (fp->uf_profiling && sourcing_lnum >= 1
25887 && sourcing_lnum <= fp->uf_lines.ga_len)
25888 {
25889 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025890 /* Skip continuation lines. */
25891 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25892 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025893 fp->uf_tml_execed = FALSE;
25894 profile_start(&fp->uf_tml_start);
25895 profile_zero(&fp->uf_tml_children);
25896 profile_get_wait(&fp->uf_tml_wait);
25897 }
25898}
25899
25900/*
25901 * Called when actually executing a function line.
25902 */
25903 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025904func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025905{
25906 funccall_T *fcp = (funccall_T *)cookie;
25907 ufunc_T *fp = fcp->func;
25908
25909 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25910 fp->uf_tml_execed = TRUE;
25911}
25912
25913/*
25914 * Called when done with a function line.
25915 */
25916 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025917func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025918{
25919 funccall_T *fcp = (funccall_T *)cookie;
25920 ufunc_T *fp = fcp->func;
25921
25922 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25923 {
25924 if (fp->uf_tml_execed)
25925 {
25926 ++fp->uf_tml_count[fp->uf_tml_idx];
25927 profile_end(&fp->uf_tml_start);
25928 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025929 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025930 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25931 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025932 }
25933 fp->uf_tml_idx = -1;
25934 }
25935}
25936#endif
25937
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938/*
25939 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025940 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 */
25942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025943func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944{
Bram Moolenaar33570922005-01-25 22:26:29 +000025945 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946
25947 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25948 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025949 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 || fcp->returned);
25951}
25952
25953/*
25954 * return TRUE if cookie indicates a function which "abort"s on errors.
25955 */
25956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025957func_has_abort(
25958 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025960 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961}
25962
25963#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25964typedef enum
25965{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025966 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25967 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25968 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969} var_flavour_T;
25970
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025971static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972
25973 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025974var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975{
25976 char_u *p = varname;
25977
25978 if (ASCII_ISUPPER(*p))
25979 {
25980 while (*(++p))
25981 if (ASCII_ISLOWER(*p))
25982 return VAR_FLAVOUR_SESSION;
25983 return VAR_FLAVOUR_VIMINFO;
25984 }
25985 else
25986 return VAR_FLAVOUR_DEFAULT;
25987}
25988#endif
25989
25990#if defined(FEAT_VIMINFO) || defined(PROTO)
25991/*
25992 * Restore global vars that start with a capital from the viminfo file
25993 */
25994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025995read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996{
25997 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025998 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025999 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026000 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001
26002 if (!writing && (find_viminfo_parameter('!') != NULL))
26003 {
26004 tab = vim_strchr(virp->vir_line + 1, '\t');
26005 if (tab != NULL)
26006 {
26007 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026008 switch (*tab)
26009 {
26010 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026011#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026012 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026013#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026014 case 'D': type = VAR_DICT; break;
26015 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026016 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018
26019 tab = vim_strchr(tab, '\t');
26020 if (tab != NULL)
26021 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026022 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026023 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026024 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026026#ifdef FEAT_FLOAT
26027 else if (type == VAR_FLOAT)
26028 (void)string2float(tab + 1, &tv.vval.v_float);
26029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026031 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026032 if (type == VAR_DICT || type == VAR_LIST)
26033 {
26034 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26035
26036 if (etv == NULL)
26037 /* Failed to parse back the dict or list, use it as a
26038 * string. */
26039 tv.v_type = VAR_STRING;
26040 else
26041 {
26042 vim_free(tv.vval.v_string);
26043 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026044 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026045 }
26046 }
26047
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026048 /* when in a function use global variables */
26049 save_funccal = current_funccal;
26050 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026051 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026052 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026053
26054 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026055 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026056 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26057 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058 }
26059 }
26060 }
26061
26062 return viminfo_readline(virp);
26063}
26064
26065/*
26066 * Write global vars that start with a capital to the viminfo file
26067 */
26068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026069write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026070{
Bram Moolenaar33570922005-01-25 22:26:29 +000026071 hashitem_T *hi;
26072 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026073 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026074 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026075 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026076 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026077 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078
26079 if (find_viminfo_parameter('!') == NULL)
26080 return;
26081
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026082 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026083
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026084 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026085 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026087 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026089 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026090 this_var = HI2DI(hi);
26091 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026092 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026093 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026094 {
26095 case VAR_STRING: s = "STR"; break;
26096 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026097 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026098 case VAR_DICT: s = "DIC"; break;
26099 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026100 case VAR_SPECIAL: s = "XPL"; break;
26101
26102 case VAR_UNKNOWN:
26103 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026104 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026105 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026106 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026107 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026108 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026109 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026110 if (p != NULL)
26111 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026112 vim_free(tofree);
26113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 }
26115 }
26116}
26117#endif
26118
26119#if defined(FEAT_SESSION) || defined(PROTO)
26120 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026121store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122{
Bram Moolenaar33570922005-01-25 22:26:29 +000026123 hashitem_T *hi;
26124 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026125 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126 char_u *p, *t;
26127
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026128 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026129 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026131 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026133 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026134 this_var = HI2DI(hi);
26135 if ((this_var->di_tv.v_type == VAR_NUMBER
26136 || this_var->di_tv.v_type == VAR_STRING)
26137 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026138 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026139 /* Escape special characters with a backslash. Turn a LF and
26140 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026141 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026142 (char_u *)"\\\"\n\r");
26143 if (p == NULL) /* out of memory */
26144 break;
26145 for (t = p; *t != NUL; ++t)
26146 if (*t == '\n')
26147 *t = 'n';
26148 else if (*t == '\r')
26149 *t = 'r';
26150 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026151 this_var->di_key,
26152 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26153 : ' ',
26154 p,
26155 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26156 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026157 || put_eol(fd) == FAIL)
26158 {
26159 vim_free(p);
26160 return FAIL;
26161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026162 vim_free(p);
26163 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026164#ifdef FEAT_FLOAT
26165 else if (this_var->di_tv.v_type == VAR_FLOAT
26166 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26167 {
26168 float_T f = this_var->di_tv.vval.v_float;
26169 int sign = ' ';
26170
26171 if (f < 0)
26172 {
26173 f = -f;
26174 sign = '-';
26175 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026176 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026177 this_var->di_key, sign, f) < 0)
26178 || put_eol(fd) == FAIL)
26179 return FAIL;
26180 }
26181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182 }
26183 }
26184 return OK;
26185}
26186#endif
26187
Bram Moolenaar661b1822005-07-28 22:36:45 +000026188/*
26189 * Display script name where an item was last set.
26190 * Should only be invoked when 'verbose' is non-zero.
26191 */
26192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026193last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026194{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026195 char_u *p;
26196
Bram Moolenaar661b1822005-07-28 22:36:45 +000026197 if (scriptID != 0)
26198 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026199 p = home_replace_save(NULL, get_scriptname(scriptID));
26200 if (p != NULL)
26201 {
26202 verbose_enter();
26203 MSG_PUTS(_("\n\tLast set from "));
26204 MSG_PUTS(p);
26205 vim_free(p);
26206 verbose_leave();
26207 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026208 }
26209}
26210
Bram Moolenaard812df62008-11-09 12:46:09 +000026211/*
26212 * List v:oldfiles in a nice way.
26213 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026215ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026216{
26217 list_T *l = vimvars[VV_OLDFILES].vv_list;
26218 listitem_T *li;
26219 int nr = 0;
26220
26221 if (l == NULL)
26222 msg((char_u *)_("No old files"));
26223 else
26224 {
26225 msg_start();
26226 msg_scroll = TRUE;
26227 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26228 {
26229 msg_outnum((long)++nr);
26230 MSG_PUTS(": ");
26231 msg_outtrans(get_tv_string(&li->li_tv));
26232 msg_putchar('\n');
26233 out_flush(); /* output one line at a time */
26234 ui_breakcheck();
26235 }
26236 /* Assume "got_int" was set to truncate the listing. */
26237 got_int = FALSE;
26238
26239#ifdef FEAT_BROWSE_CMD
26240 if (cmdmod.browse)
26241 {
26242 quit_more = FALSE;
26243 nr = prompt_for_number(FALSE);
26244 msg_starthere();
26245 if (nr > 0)
26246 {
26247 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26248 (long)nr);
26249
26250 if (p != NULL)
26251 {
26252 p = expand_env_save(p);
26253 eap->arg = p;
26254 eap->cmdidx = CMD_edit;
26255 cmdmod.browse = FALSE;
26256 do_exedit(eap, NULL);
26257 vim_free(p);
26258 }
26259 }
26260 }
26261#endif
26262 }
26263}
26264
Bram Moolenaar53744302015-07-17 17:38:22 +020026265/* reset v:option_new, v:option_old and v:option_type */
26266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026267reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026268{
26269 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26270 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26271 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26272}
26273
26274
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275#endif /* FEAT_EVAL */
26276
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026278#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026279
26280#ifdef WIN3264
26281/*
26282 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26283 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026284static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26285static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26286static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287
26288/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026289 * Get the short path (8.3) for the filename in "fnamep".
26290 * Only works for a valid file name.
26291 * When the path gets longer "fnamep" is changed and the allocated buffer
26292 * is put in "bufp".
26293 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26294 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 */
26296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026297get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026299 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 char_u *newbuf;
26301
26302 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026303 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026304 if (l > len - 1)
26305 {
26306 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026307 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308 newbuf = vim_strnsave(*fnamep, l);
26309 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311
26312 vim_free(*bufp);
26313 *fnamep = *bufp = newbuf;
26314
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026315 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026316 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 }
26318
26319 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026320 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321}
26322
26323/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 * Get the short path (8.3) for the filename in "fname". The converted
26325 * path is returned in "bufp".
26326 *
26327 * Some of the directories specified in "fname" may not exist. This function
26328 * will shorten the existing directories at the beginning of the path and then
26329 * append the remaining non-existing path.
26330 *
26331 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026332 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026333 * bufp - Pointer to an allocated buffer for the filename.
26334 * fnamelen - Length of the filename pointed to by fname
26335 *
26336 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337 */
26338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026339shortpath_for_invalid_fname(
26340 char_u **fname,
26341 char_u **bufp,
26342 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026344 char_u *short_fname, *save_fname, *pbuf_unused;
26345 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026347 int old_len, len;
26348 int new_len, sfx_len;
26349 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350
26351 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026352 old_len = *fnamelen;
26353 save_fname = vim_strnsave(*fname, old_len);
26354 pbuf_unused = NULL;
26355 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026357 endp = save_fname + old_len - 1; /* Find the end of the copy */
26358 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026360 /*
26361 * Try shortening the supplied path till it succeeds by removing one
26362 * directory at a time from the tail of the path.
26363 */
26364 len = 0;
26365 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026367 /* go back one path-separator */
26368 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26369 --endp;
26370 if (endp <= save_fname)
26371 break; /* processed the complete path */
26372
26373 /*
26374 * Replace the path separator with a NUL and try to shorten the
26375 * resulting path.
26376 */
26377 ch = *endp;
26378 *endp = 0;
26379 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026380 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026381 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26382 {
26383 retval = FAIL;
26384 goto theend;
26385 }
26386 *endp = ch; /* preserve the string */
26387
26388 if (len > 0)
26389 break; /* successfully shortened the path */
26390
26391 /* failed to shorten the path. Skip the path separator */
26392 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026393 }
26394
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026395 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026397 /*
26398 * Succeeded in shortening the path. Now concatenate the shortened
26399 * path with the remaining path at the tail.
26400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026402 /* Compute the length of the new path. */
26403 sfx_len = (int)(save_endp - endp) + 1;
26404 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026406 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026408 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026410 /* There is not enough space in the currently allocated string,
26411 * copy it to a buffer big enough. */
26412 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026414 {
26415 retval = FAIL;
26416 goto theend;
26417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 }
26419 else
26420 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026421 /* Transfer short_fname to the main buffer (it's big enough),
26422 * unless get_short_pathname() did its work in-place. */
26423 *fname = *bufp = save_fname;
26424 if (short_fname != save_fname)
26425 vim_strncpy(save_fname, short_fname, len);
26426 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026428
26429 /* concat the not-shortened part of the path */
26430 vim_strncpy(*fname + len, endp, sfx_len);
26431 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433
26434theend:
26435 vim_free(pbuf_unused);
26436 vim_free(save_fname);
26437
26438 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439}
26440
26441/*
26442 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026443 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444 */
26445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026446shortpath_for_partial(
26447 char_u **fnamep,
26448 char_u **bufp,
26449 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450{
26451 int sepcount, len, tflen;
26452 char_u *p;
26453 char_u *pbuf, *tfname;
26454 int hasTilde;
26455
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026456 /* Count up the path separators from the RHS.. so we know which part
26457 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026458 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026459 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 if (vim_ispathsep(*p))
26461 ++sepcount;
26462
26463 /* Need full path first (use expand_env() to remove a "~/") */
26464 hasTilde = (**fnamep == '~');
26465 if (hasTilde)
26466 pbuf = tfname = expand_env_save(*fnamep);
26467 else
26468 pbuf = tfname = FullName_save(*fnamep, FALSE);
26469
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026470 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026471
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026472 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26473 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474
26475 if (len == 0)
26476 {
26477 /* Don't have a valid filename, so shorten the rest of the
26478 * path if we can. This CAN give us invalid 8.3 filenames, but
26479 * there's not a lot of point in guessing what it might be.
26480 */
26481 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026482 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026484 }
26485
26486 /* Count the paths backward to find the beginning of the desired string. */
26487 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026488 {
26489#ifdef FEAT_MBYTE
26490 if (has_mbyte)
26491 p -= mb_head_off(tfname, p);
26492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026493 if (vim_ispathsep(*p))
26494 {
26495 if (sepcount == 0 || (hasTilde && sepcount == 1))
26496 break;
26497 else
26498 sepcount --;
26499 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501 if (hasTilde)
26502 {
26503 --p;
26504 if (p >= tfname)
26505 *p = '~';
26506 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026507 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026508 }
26509 else
26510 ++p;
26511
26512 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26513 vim_free(*bufp);
26514 *fnamelen = (int)STRLEN(p);
26515 *bufp = pbuf;
26516 *fnamep = p;
26517
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026518 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026519}
26520#endif /* WIN3264 */
26521
26522/*
26523 * Adjust a filename, according to a string of modifiers.
26524 * *fnamep must be NUL terminated when called. When returning, the length is
26525 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026526 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026527 * When there is an error, *fnamep is set to NULL.
26528 */
26529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026530modify_fname(
26531 char_u *src, /* string with modifiers */
26532 int *usedlen, /* characters after src that are used */
26533 char_u **fnamep, /* file name so far */
26534 char_u **bufp, /* buffer for allocated file name or NULL */
26535 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536{
26537 int valid = 0;
26538 char_u *tail;
26539 char_u *s, *p, *pbuf;
26540 char_u dirname[MAXPATHL];
26541 int c;
26542 int has_fullname = 0;
26543#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026544 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545 int has_shortname = 0;
26546#endif
26547
26548repeat:
26549 /* ":p" - full path/file_name */
26550 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26551 {
26552 has_fullname = 1;
26553
26554 valid |= VALID_PATH;
26555 *usedlen += 2;
26556
26557 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26558 if ((*fnamep)[0] == '~'
26559#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26560 && ((*fnamep)[1] == '/'
26561# ifdef BACKSLASH_IN_FILENAME
26562 || (*fnamep)[1] == '\\'
26563# endif
26564 || (*fnamep)[1] == NUL)
26565
26566#endif
26567 )
26568 {
26569 *fnamep = expand_env_save(*fnamep);
26570 vim_free(*bufp); /* free any allocated file name */
26571 *bufp = *fnamep;
26572 if (*fnamep == NULL)
26573 return -1;
26574 }
26575
26576 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026577 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026578 {
26579 if (vim_ispathsep(*p)
26580 && p[1] == '.'
26581 && (p[2] == NUL
26582 || vim_ispathsep(p[2])
26583 || (p[2] == '.'
26584 && (p[3] == NUL || vim_ispathsep(p[3])))))
26585 break;
26586 }
26587
26588 /* FullName_save() is slow, don't use it when not needed. */
26589 if (*p != NUL || !vim_isAbsName(*fnamep))
26590 {
26591 *fnamep = FullName_save(*fnamep, *p != NUL);
26592 vim_free(*bufp); /* free any allocated file name */
26593 *bufp = *fnamep;
26594 if (*fnamep == NULL)
26595 return -1;
26596 }
26597
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026598#ifdef WIN3264
26599# if _WIN32_WINNT >= 0x0500
26600 if (vim_strchr(*fnamep, '~') != NULL)
26601 {
26602 /* Expand 8.3 filename to full path. Needed to make sure the same
26603 * file does not have two different names.
26604 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26605 p = alloc(_MAX_PATH + 1);
26606 if (p != NULL)
26607 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026608 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026609 {
26610 vim_free(*bufp);
26611 *bufp = *fnamep = p;
26612 }
26613 else
26614 vim_free(p);
26615 }
26616 }
26617# endif
26618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026619 /* Append a path separator to a directory. */
26620 if (mch_isdir(*fnamep))
26621 {
26622 /* Make room for one or two extra characters. */
26623 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26624 vim_free(*bufp); /* free any allocated file name */
26625 *bufp = *fnamep;
26626 if (*fnamep == NULL)
26627 return -1;
26628 add_pathsep(*fnamep);
26629 }
26630 }
26631
26632 /* ":." - path relative to the current directory */
26633 /* ":~" - path relative to the home directory */
26634 /* ":8" - shortname path - postponed till after */
26635 while (src[*usedlen] == ':'
26636 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26637 {
26638 *usedlen += 2;
26639 if (c == '8')
26640 {
26641#ifdef WIN3264
26642 has_shortname = 1; /* Postpone this. */
26643#endif
26644 continue;
26645 }
26646 pbuf = NULL;
26647 /* Need full path first (use expand_env() to remove a "~/") */
26648 if (!has_fullname)
26649 {
26650 if (c == '.' && **fnamep == '~')
26651 p = pbuf = expand_env_save(*fnamep);
26652 else
26653 p = pbuf = FullName_save(*fnamep, FALSE);
26654 }
26655 else
26656 p = *fnamep;
26657
26658 has_fullname = 0;
26659
26660 if (p != NULL)
26661 {
26662 if (c == '.')
26663 {
26664 mch_dirname(dirname, MAXPATHL);
26665 s = shorten_fname(p, dirname);
26666 if (s != NULL)
26667 {
26668 *fnamep = s;
26669 if (pbuf != NULL)
26670 {
26671 vim_free(*bufp); /* free any allocated file name */
26672 *bufp = pbuf;
26673 pbuf = NULL;
26674 }
26675 }
26676 }
26677 else
26678 {
26679 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26680 /* Only replace it when it starts with '~' */
26681 if (*dirname == '~')
26682 {
26683 s = vim_strsave(dirname);
26684 if (s != NULL)
26685 {
26686 *fnamep = s;
26687 vim_free(*bufp);
26688 *bufp = s;
26689 }
26690 }
26691 }
26692 vim_free(pbuf);
26693 }
26694 }
26695
26696 tail = gettail(*fnamep);
26697 *fnamelen = (int)STRLEN(*fnamep);
26698
26699 /* ":h" - head, remove "/file_name", can be repeated */
26700 /* Don't remove the first "/" or "c:\" */
26701 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26702 {
26703 valid |= VALID_HEAD;
26704 *usedlen += 2;
26705 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026706 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026707 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708 *fnamelen = (int)(tail - *fnamep);
26709#ifdef VMS
26710 if (*fnamelen > 0)
26711 *fnamelen += 1; /* the path separator is part of the path */
26712#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026713 if (*fnamelen == 0)
26714 {
26715 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26716 p = vim_strsave((char_u *)".");
26717 if (p == NULL)
26718 return -1;
26719 vim_free(*bufp);
26720 *bufp = *fnamep = tail = p;
26721 *fnamelen = 1;
26722 }
26723 else
26724 {
26725 while (tail > s && !after_pathsep(s, tail))
26726 mb_ptr_back(*fnamep, tail);
26727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026728 }
26729
26730 /* ":8" - shortname */
26731 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26732 {
26733 *usedlen += 2;
26734#ifdef WIN3264
26735 has_shortname = 1;
26736#endif
26737 }
26738
26739#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026740 /*
26741 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026742 */
26743 if (has_shortname)
26744 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026745 /* Copy the string if it is shortened by :h and when it wasn't copied
26746 * yet, because we are going to change it in place. Avoids changing
26747 * the buffer name for "%:8". */
26748 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026749 {
26750 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026751 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026752 return -1;
26753 vim_free(*bufp);
26754 *bufp = *fnamep = p;
26755 }
26756
26757 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026758 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026759 if (!has_fullname && !vim_isAbsName(*fnamep))
26760 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026761 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026762 return -1;
26763 }
26764 else
26765 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026766 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767
Bram Moolenaardc935552011-08-17 15:23:23 +020026768 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026769 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026770 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 return -1;
26772
26773 if (l == 0)
26774 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026775 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026776 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026777 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 return -1;
26779 }
26780 *fnamelen = l;
26781 }
26782 }
26783#endif /* WIN3264 */
26784
26785 /* ":t" - tail, just the basename */
26786 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26787 {
26788 *usedlen += 2;
26789 *fnamelen -= (int)(tail - *fnamep);
26790 *fnamep = tail;
26791 }
26792
26793 /* ":e" - extension, can be repeated */
26794 /* ":r" - root, without extension, can be repeated */
26795 while (src[*usedlen] == ':'
26796 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26797 {
26798 /* find a '.' in the tail:
26799 * - for second :e: before the current fname
26800 * - otherwise: The last '.'
26801 */
26802 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26803 s = *fnamep - 2;
26804 else
26805 s = *fnamep + *fnamelen - 1;
26806 for ( ; s > tail; --s)
26807 if (s[0] == '.')
26808 break;
26809 if (src[*usedlen + 1] == 'e') /* :e */
26810 {
26811 if (s > tail)
26812 {
26813 *fnamelen += (int)(*fnamep - (s + 1));
26814 *fnamep = s + 1;
26815#ifdef VMS
26816 /* cut version from the extension */
26817 s = *fnamep + *fnamelen - 1;
26818 for ( ; s > *fnamep; --s)
26819 if (s[0] == ';')
26820 break;
26821 if (s > *fnamep)
26822 *fnamelen = s - *fnamep;
26823#endif
26824 }
26825 else if (*fnamep <= tail)
26826 *fnamelen = 0;
26827 }
26828 else /* :r */
26829 {
26830 if (s > tail) /* remove one extension */
26831 *fnamelen = (int)(s - *fnamep);
26832 }
26833 *usedlen += 2;
26834 }
26835
26836 /* ":s?pat?foo?" - substitute */
26837 /* ":gs?pat?foo?" - global substitute */
26838 if (src[*usedlen] == ':'
26839 && (src[*usedlen + 1] == 's'
26840 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26841 {
26842 char_u *str;
26843 char_u *pat;
26844 char_u *sub;
26845 int sep;
26846 char_u *flags;
26847 int didit = FALSE;
26848
26849 flags = (char_u *)"";
26850 s = src + *usedlen + 2;
26851 if (src[*usedlen + 1] == 'g')
26852 {
26853 flags = (char_u *)"g";
26854 ++s;
26855 }
26856
26857 sep = *s++;
26858 if (sep)
26859 {
26860 /* find end of pattern */
26861 p = vim_strchr(s, sep);
26862 if (p != NULL)
26863 {
26864 pat = vim_strnsave(s, (int)(p - s));
26865 if (pat != NULL)
26866 {
26867 s = p + 1;
26868 /* find end of substitution */
26869 p = vim_strchr(s, sep);
26870 if (p != NULL)
26871 {
26872 sub = vim_strnsave(s, (int)(p - s));
26873 str = vim_strnsave(*fnamep, *fnamelen);
26874 if (sub != NULL && str != NULL)
26875 {
26876 *usedlen = (int)(p + 1 - src);
26877 s = do_string_sub(str, pat, sub, flags);
26878 if (s != NULL)
26879 {
26880 *fnamep = s;
26881 *fnamelen = (int)STRLEN(s);
26882 vim_free(*bufp);
26883 *bufp = s;
26884 didit = TRUE;
26885 }
26886 }
26887 vim_free(sub);
26888 vim_free(str);
26889 }
26890 vim_free(pat);
26891 }
26892 }
26893 /* after using ":s", repeat all the modifiers */
26894 if (didit)
26895 goto repeat;
26896 }
26897 }
26898
Bram Moolenaar26df0922014-02-23 23:39:13 +010026899 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26900 {
26901 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26902 if (p == NULL)
26903 return -1;
26904 vim_free(*bufp);
26905 *bufp = *fnamep = p;
26906 *fnamelen = (int)STRLEN(p);
26907 *usedlen += 2;
26908 }
26909
Bram Moolenaar071d4272004-06-13 20:20:40 +000026910 return valid;
26911}
26912
26913/*
26914 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26915 * "flags" can be "g" to do a global substitute.
26916 * Returns an allocated string, NULL for error.
26917 */
26918 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026919do_string_sub(
26920 char_u *str,
26921 char_u *pat,
26922 char_u *sub,
26923 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026924{
26925 int sublen;
26926 regmatch_T regmatch;
26927 int i;
26928 int do_all;
26929 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026930 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026931 garray_T ga;
26932 char_u *ret;
26933 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026934 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935
26936 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26937 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026938 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939
26940 ga_init2(&ga, 1, 200);
26941
26942 do_all = (flags[0] == 'g');
26943
26944 regmatch.rm_ic = p_ic;
26945 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26946 if (regmatch.regprog != NULL)
26947 {
26948 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026949 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026950 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26951 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026952 /* Skip empty match except for first match. */
26953 if (regmatch.startp[0] == regmatch.endp[0])
26954 {
26955 if (zero_width == regmatch.startp[0])
26956 {
26957 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026958 i = MB_PTR2LEN(tail);
26959 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26960 (size_t)i);
26961 ga.ga_len += i;
26962 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026963 continue;
26964 }
26965 zero_width = regmatch.startp[0];
26966 }
26967
Bram Moolenaar071d4272004-06-13 20:20:40 +000026968 /*
26969 * Get some space for a temporary buffer to do the substitution
26970 * into. It will contain:
26971 * - The text up to where the match is.
26972 * - The substituted text.
26973 * - The text after the match.
26974 */
26975 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026976 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026977 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26978 {
26979 ga_clear(&ga);
26980 break;
26981 }
26982
26983 /* copy the text up to where the match is */
26984 i = (int)(regmatch.startp[0] - tail);
26985 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26986 /* add the substituted text */
26987 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26988 + ga.ga_len + i, TRUE, TRUE, FALSE);
26989 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026990 tail = regmatch.endp[0];
26991 if (*tail == NUL)
26992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026993 if (!do_all)
26994 break;
26995 }
26996
26997 if (ga.ga_data != NULL)
26998 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26999
Bram Moolenaar473de612013-06-08 18:19:48 +020027000 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001 }
27002
27003 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27004 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027005 if (p_cpo == empty_option)
27006 p_cpo = save_cpo;
27007 else
27008 /* Darn, evaluating {sub} expression changed the value. */
27009 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027010
27011 return ret;
27012}
27013
27014#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */